Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Cartooning an Image using OpenCV - Python
Next article icon

Template matching is a technique for finding areas of an image that are similar to a patch (template). A patch is a small image with certain features. The goal of template matching is to find the patch/template in an image. To find it, the user has to give two input images: Source Image (S) - The image to find the template in, and Template Image (T)- The image that is to be found in the source image.  

How does Template Matching Work?

Python
# Python program to illustrate# template matchingimportcv2importnumpyasnp# Read the main imageimg_rgb=cv2.imread('mainimage.jpg').# Convert it to grayscaleimg_gray=cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)# Read the templatetemplate=cv2.imread('template',0)# Store width and height of template in w and hw,h=template.shape[::-1]# Perform match operations.res=cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)# Specify a thresholdthreshold=0.8# Store the coordinates of matched area in a numpy arrayloc=np.where(res>=threshold)# Draw a rectangle around the matched region.forptinzip(*loc[::-1]):cv2.rectangle(img_rgb,pt,(pt[0]+w,pt[1]+h),(0,255,255),2)# Show the final image with the matched area.cv2.imshow('Detected',img_rgb)

Limitations of Template Matching: 

  1. Pattern occurrences have to preserve the orientation of the reference pattern image(template)
  2. As a result, it does not work for rotated or scaled versions of the template as a change in shape/size/shear, etc. of object w.r.t. the template will give a false match.
  3. The method is inefficient when calculating the pattern correlation image for medium to large images as the process is time-consuming.

To avoid the issue caused by the different sizes of the template and original image we can usemultiscaling. In the case where, just because the dimensions of your template do not match the dimensions of the region in the image you want to match, does not mean that you cannot apply template matching. 

Multiscaling mechanism in Template Matching

The process of Multi scaling is as follows: 

  1. Loop over the input image at multiple scales (i.e. make the input image progressively smaller and smaller).
  2. Apply template matching using cv2.matchTemplate and keep track of the match with the largest correlation coefficient (along with the x, and y-coordinates of the region with the largest correlation coefficient).
  3. After looping over all scales, take the region with the largest correlation coefficient and use that as your “matched” region.
Python
# Python program to illustrate# template matchingimportcv2importnumpyasnp# Read the main imageimg_rgb=cv2.imread('mainimage.jpg').# Convert it to grayscaleimg_gray=cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)# Read the templatetemplate=cv2.imread('template',0)# Store width and height of template in w and hw,h=template.shape[::-1]# Resize the image according to scale and# keeping track of ratio of resizingresize=imutils.resize(img_gray,width=int(shape[0]),height=int(img_gray.shape[1]*scale)# If resize image is smaller than that of template# break the loop# Detect edges in the resized, grayscale image and apply template# Matching to find the template in image edged# If we have found a new maximum correlation value, update# the found variable if# found = null/maxVal > found][0]ifresized.shape[0]<horresized.shape[1]<w:breakfound=(maxVal,maxLoc,r)# Unpack the found variables and compute(x,y) coordinates# of the bounding box(__,maxLoc,r)=found(startX,startY)=(int(maxLoc[0]*r),intmaxLoc[1]*r)(endX,endY)=(int((maxLoc[0]+tw)*r),int(maxLoc[1]+tH)*r)# Draw a bounding box around the detected result and display the imagecv2.rectangle(image,(startX,startY),(endX,endY),(0,0,255),2)cv2.imshow("Image",image)cv2.waitKey(0)

A step-by-step explanation of the above code is as follows: 

  • After storing the width and height of the template in w and r, we initialize a variable found to keep track of the region and scale of the image with the best match. From there we start looping over the multiple scales of the image using the np.linspace function. This function accepts three arguments, the starting value, the ending value, and the number of equal chunk slices in between. In this example, we’ll start from 100% of the original size of the image and work our way down to 20% of the original size in 20 equally sized percent chunks.
  • We then resize the image according to the current scale and compute the ratio of the old width to the new width — as you’ll see later, it’s important that we keep track of this ratio. We make a check to ensure that the input image is larger than our template matching. If the template is larger, then our cv2.matchTemplate call will throw an error, so we just break from the loop if this is the case.
  • At this point we can apply template matching to our resized image: 
    • The cv2.minMaxLoc function takes our correlation result and returns a 4-tuple which includes the minimum correlation value, the maximum correlation value, the (x, y)-coordinate of the minimum value, and the (x, y)-coordinate of the maximum value, respectively. We are only interested in the maximum value and (x, y)-coordinate so we keep the maximums and discard the minimums.
  • After that, we inspect the regions of the image that are getting matched at each iteration of the scale. From there, we update our found variable found to keep track of the maximum correlation value found thus far, the (x, y)-coordinate of the maximum value, along with the ratio of the original image width to the current, resized image width.
  • After we have looped over all scales of the image, we unpack our found variable and then compute our starting and ending (x, y)-coordinates of our bounding box. Special care is taken to multiply the coordinates of the bounding box by the ratio to ensure that the coordinates match the original dimensions of the input image.
  • Finally, we draw our bounding box and display it on our screen.

 


K

kartik
Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp