如何使用OpenCV绘制圆角矩形(带有圆角的矩形)? [英] How to draw a rounded rectangle (rectangle with rounded corners) with OpenCV?
问题描述
如何在OpenCV中绘制带有圆角的矩形?我知道可以简单地将函数ellipse()和line()放在一起绘制它.我只是想知道是否有人曾经做过并将其置于适当的功能中以便可以使用?理想情况下,拐角半径应在参数中进行校准.
How can I draw a rectangle with rounded corners in OpenCV? I know that the functions ellipse() and line() can be simply put together to draw it. I just wonder if someone has done it before and has put it in a proper function so I can use it? Ideally the corner radius is to calibrate in a parameter.
我为此进行了大量搜索,但似乎以前没有人遇到过这个问题.如果没有人提供我这样的功能,我可能会在几天后在这里发布自己的解决方案.
I searched a lot for that, but it seems no one had that problem before. If no one has such I function I will probably post my own solution here in a few days.
推荐答案
我意识到,这比我想象的要容易得多.这是我的功能.我希望这对某人有帮助.
I realized, this is much easier that I thought. Here is my function. I hope it is helpful for someone.
/**
* Draws a rectangle with rounded corners, the parameters are the same as in the OpenCV function @see rectangle();
* @param cornerRadius A positive int value defining the radius of the round corners.
* @author K
*/
void rounded_rectangle( Mat& src, Point topLeft, Point bottomRight, const Scalar lineColor, const int thickness, const int lineType , const int cornerRadius)
{
/* corners:
* p1 - p2
* | |
* p4 - p3
*/
Point p1 = topLeft;
Point p2 = Point (bottomRight.x, topLeft.y);
Point p3 = bottomRight;
Point p4 = Point (topLeft.x, bottomRight.y);
// draw straight lines
line(src, Point (p1.x+cornerRadius,p1.y), Point (p2.x-cornerRadius,p2.y), lineColor, thickness, lineType);
line(src, Point (p2.x,p2.y+cornerRadius), Point (p3.x,p3.y-cornerRadius), lineColor, thickness, lineType);
line(src, Point (p4.x+cornerRadius,p4.y), Point (p3.x-cornerRadius,p3.y), lineColor, thickness, lineType);
line(src, Point (p1.x,p1.y+cornerRadius), Point (p4.x,p4.y-cornerRadius), lineColor, thickness, lineType);
// draw arcs
ellipse( src, p1+Point(cornerRadius, cornerRadius), Size( cornerRadius, cornerRadius ), 180.0, 0, 90, lineColor, thickness, lineType );
ellipse( src, p2+Point(-cornerRadius, cornerRadius), Size( cornerRadius, cornerRadius ), 270.0, 0, 90, lineColor, thickness, lineType );
ellipse( src, p3+Point(-cornerRadius, -cornerRadius), Size( cornerRadius, cornerRadius ), 0.0, 0, 90, lineColor, thickness, lineType );
ellipse( src, p4+Point(cornerRadius, -cornerRadius), Size( cornerRadius, cornerRadius ), 90.0, 0, 90, lineColor, thickness, lineType );
}
这篇关于如何使用OpenCV绘制圆角矩形(带有圆角的矩形)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!