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);
下一篇: 在OpenCV中获取像素值
