Kar*_*gan 6 opencv cout mouseevent mat
这是我尝试的代码,只打印坐标值而不是像素值.
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
void onMouse( int event, int x, int y, int, void* );
using namespace cv;
Mat img = cv::imread("b.jpg", 0); // force grayscale
Mat thresh=Mat::zeros(img.size(),CV_8UC1);
int main(int argc, char **argv)
{
if(!img.data) {
std::cout << "File not found" << std::endl;
return -1;
}
threshold(img,binary,50,255,THRESH_TOZERO);
namedWindow("thresh");
setMouseCallback( "thresh", onMouse, 0 );
imshow("thresh",thresh);
}
void onMouse( int event, int x, int y, int, void* )
{
if( event != CV_EVENT_LBUTTONDOWN )
return;
Point pt = Point(x,y);
std::cout<<"x="<<pt.x<<"\t y="<<pt.y<<"\t value="<<thresh.at<uchar>(x,y)<<"\n";
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出为: -

打印坐标值但不能正确打印像素值.我犯了什么错?
cout将uchar打印为字符,就像你看到的那样.
只需用cast转换为int进行打印:
cout << int( thresh.at<uchar>(y,x) )
also note, that it's at<uchar>(y,x), not x,y
Run Code Online (Sandbox Code Playgroud)