user3396218
user3396218

Reputation: 255

What does this code mean? OpenCV

May I know what does x represent? Does the content inside of () represent a pointer? then the [] is the element of the array?

 x = (df_dx->imageData+i*df_dx->widthStep)[j]; 

why doesnt it work if i put it this way?

 x=df_dx[2][j];

Does the

->imageData

give the pixel value of the image? The full code is below. THanks

float x;
IplImage*df_dx = cvCreateImage(cvGetSize(grayimg),IPL_DEPTH_16S,1); 


for(int i=0;i=grayimg->height;i++)
{
for(int j=0;grayimg->width;j++)
{

x = (df_dx->imageData+i*df_dx->widthStep)[j]; 
}
}

Upvotes: 0

Views: 79

Answers (1)

Jonathan H
Jonathan H

Reputation: 7962

Short answers:

  • x is the element on row j, column i of the matrix df_dx
  • df_dx->imageData returns a pointer to the first element of the array (top-left)

Accessing matrix elements in such a way is actually quite common for 2d matrices stored in memory as flat one-dimensional arrays (see e.g. GSL matrix accesses for a similar example with another library; see also this post for a similar question).

Storing a matrix as an array of arrays (i.e. using [][] for access) is definitely manageable, and many people chose to use this representation, but there are reasons why one would prefer a flat layout in memory.

Upvotes: 1

Related Questions