如何使用opencv获取位置并绘制矩形? [英] How can I get the position and draw rectangle using opencv?

查看:39
本文介绍了如何使用opencv获取位置并绘制矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在图片框中移动和单击鼠标时获得一个位置.我想在单击鼠标的时间和位置在图像窗口中创建矩形.

I want to get a position when move and click mouse in picturebox. I want to create rectangle in the image window when and where a mouse is clicked.

我有一个来自文档的简单代码

#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;
 
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
    if ( event == EVENT_LBUTTONDOWN )
    {
        cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;    
    }
    else if( event == EVENT_RBUTTONDOWN )
    {
        cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    }
    else if( event == EVENT_MBUTTONDOWN )
    {
        cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    }
    else if ( event == EVENT_MOUSEMOVE )
    {
        cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;
    }
}

int main(int argc, char** argv)
{
    bool isDragging = false;

    // Read image from file 
    Mat img = imread("input/pic1.jpg");

    //if fail to read the image
    if( img.empty() ) 
    { 
        cout << "Error loading the image" << endl;
        return -1; 
    }

    //Create a window
    namedWindow("My Window", 1);

    //set the callback function for any mouse event
    setMouseCallback("My Window", CallBackFunc, NULL);

    //show the image
    imshow("My Window", img);

    // Wait until user press some key
    waitKey(0);

    return 0;
}

它在 windows form = 上工作,但我想使用鼠标点击.我把代码放在 GUI 上.它抛出以下错误:

错误 3 错误 C3867:'ProjectFinal::MyForm::CallBackFunc':函数调用缺少参数列表;使用&ProjectFinal::MyForm::CallBackFunc"创建一个指向成员 c:users ungningzdocumentsvisual studio 2012projectsprojectfinalprojectfinalMyForm.h 690 1 ProjectFinal

Error 3 error C3867: 'ProjectFinal::MyForm::CallBackFunc': function call missing argument list; use '&ProjectFinal::MyForm::CallBackFunc' to create a pointer to member c:users ungningzdocumentsvisual studio 2012projectsprojectfinalprojectfinalMyForm.h 690 1 ProjectFinal

错误 6 错误 C3867:'ProjectFinal::MyForm::CallBackFunc':函数调用缺少参数列表;使用&ProjectFinal::MyForm::CallBackFunc"创建一个指向成员 c:users ungningzdocumentsvisual studio 2012projectsprojectfinalprojectfinalMyForm.h 690 1 ProjectFinal

Error 6 error C3867: 'ProjectFinal::MyForm::CallBackFunc': function call missing argument list; use '&ProjectFinal::MyForm::CallBackFunc' to create a pointer to member c:users ungningzdocumentsvisual studio 2012projectsprojectfinalprojectfinalMyForm.h 690 1 ProjectFinal

7 IntelliSense:指向成员的指针对于托管类 c:UsersNungNingZDocumentsVisual Studio 2012ProjectsProjectFinalProjectFinalMyForm.h 690 37 ProjectFinal 无效

7 IntelliSense: a pointer-to-member is not valid for a managed class c:UsersNungNingZDocumentsVisual Studio 2012ProjectsProjectFinalProjectFinalMyForm.h 690 37 ProjectFinal

推荐答案

所以您遇到了与您的问题无关的问题.

So you have a problem unrelated to your question.

但是,您可以仅使用 OpenCV highgui 工具来实现您的目标:

However, you can achieve your goal using only OpenCV highgui facilites:

#include <opencv2opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

vector<Rect> rects;
bool bDraw;
Rect r;
Point base;

Mat3b img;
Mat3b layer;
Mat3b working;


void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
    if ( event == EVENT_LBUTTONDOWN )
    {
        cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;    

        // Init your rect
        base.x = x;
        base.y = y;
        r.x = x;
        r.y = y;
        r.width = 0;
        r.height = 0;
        bDraw = true;
    }        
    else if ( event == EVENT_MOUSEMOVE )
    {
        cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;

        // If drawing, update rect width and height
        if(!bDraw) return;

        int dx = abs(r.x - x);
        int dy = abs(r.y - y);

        if(x < base.x) {
            r.x = x;
            r.width = abs(x - base.x);
        } else {
            r.width = dx;
        }

        if(y < base.y) {
            r.y = y;
            r.height = abs(y - base.y);
        } else {
            r.height = dy;
        }

        // Refresh
        working = layer.clone();
        rectangle(working, r, Scalar(0,255,0));
        imshow("My Window", working);
    }
    else if ( event == EVENT_LBUTTONUP)
    {
        cout << "Left button released" << endl;

        // Save rect, draw it on layer
        rects.push_back(r);
        rectangle(layer, r, Scalar(0,255,255));

        r = Rect(); 
        bDraw = false;

        // Refresh
        working = layer.clone();
        rectangle(working, r, Scalar(0,255,0));
        imshow("My Window", working);
    }
}

int main(int argc, char** argv)
{
    bool bDraw = false;
    bool isDragging = false;

    // Read image from file 
    img = imread("path_to_image");

    // initialize your temp images
    layer = img.clone();
    working = img.clone();

    //if fail to read the image
    if( img.empty() ) 
    { 
        cout << "Error loading the image" << endl;
        return -1; 
    }

    //Create a window
    namedWindow("My Window", 1);

    //set the callback function for any mouse event
    setMouseCallback("My Window", CallBackFunc, NULL);

    //show the image
    imshow("My Window", working);

    // Wait until user presses 'q'
    while((waitKey(1) & 0xFF) != 'q');

    return 0;
}

这篇关于如何使用opencv获取位置并绘制矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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