如何在OpenCV中绘制带圆角的矩形?我知道函数ellipse()和line()可以简单地放在一起绘制它。我只是想知道是否有人之前已经做过并且已经把它放在一个合适的功能中所以我可以使用它?理想情况下,拐角半径是在参数中校准。
我搜索了很多,但似乎以前没有人遇到过这个问题。如果没有人具有这样的功能,我可能会在几天内发布我自己的解决方案。
答案 0 :(得分:13)
我意识到,这比我想象的容易得多。这是我的功能。我希望这对某人有帮助。
/**
* 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 );
}
答案 1 :(得分:4)
这是具有填充/未填充功能以及根据图片高度自动计算的corner_radius的python版本。
import cv2
import numpy as np
def rounded_rectangle(src, top_left, bottom_right, radius=1, color=255, thickness=1, line_type=cv2.LINE_AA):
# corners:
# p1 - p2
# | |
# p4 - p3
p1 = top_left
p2 = (bottom_right[1], top_left[1])
p3 = (bottom_right[1], bottom_right[0])
p4 = (top_left[0], bottom_right[0])
height = abs(bottom_right[0] - top_left[1])
if radius > 1:
radius = 1
corner_radius = int(radius * (height/2))
if thickness < 0:
#big rect
top_left_main_rect = (int(p1[0] + corner_radius), int(p1[1]))
bottom_right_main_rect = (int(p3[0] - corner_radius), int(p3[1]))
top_left_rect_left = (p1[0], p1[1] + corner_radius)
bottom_right_rect_left = (p4[0] + corner_radius, p4[1] - corner_radius)
top_left_rect_right = (p2[0] - corner_radius, p2[1] + corner_radius)
bottom_right_rect_right = (p3[0], p3[1] - corner_radius)
all_rects = [
[top_left_main_rect, bottom_right_main_rect],
[top_left_rect_left, bottom_right_rect_left],
[top_left_rect_right, bottom_right_rect_right]]
[cv2.rectangle(src, rect[0], rect[1], color, thickness) for rect in all_rects]
# draw straight lines
cv2.line(src, (p1[0] + corner_radius, p1[1]), (p2[0] - corner_radius, p2[1]), color, abs(thickness), line_type)
cv2.line(src, (p2[0], p2[1] + corner_radius), (p3[0], p3[1] - corner_radius), color, abs(thickness), line_type)
cv2.line(src, (p3[0] - corner_radius, p4[1]), (p4[0] + corner_radius, p3[1]), color, abs(thickness), line_type)
cv2.line(src, (p4[0], p4[1] - corner_radius), (p1[0], p1[1] + corner_radius), color, abs(thickness), line_type)
# draw arcs
cv2.ellipse(src, (p1[0] + corner_radius, p1[1] + corner_radius), (corner_radius, corner_radius), 180.0, 0, 90, color ,thickness, line_type)
cv2.ellipse(src, (p2[0] - corner_radius, p2[1] + corner_radius), (corner_radius, corner_radius), 270.0, 0, 90, color , thickness, line_type)
cv2.ellipse(src, (p3[0] - corner_radius, p3[1] - corner_radius), (corner_radius, corner_radius), 0.0, 0, 90, color , thickness, line_type)
cv2.ellipse(src, (p4[0] + corner_radius, p4[1] - corner_radius), (corner_radius, corner_radius), 90.0, 0, 90, color , thickness, line_type)
return src
用法:
top_left = (0, 0)
bottom_right = (500, 800)
color = (255, 255, 255)
image_size = (500, 800, 3)
img = np.zeros(image_size)
img = rounded_rectangle(img, top_left, bottom_right, color=color, radius=0.5, thickness=-1)
cv2.imshow('rounded_rect', img)
cv2.waitKey(0)
答案 2 :(得分:1)
这是一个Python实现(如果有人正在寻找一个):它绘制一个圆角(随机半径和线条粗细---如果你想要改变它)边框图像:
def addRoundedRectangleBorder(img):
height, width, channels = img.shape
border_radius = int(width * random.randint(1, 10)/100.0)
line_thickness = int(max(width, height) * random.randint(1, 3)/100.0)
edge_shift = int(line_thickness/2.0)
red = random.randint(230,255)
green = random.randint(230,255)
blue = random.randint(230,255)
color = (blue, green, red)
#draw lines
#top
cv2.line(img, (border_radius, edge_shift),
(width - border_radius, edge_shift), (blue, green, red), line_thickness)
#bottom
cv2.line(img, (border_radius, height-line_thickness),
(width - border_radius, height-line_thickness), (blue, green, red), line_thickness)
#left
cv2.line(img, (edge_shift, border_radius),
(edge_shift, height - border_radius), (blue, green, red), line_thickness)
#right
cv2.line(img, (width - line_thickness, border_radius),
(width - line_thickness, height - border_radius), (blue, green, red), line_thickness)
#corners
cv2.ellipse(img, (border_radius+ edge_shift, border_radius+edge_shift),
(border_radius, border_radius), 180, 0, 90, color, line_thickness)
cv2.ellipse(img, (width-(border_radius+line_thickness), border_radius),
(border_radius, border_radius), 270, 0, 90, color, line_thickness)
cv2.ellipse(img, (width-(border_radius+line_thickness), height-(border_radius + line_thickness)),
(border_radius, border_radius), 10, 0, 90, color, line_thickness)
cv2.ellipse(img, (border_radius+edge_shift, height-(border_radius + line_thickness)),
(border_radius, border_radius), 90, 0, 90, color, line_thickness)
return img