Illuminati0x5B
Illuminati0x5B

Reputation: 612

Getting pixel value in OpenCV

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.

Upvotes: 2

Views: 7708

Answers (2)

AlexBG
AlexBG

Reputation: 421

Try to output as integer

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

Upvotes: 4

Haris
Haris

Reputation: 14053

You need to typecast your uchar variable to int before printing

like,

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

Upvotes: 1

Related Questions