如何使用opencv获取位置并绘制矩形
How can I get the position and draw rectangle using opencv?
我想在picturebox中移动并单击鼠标时获得一个位置我想在单击鼠标的时间和位置在图像窗口中创建矩形
我有一个来自文档的简单代码
#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\nugningz\documents\visual studio 2012\projects\ProjectFinal\ProjectFinal\MyForm.h 690 1 ProjectFinal 的指针
错误6错误C3867:"ProjectFinal::MyForm::CallBackFunc":函数调用缺少参数列表;使用'&ProjectFinal::MyForm::CallBackFunc'创建指向成员c:\users\nugningz\documents\visual studio 2012\projects\ProjectFinal\ProjectFinal\MyForm.h 690 1 ProjectFinal 的指针
7 IntelliSense:指向成员的指针对于托管类c:\Users\NungNingZ\Documents\Visual Studio 2012\Projects\ProjectFinal\ProjectFinal\MyForm.h 690 37 ProjectFinal 无效
所以您有一个与您的问题无关的问题。
然而,您只需使用OpenCV highgui工具即可实现您的目标:
#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-contrib
的tracking
模块中,有一个很好的selectROI
特性。
#include <opencv2/opencv.hpp>
// selectROI is part of tracking API
#include <opencv2/tracking.hpp>
using namespace std;
using namespace cv;
int main (int argc, char **arv)
{
// Read image
Mat im = imread("image.jpg");
// Select ROI
Rect2d r = selectROI(im, false); // false -> for creating rectangle from
// top-left to bottom-right
// Crop image
Mat imCrop = im(r);
// Display Cropped Image
imshow("Image", imCrop);
waitKey(0);
return 0;
}
使用鼠标选择ROI(感兴趣区域(,然后按SPACE
或ENTER
按钮。按c
按钮取消选择过程。
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 在不同位置绘制多次glbatch
- QGraphicsScene项目在两次(x2)位置绘制
- SFML 将形状绘制到屏幕上的多个位置
- SFML参考窗口在绝对位置绘制
- 如何在 MFC 中围绕当前光标的位置绘制一个矩形
- (C++)在圆内夹紧 2D 位置(使用中点圆算法绘制)
- C++绘制矩形位置
- C++ SDL 根据鼠标位置绘制动态矩形
- 在3D世界中的显示器上固定位置绘制2D文本-OpenGL
- 如何将顶点位置传递给顶点着色器.我写了一个着色器,它不会在屏幕上绘制任何内容
- TMX问题,在正确位置绘制瓷砖(c++)
- 未在指定位置绘制的精灵
- OpenGL 形状仅在初始位置为 (0, 0, 0) 时绘制
- C++:如何在命令提示符的特定位置绘制ASCII字符
- 如何让OpenGL在我想要的位置绘制角色
- 绘制多个网格到不同的位置(DirectX 12)
- 如何使用opencv获取位置并绘制矩形
- OpenGL如何知道它应该基于GL_ARRAY_BUFFER绘制顶点到什么位置
- 无法在屏幕上的所需位置绘制矩形