gpt4 book ai didi

Python-Opencv :how to draw not complete rectangle with four corners on image

转载 作者:太空宇宙 更新时间:2023-11-03 21:32:06 25 4
gpt4 key购买 nike

在网上搜索后,我找不到像 this 中那样绘制边界框的方法。在 Python 中使用 OpenCV 的图像。它有两个特征,第一个是彼此不连接的四个角,第二个是透明边界框。

我知道我应该使用多边形,但我不能在这一点上更进一步。

最佳答案

以下函数在感兴趣区域周围绘制一个不完整的矩形。我为提供的每个点使用了两次 cv2.line()。此外,我还利用 cv2.circle() 标记了 4 个点。

只有一个约束。您必须按以下顺序提供 4 个点:左上角、左下角、右上角、右下角

还有一个选项可以改变您要绘制的线的长度 line_length

代码:

def draw_border(img, point1, point2, point3, point4, line_length):

x1, y1 = point1
x2, y2 = point2
x3, y3 = point3
x4, y4 = point4

cv2.circle(img, (x1, y1), 3, (255, 0, 255), -1) #-- top_left
cv2.circle(img, (x2, y2), 3, (255, 0, 255), -1) #-- bottom-left
cv2.circle(img, (x3, y3), 3, (255, 0, 255), -1) #-- top-right
cv2.circle(img, (x4, y4), 3, (255, 0, 255), -1) #-- bottom-right

cv2.line(img, (x1, y1), (x1 , y1 + line_length), (0, 255, 0), 2) #-- top-left
cv2.line(img, (x1, y1), (x1 + line_length , y1), (0, 255, 0), 2)

cv2.line(img, (x2, y2), (x2 , y2 - line_length), (0, 255, 0), 2) #-- bottom-left
cv2.line(img, (x2, y2), (x2 + line_length , y2), (0, 255, 0), 2)

cv2.line(img, (x3, y3), (x3 - line_length, y3), (0, 255, 0), 2) #-- top-right
cv2.line(img, (x3, y3), (x3, y3 + line_length), (0, 255, 0), 2)

cv2.line(img, (x4, y4), (x4 , y4 - line_length), (0, 255, 0), 2) #-- bottom-right
cv2.line(img, (x4, y4), (x4 - line_length , y4), (0, 255, 0), 2)

return img

line_length = 15

img = np.zeros((512,512,3), np.uint8)
cv2.imshow('img', img)

point1, point2, point3, point4 = (280,330), (280,390), (340,330), (340,390)
fin_img = draw_border(img, point1, point2, point3, point4, line_length)

cv2.imshow('fin_img', fin_img)

cv2.waitKey()
cv2.destroyAllWindows()

结果:

enter image description here

关于Python-Opencv :how to draw not complete rectangle with four corners on image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50548556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com