简体   繁体   中英

Accessing pixel value using cv::Mat::at operator

I'm using OpenCV 2.3.1 (c++ api) and when I try to get the pixel value of colored image, I'm getting very strange results, instead of value number, output is something like this: ?, *, | etc. For example, the code is as follows:

cv::Mat inputImage = cv::imread("Picture1.jpg");
std::cout << inputImage.at<cv::Vec3b>(x,y)[0] << std::endl; //print B component

where x and y are coordinates from mouse callback function. I assume that type is wrong, do you have any idea what else could I use instead of Vec3b?

I assume that the problem happening because you are using "at(x, y)". Documentation says that the first argument should be "The 0-based row index" and second "The 0-based column index". So, you should either call at(y, x) or at(cv::Point(x, y)).

Check http://opencv.willowgarage.com/documentation/cpp/basic_structures.html

Vec3b in OpenCV is typedef Vec<uchar, 3> Vec3b; . So, I guess you need to cast to integer in the cout process.

Something like std::cout << (int) inputImage.at<cv::Vec3b>(x,y)[0] << std::endl; //print B component std::cout << (int) inputImage.at<cv::Vec3b>(x,y)[0] << std::endl; //print B component

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