Template matching and application

Template matching and application

1) Template matching

Template matching is a technique of finding the part of an image that best matches (similarly) to another template image. Template matching is not based on histograms, but by

It is a method to match the template and the input image by sliding the image block (template) on the input image and comparing the similarity at the same time.
insert image description here
application:

① Target search and positioning
② Moving object tracking
③ Other...

Template matching—cv2.matchTemplate()

insert image description here
⚫ image: the image to be searched (large image)
⚫ templ: the search template, which needs to be of the same data type as the original image and whose size cannot be larger than the source image
⚫ result: the mapped image of the comparison result, which must be a single-channel, 32-bit floating-point type Image, if the size of the original image (image to be searched) is W x H,

If the templ size is wxh, the result size must be (W-w+1)x(H-h+1)
⚫ method: The specified matching method, there are the following 6 types:
cv2.TM_SQDIFF ------square difference Matching method (best match 0) cv2.TM_SQDIFF_NORMED ------ normalized square difference matching method (best match 0) cv2.TM_CCORR ------ correlation matching method (worst match 0) cv2. TM_CCORR_NORMED ------ normalized correlation matching method (worst match 0) cv2.TM_CCOEFF ------ coefficient matching method (best match 1) cv2.TM_CCOEFF_NORMED ------ normalized correlation coefficient matching method (best match 1)

res = cv2 .matchTemplate (img,temp,CV2 .TM CCORR NORMED)
min_val, max_val, min_loc, max_loc = cv2 .minMaxLoc (res)

In general, as we go from simple measures (squared differences) to more complex measures (correlation coefficients), we obtain more and more accurate matches (which also means larger and larger calculations).

The best thing to do is to do some experimentation with all these settings to choose the best combination of speed and accuracy for your application.

insert image description here

Matrix normalization—cv2.normalize()

insert image description here
⚫src: Input the original image
⚫dst: Output the resulting image, which needs to be the same size and type as the original image
⚫alpha: The minimum value after normalization, the default value is 1
⚫beta: The maximum value after normalization, the default value is 0
⚫norm_type: normalization type, optional NORM_INF, NORM_L1, NORM_L2 (default), etc.
⚫dtype: default value -1, when this parameter is negative, the output matrix and src have the same type
⚫mask: optional mask operation

cv2.normalize(roihist,roihist,0,255,CV2.NORM_MINMAX)

The role of normalize0) is to normalize the matrix.

Find the maximum value—cv2.minMaxLoc()
insert image description here
⚫src: Input the original image, single-channel image
⚫minVal: Return the pointer of the minimum value, if you don’t need to return, set 0
⚫maxVal: Return the pointer of the maximum value, if you don’t need to return, set it 0
⚫minLoc: Return the pointer of the minimum position, if it is not necessary to return, set 0
⚫maxLoc: Return the pointer of the maximum position, if it is not necessary to return, set 0
⚫mask: Optional mask operation

res = cv2 .matchTemplate (img ,temp,CV2 .TM CCORR NORMED)
min val, max val, min loc, max loc = cv2 .minMaxLoc(res)

The purpose of the minMaxLoc0) function is to find the global minimum and maximum values ​​in the array.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_40911806/article/details/130053018