pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

我想在圖片框中移動和單擊鼠標時獲得一個位置.我想在單擊鼠標的時間和位置在圖像窗口中創(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.

我有一個來自文檔的簡單代碼

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

推薦答案

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

So you have a problem unrelated to your question.

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

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獲取位置并繪制矩形?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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 無法設置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 废气处理_废气处理设备_工业废气处理_江苏龙泰环保设备制造有限公司 | 短信通106短信接口验证码接口群发平台_国际短信接口验证码接口群发平台-速度网络有限公司 | 开云(中国)Kaiyun·官方网站 - 登录入口| 电缆桥架生产厂家_槽式/梯式_热镀锌线槽_广东东莞雷正电气 | 光栅尺_Magnescale探规_磁栅尺_笔式位移传感器_苏州德美达 | 碳钢法兰厂家,非标法兰,定制异型,法兰生产厂家-河北九瑞管道 | 筒瓦厂家-仿古瓦-寺庙-古建琉璃瓦-宜兴市古典园林建筑陶瓷厂有限公司 | 天坛家具官网 | 广州活动策划公司-15+年专业大型公关活动策划执行管理经验-睿阳广告 | 水质监测站_水质在线分析仪_水质自动监测系统_多参数水质在线监测仪_水质传感器-山东万象环境科技有限公司 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 盘装氧量分析仪-防爆壁挂氧化锆分析仪-安徽吉帆仪表有限公司 | 长春网站建设,五合一网站设计制作,免费优化推广-长春网站建设 | 紧急泄压人孔_防爆阻火器_阻火呼吸阀[河北宏泽石化] | LED显示屏_LED屏方案设计精准报价专业安装丨四川诺显科技 | 并离网逆变器_高频UPS电源定制_户用储能光伏逆变器厂家-深圳市索克新能源 | 蜂窝块状沸石分子筛-吸附脱硫分子筛-萍乡市捷龙环保科技有限公司 | 沥青灌缝机_路面灌缝机_道路灌缝机_沥青灌缝机厂家_济宁萨奥机械有限公司 | 中医中药治疗血小板减少-石家庄血液病肿瘤门诊部 | 聚丙烯酰胺PAM-聚合氯化铝PAC-絮凝剂-河南博旭环保科技有限公司 巨野电机维修-水泵维修-巨野县飞宇机电维修有限公司 | 氮化镓芯片-碳化硅二极管 - 华燊泰半导体 | 中视电广_短视频拍摄_短视频推广_短视频代运营_宣传片拍摄_影视广告制作_中视电广 | 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 | 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 营养师网,营养师考试时间,报名入口—网站首页| 苏州工作服定做-工作服定制-工作服厂家网站-尺品服饰科技(苏州)有限公司 | 铸铁平台,大理石平台专业生产厂家_河北-北重机械 | 安德建奇火花机-阿奇夏米尔慢走丝|高维|发那科-北京杰森柏汇 | 动力配电箱-不锈钢配电箱-高压开关柜-重庆宇轩机电设备有限公司 聚天冬氨酸,亚氨基二琥珀酸四钠,PASP,IDS - 远联化工 | 威海防火彩钢板,威海岩棉复合板,威海彩钢瓦-文登区九龙岩棉复合板厂 | 盐水蒸发器,水洗盐设备,冷凝结晶切片机,转鼓切片机,絮凝剂加药系统-无锡瑞司恩机械有限公司 | 北京京云律师事务所| 山东钢衬塑罐_管道_反应釜厂家-淄博富邦滚塑防腐设备科技有限公司 | 单级/双级旋片式真空泵厂家,2xz旋片真空泵-浙江台州求精真空泵有限公司 | 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 乳化沥青设备_改性沥青设备_沥青加温罐_德州市昊通路桥工程有限公司 | 旋振筛_不锈钢旋振筛_气旋筛_旋振筛厂家—新乡市大汉振动机械有限公司 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | 3d打印服务,3d打印汽车,三维扫描,硅胶复模,手板,快速模具,深圳市精速三维打印科技有限公司 |