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