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

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

How can I get the position and draw rectangle using opencv?(如何使用opencv獲取位置并繪制矩形?)
本文介紹了如何使用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獲取位置并繪制矩形?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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++ 中旋轉圖像而不使用 OpenCV 函數)
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 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 亚洲成人激情在线观看 | 日韩精品一 | 在线观看涩涩视频 | 国产黄色大片 | 91免费版在线观看 | 精品视频久久久 | 欧洲尺码日本国产精品 | 精国产品一区二区三区四季综 | 亚洲精品在线免费播放 | 女生羞羞网站 | 久久天堂网 | 国产1区在线 | 亚洲欧美日韩在线 | 国产人免费人成免费视频 | 久久成人国产精品 | 精品久久久久久亚洲精品 | 涩涩视频在线观看免费 | 美女爽到呻吟久久久久 | 天天澡天天狠天天天做 | 亚洲一区久久久 | 久久久精品视频免费看 | 欧美视频网 | 夜夜草导航 | 99久久精品免费看国产四区 | 欧美日韩成人在线观看 | www.youjizz.com日韩 | www.4虎影院| 欧美三级在线 | 久久久久无码国产精品一区 | 国产激情毛片 | 国产在线精品一区二区三区 | 日本三级黄视频 | 精品真实国产乱文在线 | 日韩在线观看一区 | 米奇7777狠狠狠狠视频 | 午夜免费观看网站 | 日韩在线观看网站 | 欧美精品一区二区三区四区 在线 | 在线资源视频 | 亚洲福利一区 | 日本免费在线看 |