Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCV Object Recognition

OpenCV Object Recognition

In the field of computer vision, object recognition is a very important task.

OpenCV is a powerful open-source computer vision library that provides a variety of methods to implement object recognition.

This article will introduce in detail how to use the template matching method in OpenCV (cv2.matchTemplate()) for object recognition.

What is template matching?

Template matching is a technique for finding the region in an image that is most similar to a given template image.

Simply put, template matching is finding the part of a large image that best matches the template image (i.e., the object we want to recognize). This method is suitable for cases where the size, orientation, and shape of the object in the image remain basically unchanged.

Basic principle of template matching

The basic principle of template matching is to slide the template image over the target image, compute the similarity at each position, and find the position with the highest similarity. OpenCV provides a variety of similarity calculation methods, such as squared difference matching (cv2.TM_SQDIFF), normalized squared difference matching (cv2.TM_SQDIFF_NORMED), correlation matching (cv2.TM_CCORR), normalized correlation matching (cv2.TM_CCORR_NORMED), correlation coefficient matching (cv2.TM_CCOEFF) and normalized correlation coefficient matching (cv2.TM_CCOEFF_NORMED)。

Application scenarios

Implementation steps of template matching

  1. Load images: Read the search image and the template image.

  2. Template matching: Use cv2.matchTemplate() Find the template image in the search image.

  3. Get the matching result: Use cv2.minMaxLoc() Get the best matching position.

  4. Draw the matching result: Draw the matching region in the search image.

  5. Display the result: Display the matching result.

Matching methods

OpenCV provides a variety of template matching methods, which can be specified through the third parameter of cv2.matchTemplate():

Method Description
cv2.TM_SQDIFF Squared difference matching; the smaller the value, the higher the match.
cv2.TM_SQDIFF_NORMED Normalized squared difference matching; the smaller the value, the higher the match.
cv2.TM_CCORR Correlation matching; the larger the value, the higher the match.
cv2.TM_CCORR_NORMED Normalized correlation matching; the larger the value, the higher the match.
cv2.TM_CCOEFF Correlation coefficient matching; the larger the value, the higher the match.
cv2.TM_CCOEFF_NORMED Normalized correlation coefficient matching; the larger the value, the higher the match.

Use cv2.matchTemplate() Perform object recognition

1. Import the necessary libraries

First, we need to import the OpenCV and NumPy libraries.

NumPy is the fundamental library for scientific computing in Python, and OpenCV uses NumPy arrays to store image data.

Example

import

cv2

import

numpy

as

np

2. Load the image and template

Next, we need to load the target image and the template image.

The target image is the image in which we want to find the object, and the template image is the object we want to recognize.

Example

Load the target image and template image

img

=

cv2.

imread

(

'target_image.jpg'

,

0

)

template

=

cv2.

imread

(

'template_image.jpg'

,

0

)

3. Get the dimensions of the template image

To slide the template image over the target image, we need to know the width and height of the template image.

Example

Get the dimensions of the template image

w

,

h

=

template.

shape

[

::-

1

]

4. Perform template matching

Use cv2.matchTemplate() function to perform template matching.

This function returns a result matrix, where each element represents the similarity between the corresponding position in the target image and the template image.

Example

Perform template matching

res

=

cv2.

matchTemplate

(

img

,

template

,

cv2.

TM_CCOEFF_NORMED

)

5. Set the matching threshold and find the matching position

We can set a threshold to determine whether the match is successful.

Then, use the cv2.minMaxLoc() function to find the positions of the maximum and minimum values in the result matrix.

Example

Set the matching threshold

threshold

=

0.8

Find the matching position

loc

=

np.

where

(

res

=

threshold

)

6. Mark the matching position in the target image

Finally, we can mark the positions in the target image that match the template.

Usually, we use a rectangle to mark the matching region.

Example

Mark the matching position in the target image

for

pt

in

zip

(

*loc

[

::-

1

]

)

:

    cv2.

rectangle

(

img

,

pt

,

(

pt

[

0

]

,

pt

[

1

]

)

,

(

0

,

255

,

0

)

,

2

)

Display the result image

cv2.

imshow

(

'Matched Image'

,

img

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)


Complete code

The following is a complete code example showing how to use cv2.matchTemplate() for object recognition.

Example

import

cv2

import

numpy

as

np

Load the target image and template image

img

=

cv2.

imread

(

'target_image.jpg'

,

0

)

template

=

cv2.

imread

(

'template_image.jpg'

,

0

)

Get the dimensions of the template image

w

,

h

=

template.

shape

[

::-

1

]

Perform template matching

res

=

cv2.

matchTemplate

(

img

,

template

,

cv2.

TM_CCOEFF_NORMED

)

Set the matching threshold

threshold

=

0.8

Find the matching position

loc

=

np.

where

(

res

=

threshold

)

Mark the matching position in the target image

for

pt

in

zip

(

*loc

[

::-

1

]

)

:

    cv2.

rectangle

(

img

,

pt

,

(

pt

[

0

]

,

pt

[

1

]

)

,

(

0

,

255

,

0

)

,

2

)

Display the result image

cv2.

imshow

(

'Matched Image'

,

img

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)