Visualizing feature matches

In OpenCV, we can easily draw matches using cv2.drawMatches. Here, we create our own function for educational purposes as well as for ease of customization of function behavior:

def draw_good_matches(img1: np.ndarray,                      kp1: Sequence[cv2.KeyPoint],                      img2: np.ndarray,                      kp2: Sequence[cv2.KeyPoint],                      matches: Sequence[cv2.DMatch]) -> np.ndarray:

The function accepts two images, namely, in our case, the image of the object of interest and the current video frame. It also accepts keypoints from both images as well as the matches. It will draw the images next to each other on a single illustration image, illustrate the matches on the image, and return the image. The latter is achieved with the following steps:

  1. Create a new ...

Get OpenCV 4 with Python Blueprints - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.