如何在OpenCV中访问像素?

时间:2011-09-23 22:11:54

标签: image video opencv pixel

我有一个x,y点坐标,如何使用它来访问IplImage上的特定点?

由于

2 个答案:

答案 0 :(得分:7)

使用CV_IMAGE_ELEM

CV_IMAGE_ELEM( image_header, elemtype, y, x*N+C )

E.g。给定一个8位3通道(如RGB)IplImage* img,我们希望第二通道上有(x,y)

CV_IMAGE_ELEM(img, uchar, y, (x * 3) + 1))

答案 1 :(得分:3)

或者,你可以这样做。更多矩阵运算,请看这里。

http://note.sonots.com/OpenCV/MatrixOperations.html

     int col, row, z;
     uchar b, g, r;
     for( y = 0; row < img->height; y++ )
     {
       for ( col = 0; col < img->width; col++ )
       {
         //for( z = 0; z < img->nChannels; z++ )
         //{
         //   c = img->imageData[img->widthStep * row + col * img->nChannels + z];
         //}
         b = img->imageData[img->widthStep * row + col * 3]
         g = img->imageData[img->widthStep * row + col * 3 + 1];
         r = img->imageData[img->widthStep * row + col * 3 + 2];
       }
     }