opencv - 使用鼠标回调打印坐标和像素值

标签 opencv mouseevent cout 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";

}

我得到的输出是:-

screenshot

打印了坐标值但没有正确打印像素值。我犯了什么错误??

最佳答案

cout 将 char 打印为字符,就像您看到的那样。

只需将它们用转换为 int 的形式进行打印即可:

cout << int( thresh.at<uchar>(y,x) )

also note, that it's at<uchar>(y,x), not x,y

关于opencv - 使用鼠标回调打印坐标和像素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14919897/

相关文章:

c++ - 将 vtkImageData 复制到 cv::Mat

javascript - 覆盖 Edge 上的触摸板滚动

javascript - 无法在 html 文本字段中输入文本

c++ - C++ 中的过度循环 (Cout)

java - Android - OpenCV 模板与阈值匹配

ios - Xcode 5.1 构建 opencv 在 64 位模拟器上失败

Java Swing : Extended jpanel class will not brighten upon repainting.

C++ 插入 << 运算符重载

c++ - C++中的cin缓冲区问题

c++ - 点坐标数据集中的众数