C++ – Show Vec3b pixel values in OpenCV Mat image

cmatopencv

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

Best Answer

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];
Related Topic