如何使用 opencv 访问图像像素
1 ) 假设你要访问第 k 通道、第 i 行、第 j 列的像素。
2 ) 间接访问: ( 通用,但效率低,可访问任意格式的图像)
对于单通道字节型图像:
IplImage*
img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
CvScalar s;
s=cvGet2D(img,i,j); // get the (j,i) pixel value, 注意 cvGet2D
与 cvSet2D 中坐标参数的顺序与其它 opencv 函数坐标参数顺序恰好相反 . 本
函数中 i 代表 y 轴,即 height;j 代表 x 轴,即 weight. 也即先 cvGet2D 的
第二个参数 i 表示行号,第三个参数 j 表示列号。而元素的坐标值的( j , i )
printf("intensity=%f\n",s.val[0]);
s.val[0]=111;
cvSet2D(img,i,j,s); // set the (j,i) pixel value
对于多通道字节型/浮点型图像:
IplImage*
img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
CvScalar s;
s=cvGet2D(img,i,j); // get the (j,i) pixel value
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
s.val[0]=111;
s.val[1]=111;
s.val[2]=111;
cvSet2D(img,i,j,s); // set the (j,i) pixel value
3 ) 直接访问: ( 效率高,但容易出错)
对于单通道字节型图像:
IplImage*
img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
((uchar *)(img->imageData + i*img->widthStep))[j]=111;
对于多通道字节型图像:
IplImage*
img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
((uchar *)(img->imageData +
i*img->widthStep))[j*img->nChannels + 0]=111; // B
((uchar *)(img->imageData +
i*img->widthStep))[j*img->nChannels + 1]=112; // G
((uchar *)(img->imageData +
i*img->widthStep))[j*img->nChannels + 2]=113; // R
对于多通道浮点型图像:
IplImage*
img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
((float *)(img->imageData +
i*img->widthStep))[j*img->nChannels + 0]=111; // B
((float *)(img->imageData +
i*img->widthStep))[j*img->nChannels + 1]=112; // G
((float *)(img->imageData +
i*img->widthStep))[j*img->nChannels + 2]=113; // R
4 ) 基于指针的直接访问: ( 简单高效)
对于单通道字节型图像:
IplImage* img =
cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
int height = img->height;
int width = img->width;
int step = img->widthStep;
uchar* data = (uchar *)img->imageData;
data[i*step+j] = 111;
对于多通道字节型图像:
IplImage* img =
cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
int height = img->height;
int width = img->width;
int step = img->widthStep;
int channels = img->nChannels;
uchar* data = (uchar *)img->imageData;
data[i*step+j*channels+k] = 111;
对于多通道浮点型图像(假设图像数据采用 4 字节(32 位)行对齐方式):
IplImage* img =
cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
int height = img->height;
int width = img->width;
int step = img->widthStep;
int channels = img->nChannels;
float * data = (float *)img->imageData;
data[i*step+j*channels+k] = 111;
5 ) 基于 c++ wrapper 的直接访问: (更简单高效)
首先定义一个 c++ wrapper ‘Image’,然后基于 Image 定义不同类型的图像:
template<class T> class Image
{
private:
IplImage* imgp;
public:
Image(IplImage* img=0) {imgp=img;}
~Image(){imgp=0;}
void operator=(IplImage* img) {imgp=img;}
inline T* operator[](const int rowIndx) {
return ((T *)(imgp->imageData +
rowIndx*imgp->widthStep));}
};
typedef struct{
unsigned char b,g,r;
} RgbPixel;
typedef struct{
float b,g,r;
} RgbPixelFloat;
typedef Image<RgbPixel> RgbImage;
typedef Image<RgbPixelFloat> RgbImageFloat;
typedef Image<unsigned char> BwImage;
typedef Image<float> BwImageFloat;
对于单通道字节型图像:
IplImage*
img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
BwImage imgA(img);
imgA[i][j] = 111;
对于多通道字节型图像:
IplImage*
img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
RgbImage imgA(img);
imgA[i][j].b = 111;
imgA[i][j].g = 111;
imgA[i][j].r = 111;
对于多通道浮点型图像:
IplImage*
img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
RgbImageFloat imgA(img);
imgA[i][j].b = 111;
imgA[i][j].g = 111;
imgA[i][j].r = 111;