鼠标处理:打印像素位置 [英] Mouse handling: printing pixel location

查看:74
本文介绍了鼠标处理:打印像素位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在VS2010中使用OpenCV做一些工作,特别是在鼠标处理方面.到目前为止,我有这个:

I've been trying to do some work with OpenCV in VS2010, specifically in the area of mouse handling. So far, I have this:

CV_EVENT_LBUTTONDOWN
        :drawing_line = true;
        cvLine( frame, cvPoint(x,y),cvPoint(350,500), CV_RGB(255,0,0), CV_AA, 15,0 );
        fprintf( stdout, "Point found. %i, %i \n", object_x0, object_y0 );
        break;

我想要做的是返回我单击的像素的位置,但返回的只是找到点0,0",而不是实际位置.最终,我想在cvLine中使用这些点画一条线,但是现在我只想返回一些值给我.任何建议将不胜感激.谢谢!

What I want it to do is return the location of the pixels that I clicked on but all it returns is "Point found. 0,0" instead of the actual location. Eventually, I would like to use the points with cvLine to draw a line but right now I would just like to get some values returned to me. Any suggestions would be much appreciated. Thanks!

推荐答案

您可以通过将鼠标参数作为参数传递给鼠标回调函数来获得鼠标单击的位置,如下所示:

You can obtain the position of a mouse-click by passing it as the parameter to the mouse callback function like so:

void onMouse(int evt, int x, int y, int flags, void* param) {
    if(evt == CV_EVENT_LBUTTONDOWN) {
        cv::Point* ptPtr = (cv::Point*)param;
        ptPtr->x = x;
        ptPtr->y = y;
    }
}

int main() {
    cv::Point2i pt(-1,-1);
    cv::namedWindow("Output Window");
    frame = cv::imread("image.jpg");
    cv::imshow(winName, frame);

    cv::setMouseCallback(winName, onMouse, (void*)&pt);
    // Note that we passed '&pt' (a pointer
    // to `pt`) to the mouse callback function.
    // Therefore `pt` will update its [x,y] coordinates
    // whenever user left-clicks on the image in "Output Window".
}

这篇关于鼠标处理:打印像素位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆