2016-03-03 53 views
2

我在看图像处理算法的掌上手册(http://adaptiveart.eecs.umich.edu/2011/wp-content/uploads/2011/09/The-pocket-handbook-of-image-processing-algorithms-in-C.pdf),我碰到这个代码(下面)。图像处理算法代码,解释指针

谁能帮我了解

*(Im->Data + (x)*Im->Cols + (y)) 

这是PDF页面33

#define pix(Im,x,y) \ 
     *(Im->Data + (x)*Im->Cols + (y)) 
/* Compute and return area for objects */ 

int area(struct Image *In, int x1, int y1, int x2, int y2, unsigned char ObjVal){ 
    long i, j, rows; 
    int area_value = 0; 

    for(i=x1; i<=x2; ++i) 
     for(j=y1; j<=y2; ++j){ 
      if(pix(In,i,j)==ObjVal)++area_value; 
     } 
    return(area_value); 
} 
+0

它假定一个2维阵列中,存储在行优先顺序。这就是如何计算m乘n 2D数组中的数据点(x,y)的方式。 – rts1

+0

图像存储在一个数组中,二维数组中的p [x] [y]'是'p [x *列+ y]'(或者等价于* *(p + x *列) + y)')。 – molbdnilo

+0

使用现代的C++界面 – dynamic

回答

1

Im上是指向图像结构

Im->Data所指向的缓冲区。我们称之为buffer

Im->Cols表示列数。​​

buffer + x * num_columns + y指向像素

顺便说一句,这是遍历你的形象,因为你是计算每个点的位置非常低效的方式。

你已经有2 for循环那里。使用这个宏没有意义。您可以轻松使用单个指针并进行调整。

类似的东西这将是更有效的(I没有测试它):

int area(struct Image *In, int x1, int y1, int x2, int y2, unsigned char ObjVal) 
{ 
    int area_value = 0; 

    unsigned char *p = Im->Data + x1 * Im->Cols + y1; // Move to the first pixel 

    int adj = In->Cols - (x2-x1)  // How much for the start of next row 

    for(int i=x1; i<=x2; ++i, p += adj) 
    { 
     for(int j=y1; j<=y2; ++j, ++p) 
     { 
      if (*p == ObjVal) ++area_value; 
     } 
    } 

    return area_value; 
}