Learn Before
  • Advanced Concepts

Object Detection (Template Matching)

Basic Template matching involves having an identical object file and then searching for the same pattern to appear in the base image. There are many methods to do template matching

methods = [cv2.TM_CCOEFF, cv2.TMCCOEFF_NORMED, cv2.TM_CCORR, cv2.TM_CCORR_NORMED, cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]

OpenCV recommends that you utilize all the methods and take the best matching algorithm as your final approach.

img = cv2.imread('path', 0) template = cv2.imread('path'. 0) height, width = template.shape #methods list for method in methods: img2 = img.copy() result = cv2.matchTemplate(img2, template, method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) if method in [cv22.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: location = min_loc else: location = max_loc bottom_right = (location[0] + width, location[1] + height) cv2.rectangle(img2, location, bottom_right, 255, 5) cv2.imshow("Match", img2) cv2.waitKey(0) cv2.destroyAllWindows()

What this will do is to move around the template around the base image and return how good of a match it finds in each area. Results will return, (W_base - W_temp + 1, H_base - H_temp + 1) size array.

With the location information, we can draw a shape on the coordinate location. SQDIFF methods use min_loc. All other methods use max_loc.

0

1

4 years ago

Tags

Python Programming Language

Data Science

Related
  • Color Extraction

  • Corner Detection

  • Object Detection (Template Matching)

  • Face and Eye Detection

  • Contour Maps

Learn After
  • Results Array