在OpenCV Mat图像

时间:2016-02-05 11:24:56

标签: c++ opencv mat

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

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

我明白了:

  

我:[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通道中的像素值?

*** EDIT1:这是我创建Mat图像的方式(我已经有了一个Mat 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;

我明白了:

  

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

     

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

     

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

如果我更改坐标?

  

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

     

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

     

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

2 个答案:

答案 0 :(得分:1)

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

只需更正作业:

cblue = centralIntensity[0];
cgreen = centralIntensity[1];
cred = centralIntensity[2];

答案 1 :(得分:0)

  • 对于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
       }
    }