0

I am having what is follow: img is an IplImage of HeightXWidth. having three channels I want to access the three values of the pixel in one image, using another solution rather that the following one that I did:

IplImage* img=cvCreateImage(...);
...
CvScalar scalar= cvGet2D(img, i, j); //where i and j are the coordinates of the considered pixel

double v0=scalar.val[0];
double v1=scalar.val[1];
double  v2=scalar.val[2];

My idea is to get rid of the OpenCV dependency and change it in another way.

Any suggestions?

4

1 回答 1

0

您可以使用CV_IMAGE_ELEM访问具有 x,y 坐标的像素,例如

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

例如,如果您想使用 i,j 访问 IplImage *img(3 channel) 的第二个通道,您可以拥有

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

或使用以下方法访问 r,g,b 像素值

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];
       }
     }

有关更多详细信息,请参阅此 Stack-overflow 问题

于 2013-10-01T05:12:59.490 回答