久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用opencv獲取位置并繪制矩形?

How can I get the position and draw rectangle using opencv?(如何使用opencv獲取位置并繪制矩形?)
本文介紹了如何使用opencv獲取位置并繪制矩形?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想在圖片框中移動(dòng)和單擊鼠標(biāo)時(shí)獲得一個(gè)位置.我想在單擊鼠標(biāo)的時(shí)間和位置在圖像窗口中創(chuàng)建矩形.

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.

我有一個(gè)來自文檔的簡(jiǎn)單代碼

#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 = 上工作,但我想使用鼠標(biāo)點(diǎn)擊.我把代碼放在 GUI 上.它拋出以下錯(cuò)誤:

錯(cuò)誤 3 錯(cuò)誤 C3867:'ProjectFinal::MyForm::CallBackFunc':函數(shù)調(diào)用缺少參數(shù)列表;使用&ProjectFinal::MyForm::CallBackFunc"創(chuàng)建一個(gè)指向成員 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

錯(cuò)誤 6 錯(cuò)誤 C3867:'ProjectFinal::MyForm::CallBackFunc':函數(shù)調(diào)用缺少參數(shù)列表;使用&ProjectFinal::MyForm::CallBackFunc"創(chuàng)建一個(gè)指向成員 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:指向成員的指針對(duì)于托管類 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

推薦答案

所以您遇到了與您的問題無關(guān)的問題.

So you have a problem unrelated to your question.

但是,您可以僅使用 OpenCV highgui 工具來實(shí)現(xiàn)您的目標(biāo):

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;
}

這篇關(guān)于如何使用opencv獲取位置并繪制矩形?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 99久久精品免费看国产四区 | 欧美日韩在线一区二区三区 | 精品欧美一区二区久久久伦 | 亚洲福利| 精品久久久久久久久久久久久久 | 91观看 | 97日日碰人人模人人澡分享吧 | 中文字幕高清免费日韩视频在线 | 欧美精品中文字幕久久二区 | 精品日韩一区 | 91精品国产91久久久久福利 | 欧美精品一区二区三区在线播放 | 欧美中文视频 | 色偷偷888欧美精品久久久 | 中文在线一区 | 国产精品自产拍在线观看蜜 | 国产美女久久 | 午夜亚洲 | 毛片网站在线观看 | 91社影院在线观看 | 一级黄色片在线免费观看 | 日韩高清中文字幕 | 国产免费视频 | 亚洲日韩第一页 | 91九色在线观看 | 久久成人一区 | 欧美福利 | 国产精品国产三级国产aⅴ中文 | 久久精品中文字幕 | 中文字幕av免费 | 亚洲精品在线免费播放 | 国产97色 | 精品国产不卡一区二区三区 | 成人福利 | 久久久久久亚洲精品 | 99爱在线 | 日韩av免费在线电影 | 国产欧美日韩久久久 | 亚洲精品黄 | 欧美激情综合 | 久久久久久中文字幕 |