1
votes

I want to know the values in a Vec3b pixel in a Mat image. I'm not really an OpenCV expert.My Vec3b is, and if I print the Vec3b :

Vec3b centralIntensity;
cx=70; cy=700;
centralIntensity = (Vec3b)imgTemp->at<Vec3b>(cx, cy);
cout<<"I:["<<cx<<","<<cy<<"]="<<centralIntensity<<endl;

I get:

I:[70,700]=[150, 162, 160]

If i print the single values:

 cout<<"***Uchar:["<<cx<<","<<cy<<"]="<<int(centralIntensity[0])<<","<<int(centralIntensity[1])<<","<<int(centralIntensity[2])<<endl;

I get:

Uchar:[70,700]=127,0,0

I noticed that if I change coordinates, the last print is always the same. Since I have to compare intensity pixel on the different channels, what would be a good way to do it and to know pixel values in BGR channels?

***EDIT1: This is how I create my Mat image (I already have a Mat imgIn as input) and I show in another way Vec3b:

Mat *imgTemp;
uchar cblue, blue, cgreen;
imgTemp = new Mat(imgIn->size(), CV_8UC3);
imgIn->convertTo(*imgTemp, CV_8UC3);
Vec3b centralIntensity;
centralIntensity = (Vec3b)imgTemp->at<Vec3b>(cx, cy);
cout<<"I:["<<cx<<","<<cy<<"]="<<centralIntensity<<endl;
centralIntensity[0] = cblue;
centralIntensity[1] = cgreen;
centralIntensity[2] = cred;
cout<<"I:["<<cx<<","<<cy<<"]="<<centralIntensity<<endl;
cout<<"***Uchar:["<<cx<<","<<cy<<"]="<<int(centralIntensity[0])<<","<<int(centralIntensity[1])<<","<<int(centralIntensity[2])<<endl;
cout<<"***Uchar:["<<cx<<","<<cy<<"]="<<int(cblue)<<","<<int(cgreen)<<","<<int(cred)<<endl;

I get:

I:[70,700]=[150, 162, 160]

***Uchar:[70,700]=127,0,0

***Uchar:[70,700]=127,0,0

If I change coordinates?

I:[300,400]=[109, 123, 105]

***Uchar:[300,400]=127,0,0

***Uchar:[300,400]=127,0,0

2
Please show this issue in a minimal reproducible example, as this works as expected (prints correct values) for me. Also make sure that imgTemp is a CV_8UC3 and correctly initialized. If you explain what is your final goal, there is a good chance it can be achieved with a better approach than scanning the matrix explicitly. - Miki
why do you convert to int? - orkan
@orkan otherwise it will print the ascii characters, not the number - Miki
@Miki I have edited my question with a complete example. I would like to know why the values in the Vec3b components are the same? - Franktrt
where are you assigning cblue, cgreen, cred? - Miki

2 Answers

1
votes

You are overwriting centralIntensity with uninitialized values cblue, cred, cgreen, which will have random values inside.

Just correct the assignment:

cblue = centralIntensity[0];
cgreen = centralIntensity[1];
cred = centralIntensity[2];
0
votes
  • For Vec3b , you can do it by this way,

    Mat image = imread("img_Path");
    for(int y=0;y<img.rows;y++)
    {
       for(int x=0;x<img.cols;x++)
       {
          // get pixel
          Vec3b color = image.at<Vec3b>(x, y);
    
          //color.val[0] // B
          //color.val[1] // G
          //color.val[2] // R
       }
    }