Python OpenCV Match Multiple Objects From an Image: A Beginner Guide – OpenCV Tutorial

By | October 20, 2020

We have introduced how to detect object using python opencv.

cv2.matchTemplate(): Object Detection From Image using Python OpenCV

However, it only can detect one object each time from an image. In this tutorial, we will introduce how to detect multiple objects from an image.

We also use cv2.matchTemplate() to implement this function.

How to detect multiple objects from an image?

We will read an source and template image using opencv.

import cv2
import numpy as np
from matplotlib import pyplot as plt
 
img = cv2.imread('comment_source.jpg', cv2.IMREAD_COLOR)
img2 = img.copy()
template = cv2.imread('comment_target_2.jpg', cv2.IMREAD_COLOR)
w = template.shape[0]
h = template.shape[1]

The source image is:

cv2.matchTemplate() source image

The template image is:

cv2.matchTemplate() template image

We can find there are 5 template images in source image.

In order to detect multiple images from a big image, we can do as follows:

Once we have detected template image from source image, we will replace this region using a black image, then we detect template image again.

To replace a part of image using other image, you can read:

OpenCV Replace a Part of Image Using Other Image or NumPy Array

We will create an function to replace template image region.

def maskSourceImage(image, top_left, mask_height, mask_width):
    mask = np.zeros((mask_height, mask_width, 3))
    image[top_left[1]:(top_left[1]+mask.shape[0]), top_left[0]:(top_left[0]+mask.shape[1])] = mask
    return image

In this tutorial, we will use cv2.TM_SQDIFF_NORMED to detect multiple objects.

method = cv2.TM_SQDIFF_NORMED
objects = 5

for i in range(objects):
    res = cv2.matchTemplate(img2, template, method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    
    top_left = min_loc
    print(top_left) #(268, 839)  w, h
    print(min_val)
    
    bottom_right = (top_left[0] + w, top_left[1] + h)
 
    cv2.rectangle(img, top_left, bottom_right, 255, 2)
    
    img2 = maskSourceImage(img2, top_left, mask_height=h, mask_width=w)

Finally, we will use matplotlib to show the effect.

plt.subplot(121),
plt.imshow(img2),
plt.title('Source Image'),
plt.axis('off')
    
plt.subplot(122),
plt.imshow(img),
plt.title('Detected Point'),
plt.axis('off')
plt.suptitle(method)
 
plt.show()

Run this code, you will see this result.

Python OpenCV Match Multiple Objects From an Image: A Beginner Guide - OpenCV Tutorial

We can find the template image regions are replaced by a black image in source image. 5 template images are detected successfully.

Leave a Reply

Your email address will not be published. Required fields are marked *