[英]How to convert two contours of horizontal line as bbox in a image to get a rectangle?
Lets' say I have two countours,假设我有两个计数,
c = (array([[[ 1, 342]],
[[ 1, 347]],
[[1705, 347]],
[[1705, 342]]], dtype=int32),
array([[[ 106, 468]],
[[ 106, 472]],
[[ 107, 473]],
[[1703, 473]],
[[1703, 468]]], dtype=int32))
I am fetching bounding rectangle from contours,我正在从轮廓中获取边界矩形,
x1,y1,w1,h1 = cv2.boundingRect(c[0])
x2,y2,w2,h2 = cv2.boundingRect(c[1])
# print(x1, y1, w1, h1)
# (1, 342, 1705, 6)
# print(x2, y2, w2, h2)
# (106, 468, 1598, 6)
Basically the (1, 342, 1705, 6)
is a bounding box around a horizontal line in a image, and (106, 468, 1598, 6)
is a bounding box of another horizontal line in a image.基本上
(1, 342, 1705, 6)
是图像中水平线周围的边界框, (106, 468, 1598, 6)
是图像中另一水平线的边界框。
I want to get the area between these two horizontal line bbox and make it as a rectangle bounding box?我想得到这两个水平线 bbox 之间的区域并将其作为矩形边界框?
I'd appreciate any help我会很感激任何帮助
there's a simple solution: concatenate the contours to a single one, then get the bounding box again (this will also fix the missing left part of the 2nd contour):有一个简单的解决方案:将轮廓连接到一个轮廓,然后再次获取边界框(这也将修复第二个轮廓丢失的左侧部分):
d = np.concatenate((c[0],c[1]))
x3,y3,w3,h3 = cv2.boundingRect(d)
print(x3,y3,w3,h3,d)
(1, 342, 1705, 132, array([[[ 1, 342]],
[[ 1, 347]],
[[1705, 347]],
[[1705, 342]],
[[ 106, 468]],
[[ 106, 472]],
[[ 107, 473]],
[[1703, 473]],
[[1703, 468]]], dtype=int32)
if you really wanted the space between the boxes (not including them), you'll have to offset it like:如果你真的想要盒子之间的空间(不包括它们),你必须像这样抵消它:
y3 += h1
h3 -= (h1+h2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.