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

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

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

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

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

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

有关IplImage定义,另请参见此处 。

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 ++来选择一个通道(颜色)。 如果这将是任何类型的实时应用程序,我建议使用查找表的像素的平方根。

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

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