通过opencv 在图像上画任意形状的封闭区域和画两条直线的方法,代码如下:
参考自https://ask.csdn.net/questions/247228?locationNum=9&fps=1,原来代码只提供了画封闭区域的代码,但是有点bug,在首次单击鼠标画线时会闪出一条从(0,0)到鼠标位置的线段,以下代码已修改了此bug。参考源代码,这里写了一个可以画多调直线的鼠标响应函数。
/*************************************************
* 使用说明: *
*MyMouseCallbackForEnclosedArea()画封闭区域的函数*
* 左键单击画点 *
* 右键自动闭合曲线 *
* 右键双击上色 *
***************************************************
*MyMouseCallbackForLine()画多条直线 *
* 左键单击画点,单击起点、单击终点 *
**************************************************/
#include <string>
#include <list>
#include <vector>
#include <cmath>
#include "include/opencv2/core/core.hpp"
#include "include/opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
bool bFirstLButtenDown = true;//wyj
bool bDrawLineEnding = false;//wyj
void MyMouseCallbackForEnclosedArea (int event, int x, int y, int flags, void *param);
void MyMouseCallbackForLine(int event, int x, int y, int flags, void *param);
CvPoint point_1, point_2, point_3;
int ymin = 0;
i