OpenCV获取某点的像素值

1.cvGet2D(图片, y坐标, x坐标)    速度慢

CvScalar s;
s = cvGet2D(src, j,i);//获取src图像中坐标为(i,j)的像素点的值
s.val[0] 代表src图像BGR中的B通道的值~

例如:

CvScalar s = cvGet2D(img, y, x);
printf(" RGB : B=%d, G=%d, R=%d \n" , (int)s.val[0], (int)s.val[1], (int)s.val[2]);

 

2.直接访存Imagedata

3通道图像存储结构如下:

|························  widthStep  ·············································|

(B,G,R) (B,G,R) (B,G,R)  (B,G,R) (B,G,R) (B,G,R) (B,G,R)  (B,G,R)

(B,G,R) (B,G,R) (B,G,R)  (B,G,R) (B,G,R) (B,G,R) (B,G,R)  (B,G,R)

(B,G,R) (B,G,R) (B,G,R)  (B,G,R) (B,G,R) (B,G,R) (B,G,R)  (B,G,R)

因此,要乘以 3 才能正确,例如:Img(x,y),x,y是像素坐标

blue:   ((uchar*)(img->imageData + img->widthStep*y))[x*3]
green:   ((uchar*)(img->imageData + img->widthStep*y))[x*3+1]
red:   ((uchar*)(img->imageData + img->widthStep*y))[x*3+2]

3.将ImageData方法定义成宏 CV_IMAGE_ELEM(img,elemType,row,column),可以当成函数用

  for(int j = 0;j < RGBimg->height;j++)
 {
    for (int i = 0;i < RGBimg->widthStep;i += 3) //每次移动 3 ,
    {

       uchar B = CV_IMAGE_ELEM(RGBimg,uchar,j,i+0);
       uchar G = CV_IMAGE_ELEM(RGBimg,uchar,j,i+1);
       uchar R = CV_IMAGE_ELEM(RGBimg,uchar,j,i+2);

      }

}

 

推荐使用第2或3种方法,速度快。个人提倡CV_IMAGE_ELEM()。

 

 

你可能感兴趣的:(opencv)