Live up to the time, the creation of non-stop, this article is participating in 2021 year-end summary essay contest
preface
Use Python to achieve OpenCV image retrieval. Without further ado.
Let’s have a good time
The development tools
Python version: 3.6.4
Related modules:
Cv2 module;
And some modules that come with Python.
Environment set up
Install Python and add it to the environment variables. PIP installs the required related modules.
Template matching
Template matching is to find a small area matching the given stator image in the whole image region
Here we need a template image (given child image) and an image to be detected (original image)
On the image to be detected, the matching degree between template image and overlapping sub-image is calculated from left to right and top to bottom. The greater the matching degree is, the more likely the two are to be the same
The cv2.matchTemplate() and cv2.minmaxloc () functions of OpenCV are mainly involved here
The first function is to find a match between the template and the input image and get the matching result image
The second function finds the maximum and minimum values (including their positions) in a given matrix
There are six template matching algorithms
# The first type, using the square variance to match, the best match is 0. The worse the match, the greater the match value
# square deviation matching
method=CV_TM_SQDIFF
# Standard square deviation matching
method=CV_TM_SQDIFF_NORMED
# The second type adopts the multiplication operation between template and image, so a larger number indicates a higher matching degree, and 0 indicates the worst matching effect
# correlation matching
method=CV_TM_CCORR
# Standard correlation matching
method=CV_TM_CCORR_NORMED
# Third, match the relative value of the template to its mean with the correlation value of the image to its mean,1 means perfect match,-1 means bad match,0 means no correlation (random sequence)
# Correlation coefficient matching
method=CV_TM_CCOEFF
# Standard correlation coefficient matching
method=CV_TM_CCOEFF_NORMED
Copy the code
Standardization means unifying values to 0 or 1
Retrieve the image
Take a look at these two images, both grayscale
import cv2
Read the image to be detected
img = cv2.imread('game.png'.0)
Read the template image
temple = cv2.imread('temple.png'.0)
# Display the image to be detected after gray processing
cv2.namedWindow('sample'.0)
cv2.resizeWindow('sample'.400.600)
cv2.imshow('sample', img)
# Show the template image after grayscale processing
cv2.namedWindow('target'.0)
cv2.resizeWindow('target'.400.600)
cv2.imshow('target', temple)
Copy the code
The results show
The first is the template image, and the second is the image to be detected
The following uses two functions of OpenCV to achieve template matching
Get the height and width of the template image
th, tw = temple.shape[:2]
print(th, tw)
# Use standard correlation coefficient matching,1 for perfect match,-1 for bad match, and 0 for no correlation at all
result = cv2.matchTemplate(img, temple, cv2.TM_CCOEFF_NORMED)
# result = matching result matrix
# print(result)
# TM_CCOEFF_NORMED method after processing the resulting image
cv2.namedWindow('match_r'.0)
cv2.resizeWindow('match_r'.400.600)
# display window
cv2.imshow('match_r', result)
# Use the function minMaxLoc to determine the maximum and minimum values of the matching result matrix (val) and their positions (LOC).
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# Select the position of the maximum value here, which is the upper left corner of the image
tl = max_loc
Get the lower right corner of the image
br = (tl[0]+tw, tl[1]+th)
# Draw rectangle box
cv2.rectangle(img, tl, br, (0.0.255), 2)
Set the display window
cv2.namedWindow('match'.0)
cv2.resizeWindow('match'.400.600)
# display window
cv2.imshow('match', img)
# end
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code
Results output
The whitest position in the first diagram represents the highest match
In the second picture, the rectangle represents the result of the match
Through the position parameters of the rectangular box and the size of the template image, the center position of the little checkers (bottom) can be obtained.