Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Image Matching (Template Matching)

Template Matching is a method in image processing for searching the location of a template or a target image.

Outline

  • Template Matching
  • Template Matching for Multi-Objects

Template Matching

- File name: Image_Matching.py 
- Input image: brain.jpg 
- Input target: target.jpg
- Command Line: python Image_Matching.py -i brain.jpg -t target.jpg
  • Main Function: res = cv2.matchTemplate(src_img, target, method)
Method:
   * cv2.TM_CCOEFF            * cv2.TM_CCORR_NORMED
   * cv2.TM_CCOEFF_NORMED     * cv2.TM_SQDIFF
   * cv2.TM_CCORR             * cv2.TM_SQDIFF_NORMED
NOTE: use function cv2.minMaxLoc() to calculate the "rectangle" of the target.
  - min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    * If method use "cv2.TM_SQDIFF", "cv2.TM_SQDIFF_NORMED", top_left = min_loc.
      Else, top_left = max_loc
    
  - cv2.rectangle(src_img, top_left, bottom_right, color, thickness)
    * top_left = min_loc or max_loc (depend on the method)
    * bottom_right = (top_left[0] + width, top_left[1] + height)

Template Matching for Multi-objects

- File name: Image_Matching.py 
- Input image: cell.jpg
- Input target: cell_target.jpg
- Command Line: python Image_Matching.py -i cell.jpg -t cell_target.jpg
Set a threshold for the return value by using numpy.where() for cv2.matchTemplate(), then 
draw all the region using cv2.rectangle():

* location = np.where( res >= threshold )

Useful link:

Code

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Reference & Acknowledgments