I am using OpenCV to detect rectangles in an image based on their color. The code works well for non-overlapping rectangles, but it fails to detect overlapping rectangles.
Here’s code I am using
<code>image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_blue = np.array([110, 50, 50])
upper_blue = np.array([130, 255, 255])
mask = cv2.inRange(image_hsv, lower_blue, upper_blue)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for index, contour in enumerate(contours):
epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
if len(approx) == 4:
(x, y, w, h) = cv2.boundingRect(approx)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
</code>
<code>image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_blue = np.array([110, 50, 50])
upper_blue = np.array([130, 255, 255])
mask = cv2.inRange(image_hsv, lower_blue, upper_blue)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for index, contour in enumerate(contours):
epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
if len(approx) == 4:
(x, y, w, h) = cv2.boundingRect(approx)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
</code>
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_blue = np.array([110, 50, 50])
upper_blue = np.array([130, 255, 255])
mask = cv2.inRange(image_hsv, lower_blue, upper_blue)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for index, contour in enumerate(contours):
epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
if len(approx) == 4:
(x, y, w, h) = cv2.boundingRect(approx)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
Overlapping rectangles are either not detected at all or are detected as a single merged shape. This is problematic because I need to individually detect and crop each rectangle, even if they overlap.
I’ve Tried:
- Adjusting the Contour Approximation: I tried tweaking the epsilon parameter in cv2.approxPolyDP to various values, but it didn’t help in detecting overlapping rectangles.
- Changing the Mask Range: I experimented with different HSV ranges to ensure the mask correctly covers the rectangles, but this didn’t resolve the overlapping issue
- Contour Retrieval Mode: I also tried different contour retrieval modes (like cv2.RETR_TREE and cv2.RETR_LIST), but it didn’t improve the detection of overlapping rectangles.
For eg. Sample Input Image
Outputs I get
Not getting the overlapping one’s.