2

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

#include<opencv2\opencv.hpp> #include<opencv2\highgui\highgui.hpp> #include<opencv2\core\core.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.

1

2 Answers 2

4

Try to output as integer

std::cout<<"Value: "<<static_cast<int>(gray_image.at<uchar>(0,0)); 
Sign up to request clarification or add additional context in comments.

Comments

1

You need to typecast your uchar variable to int before printing

like,

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.