Setti7
Setti7

Reputation: 149

Finding the top and bottom y coordinate of a opencv rectangle

I want to merge these 2 rectangles into one, like this. The easiest way I can think to do this is getting the top y coordinate of the top rectangle and the bottom y coordinates of the bottom one and use them in cv2.rectangle(), but I am having trouble getting both these points because of a for loop.

Here is the code:

#Finding contours (always finds those 2 retangles + some noise):
_, conts, hierarchy = cv2.findContours(img_green, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for cnt in conts:
    area = cv2.contourArea(cnt)

    #filter all the noise
    if area > 20:

        x1, y1, w, h = cv2.boundingRect(cnt)
        x2 = x1 + w                           # (x1, y1) = top-left vertex
        y2 = y1 + h                           # (x2, y2) = bottom-right vertex
        cv2.rectangle(green_bar_win, (x1, y1), (x2, y2), (255,0,0), 2)
        print("x1:", x1, " y1:", y1, " x2:", x2, " y2:", y2)

Here is the print result (it prints the x,y coordinates of the top-left and bottom-right rectangle points for both rectangles in diferent itinerations of the loop):

x1: 60  y1: 217  x2: 83  y2: 288
x1: 60  y1: 169  x2: 83  y2: 216
x1: 60  y1: 217  x2: 83  y2: 288
x1: 60  y1: 169  x2: 83  y2: 216...

Thanks.

EDIT: my solution

Upvotes: 0

Views: 4324

Answers (2)

I.Newton
I.Newton

Reputation: 1783

Instead of going with the coordinates, you can do it in a simpler way by using 'or'.

If both of the rectangles are of opencv type rect, you can simply use,

result = rect1 | rect2;

That will give you a rectangle enclosing both. Similarly, you can do other operations like

result = rect1 & rect2;

That'll give you the internsection of both the rectangles. Reference.

Upvotes: 1

janu777
janu777

Reputation: 1978

There are many ways to achieve the result, some of it can be the following:

1) From the image, it is very clear that both the rectangles are very close to each other. So the contours must also be very close. Therefore before finding the contours, perform morphological transformations to the image. Mostly dilation will help. Find the working of this operation in the link.

dilation = cv2.dilate(img,kernel,iterations = 1)

2) Else if you only want to combine the rectangles, you can follow up with this link

Hope this helps!

Upvotes: 0

Related Questions