简体   繁体   中英

OpenCV Mat method “at” returning strange character in Linux

I am looking for a way to acces the value of the grayscale pixel in a cv::Mat object, I was able to find a lot of answers and I'm sure they worked, but for me they just don't do. So basically what I have is the following:

    gray_image = imread("myimage.png", CV_LOAD_IMAGE_GRAYSCALE);

    equalizeHist(gray_image, eq_image);

    // This line prints garbage
    const unsigned char* row = eq_image.ptr<unsigned char>(10);
    cout << row[10] << endl;

    // This line also prints garbage
    cout << eq_image.at<uchar>(10, 10) << endl;

I just want to see the grayscale[0,255] value of the pixel, in position (10,10). I'm pretty sure those 2 lines worked for some other people, but not for me, maybe it's a Linux thing.

How can I read the cv::Mat pixel in a grayscale integer ?

Thank you,

The value is printed as an ASCII character, that is depending on the actual value possibly non-printable garbage. If you are interested to print the pixel value as an integer number instead, you need to cast the value to an int to get the other operator<< overload :

cout << static_cast<int>(row[10]) << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM