0

enter image description here

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.

template image in the left

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.

  • Look for [Cascade Classifier](https://docs.opencv.org/3.4/db/d28/tutorial_cascade_classifier.html). – Costantino Grana Jun 27 '23 at 16:41
  • @CostantinoGrana thanks. I'll refer it. Is it possible to detect the patterns without using a classifier? I'm working on a uni project and we have been restricted not to use classification models or deep learning. – Sahan Kodituwakku Jun 27 '23 at 17:02
  • Template matching does not work when the image are different shapes (dimensions, rotations distortions). It only works for translation (different positions). – fmw42 Jun 27 '23 at 17:46
  • Ok. The restriction doesn't make any sense, since a threshold is a classification model. Simple, but still a classification model. Anyway, if you want help, take the time to [edit](https://stackoverflow.com/posts/76566858/edit) your question to show some effort ("enter image description here" is sloppy). Provide a link to multiple images to allow us to understand the problem variability. Then try to get some real output. The output you are showing is the original image, which makes no sense. – Costantino Grana Jun 27 '23 at 21:55
  • You can start with simply getting all contours that have a nearly quadratic aspect ratio. After that you can think about filtering out false positives. – Markus Jun 28 '23 at 05:40
  • talk to your advisor. there are open questions. the pictures you showed require some level of machine learning, even if it's 20 year old technology (haar cascades). 20-year-old technology will have trouble solving this. you've been given an impossible task. get your advisor to clarify. – Christoph Rackwitz Jun 28 '23 at 09:01

0 Answers0