OpenCV:访问并获取像素的平方根

时间:2009-06-15 21:13:04

标签: c++ c opencv

我正在使用OpenCV进行对象检测,我希望能够执行的操作之一是每像素平方根。我想这个循环会是这样的:

IplImage* img_;
...
for (int y = 0; y < img_->height; y++) {
  for(int x = 0; x < img_->width; x++) {
    // Take pixel square root here
  }
}

我的问题是如何在IplImage对象中的坐标(x,y)处访问像素值?

4 个答案:

答案 0 :(得分:3)

假设img_是IplImage类型,假设16位无符号整数数据,我会说

unsigned short pixel_value = ((unsigned short *)&(img_->imageData[img_->widthStep * y]))[x];

另请参阅here了解IplImage定义。

答案 1 :(得分:1)

OpenCV IplImage是一维数组。您必须创建单个索引才能获取图像数据。像素的位置将基于图像中的颜色深度和通道数。

// width step 
int ws = img_->withStep;
// the number of channels (colors)
int nc = img_->nChannels;
// the depth in bytes of the color 
int d = img_->depth&0x0000ffff) >> 3;
//  assuming the depth is the size of a short
unsigned short * pixel_value = (img_->imageData)+((y*ws)+(x*nc*d));
// this gives you a pointer to the first color in a pixel
//if your are rolling grayscale just dereference the pointer. 

您可以通过移动像素指针pixel_value ++来选择通道(颜色)。如果这将是任何类型的实时应用程序,我建议使用查找表作为像素的平方根。

答案 2 :(得分:1)

请使用CV_IMAGE_ELEM宏。 另外,考虑使用功率= 0.5的cvPow,而不是自己处理像素,无论如何应该避免使用

答案 3 :(得分:0)

你可以在Gady Agam的漂亮的OpenCV教程here中找到几种到达图像元素的方法。