Finding objects via template matching
Finding an object in the image isn't a simple task, due to various representations the same instance may look dramatically different, and at first sight, some complicated computer vision algorithms are required. But, if we limit the issue, the task may be successfully solved by relatively simple methods. In this recipe, we consider the methods for finding objects on the image which correspond to some of the template.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV 3.x Python API package.
How to do it...
Perform the following steps:
- Import the modules:
import cv2 import numpy as np
- Load image and define mouse callback function for selecting image ROI. What is inside of the rectangle that is drawn will be our template for matching:
img = cv2.imread('../data/Lena.png', cv2.IMREAD_COLOR) show_img = np.copy(img) mouse_pressed = False y = x = w = h = 0 def mouse_callback(event, _x, _y, flags, param): global show_img, x, y, w...