1. 引言
通常我们会在图片上绘制矩形、圆形和其它图形,仅仅通过代码绘制非常简单,但是可能会面临使用鼠标在图片上动态绘制圆形的情况。鉴于网上的相关参考较少,且我在学习过程中也使用到,在前人文章分享的基础上,整理了一篇鼠标绘制圆形的文章。本文将给出算法的实现思路、代码实现和绘制结果,以供大家参考。
2. 实现思路
- 定义鼠标回调函数(在本文中为myMouseeventCircle()函数,注意函数各个形参的意义);
- 如果用单用左键绘制圆形的话,仅需识别鼠标的三种状态即可,即左键按下-左键移动-左键松开,这三个动作结束之后,即绘制好一个圆形;
- 首先按下左键,识别此时鼠标所在位置的坐标点为圆心O;
- 然后移动左键,此时鼠标位置与圆心O之间构成了圆形的半径;
- 最后松开左键,绘制出想要的圆形。
另外,绘制圆形非常简单,只需要调用opencv库中imgproc.hpp文件中声明的circle()函数即可,以下为文件中针对该函数的详细声明,一共有7个形参,具体每个形参的意义说得非常清楚,在此不再赘述。
/** @brief Draws a circle.
The function circle draws a simple or filled circle with a given center and radius.
@param img Image where the circle is drawn.
@param center Center of the circle, a point parameter.
@param radius Radius of the circle.
@param color Circle color.
@param thickness Thickness of the circle outline, if positive. Negative thickness means that a
filled circle is to be drawn.
@param lineType Type of the circle boundary. See the line description.
@param shift Number of fractional bits in the coordinates of the center and in the radius value.
*/
CV_EXPORTS_W void circle(InputOutputArray img, Point center, int radius,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
3. 代码实现
#include<opencv2/opencv.hpp>
#include <opencv.hpp>
using namespace cv;
using namespace std;
Point2i startP;
Point2i endP;
//声明并定义绘制圆形的回调函数
void myMouseeventCircle(int event, int x, int y, int flags, void* userdata) {
int Circle_Radious = 0; //圆形的半径
Mat dst = (*(Mat*)userdata).clone();
//判断左键按下,记录起始点坐标
if (event == EVENT_LBUTTONDOWN) {
startP.x = x;
startP.y = y;
}
//鼠标左键为按下状态,显示需要画的圆形
if (flags == EVENT_FLAG_LBUTTON) {
//在图像中画出圆形
Circle_Radious = sqrt(pow((x - startP.x), 2) + pow((y - startP.y), 2)); //计算圆形半径
circle(dst, startP, Circle_Radious, Scalar(255, 255, 0), 2, 4);
imshow("image show", dst);
}
//左键松开,在原图像画出圆形
if (event == EVENT_LBUTTONUP) {
endP.x = x;
endP.y = y;
Circle_Radious = sqrt(pow((endP.x - startP.x), 2) + pow((endP.y - startP.y), 2));
circle(*(Mat*)userdata, startP, Circle_Radious, Scalar(255, 255, 0), 2, 4);
imshow("image show", *(Mat*)userdata);
}
}
int main(int argn, void* argc[]){
Mat src = imread("你图片存放的位置", IMREAD_UNCHANGED); //这部分改成你自己图片存放的路径
namedWindow("image show", WINDOW_NORMAL);
imshow("image show", src);
setMouseCallback("image show", myMouseeventCircle, &src);
waitKey(0);
return 0;
}
4. 绘制结果
5. 总结
以上内容完整概述了使用鼠标在图片上动态绘制圆形的方法,如果有疑问或者点明不足之处,欢迎留言讨论!
参考文章
- 圆形绘制:https://blog.csdn.net/caomin1hao/article/details/81876836
- 鼠标事件:https://blog.csdn.net/HWWH520/article/details/125095125