based on the code from @author K. make the function have ability to draw filled rounded rectangle if pass -1 as thickness
python version
def DrawRoundedRectangle(img, topLeft, bottomRight, radius=1, color=255, thickness=1, line_type=cv.LINE_AA):
min_half = int(min((bottomRight[0] - topLeft[0]), (bottomRight[1] - topLeft[1])) * 0.5)
radius = min(radius, min_half)
# /* corners:
# * p1 - p2
# * | |
# * p4 - p3
# */
p1 = topLeft
p2 = (bottomRight[0], topLeft[1])
p3 = bottomRight
p4 = (topLeft[0], bottomRight[1])
if(thickness < 0):
# // draw rectangle
cv.rectangle(img, (p1[0] + radius, p1[1]), (p3[0] - radius, p3[1]), color, thickness, line_type)
cv.rectangle(img, (p1[0], p1[1] + radius), (p3[0], p3[1] - radius), color, thickness, line_type)
else:
# // draw straight lines
cv.line(img, (p1[0] + radius, p1[1]), (p2[0] - radius, p2[1]), color, thickness, line_type);
cv.line(img, (p2[0], p2[1] + radius), (p3[0], p3[1] - radius), color, thickness, line_type);
cv.line(img, (p4[0] + radius, p4[1]), (p3[0]-radius, p3[1]), color, thickness, line_type);
cv.line(img, (p1[0], p1[1] + radius), (p4[0], p4[1] - radius), color, thickness, line_type);
# // draw arcs
if(radius > 0):
cv.ellipse( img, (p1[0] + radius, p1[1] + radius), ( radius, radius ), 180.0, 0, 90, color, thickness, line_type );
cv.ellipse( img, (p2[0] - radius, p2[1] + radius), ( radius, radius ), 270.0, 0, 90, color, thickness, line_type );
cv.ellipse( img, (p3[0] - radius, p3[1] - radius), ( radius, radius ), 0.0, 0, 90, color, thickness, line_type );
cv.ellipse( img, (p4[0] + radius, p4[1] - radius), ( radius, radius ), 90.0, 0, 90, color, thickness, line_type );
javascript version
function DrawRoundedRectangle(img, topLeft, bottomRight, radius=1, color=255, thickness=1, line_type=cv.LINE_AA){
let min_half = Math.floor(Math.min((bottomRight.x - topLeft.x), (bottomRight.y - topLeft.y)) * 0.5)
radius = Math.min(radius, min_half)
/* corners:
# * p1 - p2
# * | |
# * p4 - p3
# */
let p1 = topLeft
let p2 = new cv.Point(bottomRight.x, topLeft.y)
let p3 = bottomRight
let p4 = new cv.Point(topLeft.x, bottomRight.y)
if(thickness < 0){
// draw rectangle
cv.rectangle(img, new cv.Point(p1.x + radius, p1.y), new cv.Point(p3.x - radius, p3.y), color, thickness, line_type)
cv.rectangle(img, new cv.Point(p1.x, p1.y + radius), new cv.Point(p3.x, p3.y - radius), color, thickness, line_type)
}
else{
// draw straight lines
cv.line(img, new cv.Point(p1.x + radius, p1.y), new cv.Point(p2.x - radius, p2.y), color, thickness, line_type);
cv.line(img, new cv.Point(p2.x, p2.y + radius), new cv.Point(p3.x, p3.y - radius), color, thickness, line_type);
cv.line(img, new cv.Point(p4.x + radius, p4.y), new cv.Point(p3.x-radius, p3.y), color, thickness, line_type);
cv.line(img, new cv.Point(p1.x, p1.y + radius), new cv.Point(p4.x, p4.y - radius), color, thickness, line_type);
}
// draw arcs
if(radius > 0){
cv.ellipse( img, new cv.Point(p1.x + radius, p1.y + radius), new cv.Size( radius, radius ), 180.0, 0, 90, color, thickness, line_type );
cv.ellipse( img, new cv.Point(p2.x - radius, p2.y + radius), new cv.Size( radius, radius ), 270.0, 0, 90, color, thickness, line_type );
cv.ellipse( img, new cv.Point(p3.x - radius, p3.y - radius), new cv.Size( radius, radius ), 0.0, 0, 90, color, thickness, line_type );
cv.ellipse( img, new cv.Point(p4.x + radius, p4.y - radius), new cv.Size( radius, radius ), 90.0, 0, 90, color, thickness, line_type );
}
}
cornerRadius
be calculated automatically, because when you draw a small size Rectangle, then it will look like a circle, so thecornerRadius
can be calculated like thiscornerRadius = (rect.width+rect.height)*0.1
– Naphthalene