I have python code which is detecting a color. Once the color is detected, I am finding the contours and drawing them. Below is the original image:
and below is the image with contours & bounding box on it:
As you can see there are lot of contours detected and thus there are multiple bounding box. Is there a way to merge these bounding box into one. Below is the code
import cv2
import imutils
import numpy as np
image = cv2.imread("L00001.png")
image = imutils.resize(image, width=800)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_bound = np.array([45, 150, 20])
upper_bound = np.array([75, 305, 255])
origMask = cv2.inRange(hsv, lower_bound, upper_bound)
contours, h = cv2.findContours(origMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
new = np.vstack(contours)
area = cv2.contourArea(c)
if area > 10:
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(image, (int(x), int(y)), (int(x + w), int(y + h)), (0, 0, 255), 2)
cv2.imshow("FRAME", image)
cv2.waitKey(0)
cv2.destroyAllWindows()


