Python OpenCV Getting Started template matching (num) __python

Source: Internet
Author: User

Content from Opencv-python tutorials own translation finishing

Target:
Finding a target in an image using template matching
Learning function Cv2.matchtemplate (), Cv2.minmaxloc ()

principle:
Template matching is a way to find template images in a pair of images. A function cv2.matchtemplate () is implemented in OpenCV. Like 2D convolution, it also slides (like a window) on the input image with a template image, matching the template image to the window area of the input image at each location. The exact principle is similar to the inverted projection of the histogram.
OPENCV offers several different matching methods.
function document
-Cv_tm_sqdiff squared Difference Matching method: The method uses square difference to match, the best match is 0, the match is worse, the matching value is larger.
-Cv_tm_sqdiff_normed Correlation Matching method: This method uses multiplication operation, the larger the numerical value indicates the better the matching degree.
-Cv_tm_ccorr correlation coefficient matching method: 1 for perfect match;-1 for worst match.
-cv_tm_ccorr_normed normalized square difference matching method
-Cv_tm_ccoeff normalized correlation matching method
-cv_tm_ccoeff_normed normalized correlation coefficient matching method

Each method corresponds to a different calculation formula. Formula slightly

The result returns a grayscale chart in which each pixel value represents the degree to which the adjacent area of the pixel matches the template.

If the input image size is WxH, the template size is WxH, the output result of the big trifle (w-w+1,h-h+1). When you get this result, you can use the function Cv2.minmaxloc () to find the location of the minimum and maximum values. The first value is the position of the upper-left corner of the rectangle (w,h) is the width and height of the template rectangle. The rectangle is the template area.

If the comparison method used is cv2. Tm_sqdiff, then the minimum value corresponds to the location of the matching region.

using OpenCV for template matching

Here's a fun one.

In a pile of chicken sister find Jolin Tsai (last row third, if my eyes are not blind)

Cut a picture and cut out the Jolin.

Import CV2 Import NumPy as NP from matplotlib import pyplot as plt img = cv2.imread (' 16.jpg ', 0) print (img.shape) Img2 = i Mg.copy () template = Cv2.imread (' 166.jpg ', 0) W, h = template.shape[::-1] # All of the 6 methods for comparison in a list met Hods = [' Cv2. Tm_ccoeff ', ' Cv2. Tm_ccoeff_normed ', ' Cv2. Tm_ccorr ', ' Cv2. Tm_ccorr_normed ', ' Cv2. Tm_sqdiff ', ' Cv2.
    Tm_sqdiff_normed '] for meth in methods:img = Img2.copy () method = eval (meth) # Apply template Matching res = Cv2.matchtemplate (img,template,method) # print (Res.shape) min_val, Max_val, min_loc, Max_loc = Cv2.minmaxloc ( RES) #找到最大值和最小值 Print (Cv2.minmaxloc (res)) # If The is Tm_sqdiff or tm_sqdiff_normed, take minimum If met Hod in [Cv2. Tm_sqdiff, Cv2. Tm_sqdiff_normed]: top_left = min_loc Else:top_left = Max_loc Bottom_right = (Top_left[0] + W, to P_LEFT[1] + h) cv2.rectangle (Img,top_left, Bottom_right, 255, 2) Plt.subplot (121), plt.imshow (Res,cmap = ' Gray') plt.title (' Matching result '), plt.xticks ([]), Plt.yticks ([]) Plt.subplot (122), plt.imshow (Img,cmap = ' gray ') Plt.title (' detected point '), Plt.xticks ([]), Plt.yticks ([]) Plt.suptitle (meth) plt.show ()

The result is not very ideal, incredibly cannot find. It seems that Fengjie and Jolin are really not very good at distinguishing-_-

multi-Object matching

The target object appears many times, how to find.
The Cv.immaxloc () function gives only the maximum and minimum values, and is now used to find the threshold value.

Import cv2
import NumPy as NP from
matplotlib import pyplot as plt
Img_rgb = cv2.imread (' 17.jpg ')
Img_gra y = Cv2.cvtcolor (Img_rgb, Cv2. Color_bgr2gray)
template = Cv2.imread (' 177.jpg ', 0)

W, h = template.shape[::-1]
res = cv2.matchtemplate ( Img_gray,template,cv2. tm_ccoeff_normed)

threshold = 0.25

loc = Np.where (res >= threshold) for
pt in Zip (*loc[::-1)):
    Cv2 . Rectangle (Img_rgb, PT, (Pt[0] + W, pt[1] + h), (0,0,255), 2
cv2.imwrite (' res.png ', Img_rgb)

I would like to find all the other than Jolin Tsai Sister ~ results ....
This matching method is not reliable ....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.