在OpenCV Mat图像中显示Vec3b像素值

我想知道Mat图像中Vec3b像素的值。 我不是一个真正的OpenCV专家。我的Vec3b是,如果我打印Vec3b:

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

我得到:

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

如果我打印单个值:

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

我得到:

UCHAR:[70700] = 127,0,0

我注意到如果我改变坐标,最后的打印总是一样的。 由于我必须比较不同通道上的强度像素,因此在BGR通道中使用像素值的方法是什么?

***编辑1:这是我如何创建我的地毯图像(我已经有一个垫imgIn作为输入),我用另一种方式显示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:[70,700] = [150,162,160]

*** UCHAR:[70700] = 127,0,0

*** UCHAR:[70700] = 127,0,0

如果我改变坐标?

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

*** UCHAR:[300400] = 127,0,0

*** UCHAR:[300400] = 127,0,0


您正在使用未初始化的值cbluecredcgreen覆盖centralIntensity ,其中将包含随机值。

只需纠正任务:

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
       }
    }
    
  • 链接地址: http://www.djcxy.com/p/89819.html

    上一篇: Show Vec3b pixel values in OpenCV Mat image

    下一篇: How to detect and fix dead pixels in images using OpenCV?