我正在查看图像处理算法的袖珍手册(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))
见第33页的pdf。
#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 :(得分:1)
Im
是指向Image结构的指针
Im->Data
指向缓冲区。我们称之为buffer
Im->Cols
表示列数。 num_columns
buffer + x * num_columns + y
指向像素
顺便说一句,这是一种非常低效的遍历图像的方法,因为您正在计算每个点的位置。
你已经有2个for循环。使用这个宏是没有意义的。您可以轻松使用单个指针并进行调整。
类似的东西会更有效率(我还没有测试过):
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;
}