0

Possible Duplicate:
OpenCV rgb value for cv::Point in cv::Mat

As you know, in matlab it's easy to get r/g/b values using r = image(:,:,1).

But in openCV (before 2.2) we must use pointer like this:

plImage* 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

But as openCV2.3 comes out, it's easy to get pixel value of a single channel image like this:

Mat image;
int pixel = image.at<uchar>(row,col);

So I just wonder it there also a easy way to get the r,g,b pixel value of a multichannel image just like that in the Matlab? Any help will be appreciated =)

Community
  • 1
  • 1
yvetterowe
  • 1,239
  • 7
  • 20
  • 34
  • Here is my answer to this same question from a while back: http://stackoverflow.com/questions/7899108/opencv-get-pixel-information-from-mat-image/7903042#7903042 – mevatron Nov 25 '11 at 07:25

2 Answers2

4

For C++ interface you can do:

Vec3f pixel = image.at<Vec3f>(row, col);

int b = pixel[0];
int g = pixel[1];
int r = pixel[2];
Sam
  • 19,708
  • 4
  • 59
  • 82
2

as vasile said, getting a cell as a Vec3 will get you the pixel with easy access to its rgb components, this is the simplest solution in opencv since the data structure saves the pixels in the following format "RGBRGBRGBRGBRGB..." while matlab saves it as "RRRRRRRGGGGGGGBBBBBBBB..." to get a specified channel like in matlab you can use the CvSplit (or cv::split in c++ style), this function will split the image into its 3-4 different channels so you could access a channels like in matlab. in the provided links you can find also a reference for the opposite function - merge

Boaz
  • 4,549
  • 2
  • 27
  • 40