Я новичок в openCV и пытаюсь обнаружить флажки, помеченные галочкой, и заменить их на «#», чтобы я мог лучше их распознать. Однако я сталкиваюсь с парой проблем. а) Когда я определяю контуры, он не обнаруживает флажки, а также обнаруживает дополнительные элементы. б) Изображение обрезается и отображается неправильно.
Код ниже:
# Replace mydir with the directory you want
path = "C:/path to file/*.jpg"
import glob, os, errno
for file in glob.glob(path):
original_image = cv2.imread(file)
#original_image = cv2.resize(original_image,(2140,2140))
gray = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 120, 255, 1)
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
checkbox_contours = []
threshold_max_area = 3000
threshold_min_area = 200
contour_image = edged.copy()
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.035 * peri, True)
(x, y, w, h) = cv2.boundingRect(approx)
aspect_ratio = w / float(h)
area = cv2.contourArea(c)
if area < threshold_max_area and area > threshold_min_area and (aspect_ratio >= 0.9 and aspect_ratio <= 1.1):
cv2.drawContours(original_image,[c], 0, (0,255,0), 3)
checkbox_contours.append(c)
print('checkbox_contours', len(checkbox_contours))
#cv2.imshow("checkboxes", original_image)
#cv2.resize(original_image, (960, 540))
cv2.imshow("checkboxes", original_image)
cv2.waitKey(0)
Исходное изображение -