Show Vec3b pixel values in OpenCV Mat image
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
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];
对于Vec3b,你可以这样做,
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
}
}
上一篇: OpenCV复制到Mat没有透明像素