I want to detect the crossed out boxes and mark them using bounding Rectangles. So to simplify the problem, I extracted few images and selected 1 image as the template image.
I want to use opencv and python to detect the test images using a suitable method. The template image and the test images are given below. I want to do the detection purely using opencv and python, without using any deep learning or ai classification.
I tried using matchTemplate() but it gives the exact image match.
import cv2
import numpy as np
import matplotlib.pyplot as plt
"""
This script finds a single match for the template image
and saves the match.
"""
# Load the image
_img = cv2.imread('data/wet/6.jpg', cv2.IMREAD_GRAYSCALE)
_temp = _img.copy()
# Load the template image
_template = cv2.imread('data/wet/9.jpg', cv2.IMREAD_GRAYSCALE)
w, h = _template.shape[::-1]
# All the 6 methods for comparison in a list
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
matched = []
for meth in methods:
_img = _temp.copy()
method = eval(meth)
# Apply template Matching
res = cv2.matchTemplate(_img,_template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(_img,top_left, bottom_right, 255, 2)
matched.append(_img)
view_img = matched[1]
plt.imshow(view_img)
plt.show()
cv2.imwrite('data/wet/10.jpg', view_img)
here's the output.
output image from matchTemplate
I also tried using canny edge detection as well, but still the result was same.