'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.

enter image description here

I want to get the area between these two horizontal line bbox and make it as a rectangle bounding box?

I'd appreciate any help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source