Getting pixel value in OpenCV

I'm new to OpenCV and I'm trying to get the pixel value from a grayscale image.

#include<opencv2opencv.hpp>
#include<opencv2highguihighgui.hpp>
#include<opencv2corecore.hpp>
#include<iostream>

using namespace cv;

int main()
{
    VideoCapture cap(1);
    Mat image,gray_image;
    cap>>image;
    cvtColor(image,gray_image,CV_BGR2GRAY);
    std::cout<<"Value: "<<gray_image.at<uchar>(0,0);
    imshow("Window",gray_image);
    waitKey(0);
    return 0;
}

The pixel value is displayed as * or ~ etc. I think it is getting converted to ASCII value. How do I fix that?

Thank you.


尝试输出为整数

std::cout<<"Value: "<<static_cast<int>(gray_image.at<uchar>(0,0));

You need to typecast your uchar variable to int before printing

like,

std::cout<<"Value: "<<(int)src.at<uchar>(0,0);
链接地址: http://www.djcxy.com/p/89788.html

上一篇: 通过强度值OpenCV规范化和图像的颜色通道

下一篇: 在OpenCV中获取像素值