如何在opencv中的2点之间绘制点和线?希望通过使用鼠标绘制点单击图像并连接它们。opencv绘制点和2点之间的线
3
A
回答
2
参见Drawing Functions具体CV ::线
1
看称为drawing.py和inpaint.py或它们的C++当量的包含样本。 inpaint.py中的Sketcher类可能是您正在查找的内容。
0
在opencv中使用鼠标功能是一件很麻烦的事情。如果有帮助,这里是我在视频上绘制矩形的代码示例。它的功能完备,所以你可以剪切和粘贴整个事情。你应该能够把它分开,看看鼠标处理如何工作。
#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#define SSTR(x) dynamic_cast< std::ostringstream & >((std::ostringstream() << std::dec << x)).str()
using namespace std;
using namespace cv;
Rect box;
bool drawing_box = false;
struct mousecallbackstruct{
Mat* src;
Mat* overlay;
string windowname;
};
Mat srcoverlay,smallsrcoverlay;
void onMouse(int event, int x, int y, int flags, void* param) //it seems the only way to use this is by keeping different globals for different windows - meaning you have to set up all thise ahead of time, and keep track of it and not mix/match windows/frames!! horrible design here.
{
cout << event;
mousecallbackstruct mousestruct;
mousestruct = *((mousecallbackstruct*)param);
Mat* srcp = mousestruct.src;
Mat* overlayp = mousestruct.overlay;
Mat src = *srcp;
Mat overlay = *overlayp;
if(!src.data){
cout << "your void * cast didn't work :(\n";
return;
}
switch(event){
case CV_EVENT_MOUSEMOVE:
if(drawing_box){
box.width = x-box.x;
box.height = y-box.y;
}
break;
case CV_EVENT_LBUTTONDOWN: //start drawing
drawing_box = true;
box = cvRect(x, y, 0, 0);
break;
case CV_EVENT_LBUTTONDBLCLK: //double click to clear
drawing_box = false;
overlay.setTo(cv::Scalar::all(0)); //clear it
break;
case CV_EVENT_LBUTTONUP: //draw what we created with Lbuttondown
drawing_box = false;
if(box.width < 0){
box.x += box.width;
box.width *= -1;
}
if(box.height < 0){
box.y += box.height;
box.height *= -1;
}
rectangle(overlay, Point(box.x, box.y), Point(box.x+box.width,box.y+box.height),CV_RGB(100,200,100),4); //draw rectangle. You can change this to line or circle or whatever. Maybe with the Right mouse button.
break;
}
}
void iimshow(mousecallbackstruct* mystructp){ //this is where we add the text/drawing created in the mouse handler to the actual image (since mouse handler events do not line up with the drawing events)
mousecallbackstruct mystruct = *mystructp; //custom struct made for the mouse callback - very handy for other functions too
Mat overlay, src;
Mat* srcp = mystruct.src;
Mat* overlayp = mystruct.overlay;
src = *srcp; // yeah, yeah, i use 9 lines where I could use 3, so sue me
overlay = *overlayp;
string name = mystruct.windowname;
Mat added,imageROI;
try{
//cout << "tch:" << overlay.rows << "," << src.rows << ";" << overlay.cols << "," << src.cols << ";" << src.channels() << "," << overlay.channels() <<"," << src.type() << "," << overlay.type() << "\n";
if(overlay.data && overlay.rows == src.rows && overlay.cols == src.cols && overlay.channels() == src.channels()){ //basic error checking
add(src,overlay,added);
}else{
//try to resize it
imageROI= overlay(Rect(0,0,src.cols,src.rows));
add(src,imageROI,added);
}
imshow(name,added);// the actual draw moment
}catch(...){ //if resize didn't work then this should catch it
cout << "Error. Mismatch:" << overlay.rows << "," << src.rows << ";" << overlay.cols << "," << src.cols << ";" << src.channels() << "," << overlay.channels() <<"," << src.type() << "," << overlay.type() << "\n";
imshow(name + "overlay",overlay);
imshow(name+"source",src);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) { // check if we succeeded
cout << "NO camera found \n";
return -1;
}
Mat src,smallsrc,overlay;
cap >> src; //grab 1 frame to build our preliminary Mats and overlays
srcoverlay.create(src.rows,src.cols,src.type()); //create overlays
smallsrcoverlay.create(src.rows,src.cols,src.type());
srcoverlay.setTo(cv::Scalar::all(0)); //clear it
smallsrcoverlay.setTo(cv::Scalar::all(0)); //clear it
namedWindow("smallsrc", CV_WINDOW_AUTOSIZE);
namedWindow("source", CV_WINDOW_AUTOSIZE); //these must be created early for the setmousecallback, AND you have to know what Mats will be using them and not switch them around :(
moveWindow("smallsrc",2000,100); //create a small original capture off to the side of screen
////////////// for each window/mat that uses a mouse handler, you must create one of these structures for it and pass it into the mouse handler
mousecallbackstruct srcmousestruct,smallsrcmousestruct; //these get passed into the mouse callback function. Hopefully they update their contents automatically for the callback? :(
srcmousestruct.overlay = &srcoverlay; //fill our custom struct
srcmousestruct.src = &src;
srcmousestruct.windowname = "source";
smallsrcmousestruct.overlay = &smallsrcoverlay; //the small window
smallsrcmousestruct.src = &smallsrc;
smallsrcmousestruct.windowname = "smallsrc";
setMouseCallback(smallsrcmousestruct.windowname, onMouse, (void*)&smallsrcmousestruct); //the actual 'set mouse callback' call
setMouseCallback(srcmousestruct.windowname, onMouse, (void*)&srcmousestruct);
for(;;){ //main loop
/// Load an image
cap >> src;
if(!src.data)
{ return -1; }
resize(src,smallsrc,Size(),.5,.5); //smaller scale window of original
overlay = *srcmousestruct.overlay;
src = *srcmousestruct.src;
iimshow(&srcmousestruct); //my imshow replacement. uses structs
iimshow(&smallsrcmousestruct);
if(waitKey(30) == 27) cin.get(); //esc pauses
}
cin.get();
return 0;
}
相关问题
- 1. Highcharts没有绘制点之间的线
- 2. 点之间绘制线在的UIImage
- 3. 两点之间绘制曲线
- 4. 使用Android 2.3绘制2个地理点之间的界线
- 5. pcolormesh不绘制点但是数据点之间的线条
- 6. 绘制置信区间,点和线
- 7. 查找2点之间的限制点
- 8. 在matplotlib中绘制预测和ground_truth点之间的一条线
- 9. 使用Canvas和jQuery绘制鼠标点击之间的直线
- 10. MapKit - 2针之间的绘制线
- 11. OpenCV绘制非匹配点
- 12. 绘制两点之间的路径
- 13. 绘制点之间的路径
- 14. C#/ java的拖曳点,并绘制他们之间的线路
- 15. 在地图中绘制2点之间的短路径
- 16. 如何在android中的两点之间绘制线
- 17. Xamarin.Forms.Maps - 如何绘制两点之间的路径 - 多段线
- 18. 使用LatLng的Arraylist在两点之间绘制多段线
- 19. 将变量绘制为点之间的直线斜率
- 20. 绘制两点之间的曲线在PHP
- 21. 如何更改标记,绘制点之间的直线MATLAB
- 22. Android - 绘制两个地理点之间的路线
- 23. 在android上的地点之间绘制平滑线
- 24. 沿两点之间的路线绘制商店标记
- 25. 需要帮助绘制点之间的线条
- 26. gnuplot中的绘制点和线条
- 27. 用A型框在2个点之间绘制一个圆柱
- 28. 在x轴2点之间绘制阴谋带 - Highcharts
- 29. 在两个3D点之间绘制一条弧线
- 30. 在地图上给定点之间绘制折线
...并注意,cvPoint只是CvPoint结构的构造函数,它不会绘制任何东西。绘制单个点(像素)您应该使用cvLine,并且两端的点数相等。 – 2011-12-29 16:26:05