Hello! Mastering OpenCV, I encountered a problem: I can't find any of these boxes to them then cut. Tell me, please, what filters and logic to use?
#!/usr/bin/env python
import cv2
import os
img_path = os.path.join('img', '1.jpg')
image = cv2.imread(img_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200)
cv2.imshow('gray', gray)
cv2.waitKey(0)
cv2.imshow('edged', edged)
cv2.waitKey(0)
(_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
if len(approx) == 4:
cv2.drawContours(image, [approx], -1, (0, 255, 0), 3)
cv2.imshow('result', image)
cv2.waitKey(0)
This example finds a lot of garbage, and all rectangles (not just those with background)
EDIT: OpenСV duplicates rectangle contours. How can i cut off duplicates?
