简体   繁体   English

如何将水平线的两个轮廓转换为图像中的 bbox 以获得矩形?

[英]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.

相关问题 如何使用 cv2.minAreaRect(cnt) 在多轮廓图像上获得唯一的最小区域矩形? - How to get the only min area rectangle on a multiple contours image with cv2.minAreaRect(cnt)? 如何使用 opencv 将轮廓作为图像获取 - How to get contours as image with opencv 获取矩形内圆的轮廓 - get contours of circles within a rectangle OpenCV Python:在图像中查找轮廓/边缘/矩形 - OpenCV Python: find contours/edges/rectangle in an image 获取图像的轮廓和点 - Get contours and points of an image 有没有办法在opencv python中获得两个独立轮廓的边界矩形的4个坐标点? - Is there any way to get 4 coordinate points of bounding rectangle of two separate contours in opencv python? 如何对轮廓进行分组并绘制单个边界矩形 - How to group contours and draw a single bounding rectangle 在 matplotlib 中使用 bbox_inches='tight' 时,如何获得以像素为单位的精确大小的图像? - How do I get an image w/ the exact size in pixels, when using bbox_inches='tight' in matplotlib? 扩张图像以消除间隙并获得原始轮廓大小的扩张轮廓 - Dilate image to remove gaps and get dilated contours in the original contours size 使用带有水平线的熊猫绘制矩形补丁 - Plot rectangle patches using pandas with horizontal line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM