简体   繁体   English

使用cv :: Mat :: at运算符访问像素值

[英]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: ?, *, | 我正在使用OpenCV 2.3.1(c ++ api),当我尝试获取彩色图像的像素值时,得到的结果非常奇怪,而不是数值,输出是这样的:?,*,|。 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. 其中x和y是鼠标回调函数的坐标。 I assume that type is wrong, do you have any idea what else could I use instead of Vec3b? 我认为该类型是错误的,您是否知道我还能用什么代替Vec3b?

I assume that the problem happening because you are using "at(x, y)". 我认为发生问题是因为您使用的是“ at(x,y)”。 Documentation says that the first argument should be "The 0-based row index" and second "The 0-based column index". 文档说,第一个参数应为“基于0的行索引”,第二个参数应为“基于0的列索引”。 So, you should either call at(y, x) or at(cv::Point(x, y)). 因此,您应该调用at(y,x)或at(cv :: Point(x,y))。

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

Vec3b in OpenCV is typedef Vec<uchar, 3> Vec3b; OpenCV中的Vec3b是typedef Vec<uchar, 3> Vec3b; . So, I guess you need to cast to integer in the cout process. 因此,我想您需要在cout流程中将其转换为整数。

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 std::cout << (int) inputImage.at<cv::Vec3b>(x,y)[0] << std::endl; //print B component

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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