ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – 如何使用opencv获取位置和绘制矩形?

2019-09-18 19:14:56  阅读:163  来源: 互联网

标签:opencv3-0 c opencv visual-studio-2012


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

我有一个简单的文档代码

#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上.它会引发以下错误:

Error 3 error C3867: ‘ProjectFinal::MyForm::CallBackFunc’: function call missing argument list; use ‘&ProjectFinal::MyForm::CallBackFunc’ to create a pointer to member c:\users\nungningz\documents\visual studio 2012\projects\projectfinal\projectfinal\MyForm.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\nungningz\documents\visual studio 2012\projects\projectfinal\projectfinal\MyForm.h 690 1 ProjectFinal

7 IntelliSense: a pointer-to-member is not valid for a managed class c:\Users\NungNingZ\Documents\Visual Studio 2012\Projects\ProjectFinal\ProjectFinal\MyForm.h 690 37 ProjectFinal

解决方法:

所以你有一个与你的问题无关的问题.

但是,您只需使用OpenCV highgui设施即可实现目标:

#include <opencv2\opencv.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;
}

标签:opencv3-0,c,opencv,visual-studio-2012
来源: https://codeday.me/bug/20190918/1811383.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有