Bird
Raised Fist0
Computer Visionml~10 mins

Template matching in Computer Vision - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read an image using OpenCV.

Computer Vision
import cv2

image = cv2.[1]('image.jpg', cv2.IMREAD_COLOR)
Drag options to blanks, or click blank then click option'
Aimread
Bimshow
Cimwrite
Dresize
Attempts:
3 left
💡 Hint
Common Mistakes
Using cv2.imshow instead of cv2.imread to load the image.
Using cv2.imwrite which saves an image, not loads it.
2fill in blank
medium

Complete the code to perform template matching using OpenCV.

Computer Vision
result = cv2.matchTemplate(image, template, cv2.[1])
Drag options to blanks, or click blank then click option'
ATM_CCOEFF_NORMED
BTM_SQDIFF
CTM_CCORR
DTM_SQDIFF_NORMED
Attempts:
3 left
💡 Hint
Common Mistakes
Using TM_SQDIFF which measures squared differences and requires different interpretation.
Using TM_CCORR which is less robust to lighting changes.
3fill in blank
hard

Fix the error in the code to find the best match location after template matching.

Computer Vision
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
best_match = [1]
Drag options to blanks, or click blank then click option'
Amin_val
Bmax_val
Cmax_loc
Dmin_loc
Attempts:
3 left
💡 Hint
Common Mistakes
Using min_loc which is the location of the minimum value, incorrect for this method.
Using min_val or max_val which are values, not locations.
4fill in blank
hard

Fill both blanks to draw a rectangle around the matched region.

Computer Vision
top_left = best_match
bottom_right = (top_left[0] + [1], top_left[1] + [2])
cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2)
Drag options to blanks, or click blank then click option'
Atemplate.shape[1]
Btemplate.shape[0]
Cimage.shape[1]
Dimage.shape[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using image dimensions instead of template dimensions.
Swapping width and height values.
5fill in blank
hard

Fill all three blanks to complete the template matching and display the result.

Computer Vision
import cv2

image = cv2.imread('scene.jpg')
template = cv2.imread('template.jpg')
result = cv2.matchTemplate(image, template, cv2.[1])
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
best_match = [2]
top_left = best_match
bottom_right = (top_left[0] + [3], top_left[1] + template.shape[0])
cv2.rectangle(image, top_left, bottom_right, (255, 0, 0), 3)
cv2.imshow('Matched Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Drag options to blanks, or click blank then click option'
ATM_CCOEFF_NORMED
Bmax_loc
Ctemplate.shape[1]
Dmin_loc
Attempts:
3 left
💡 Hint
Common Mistakes
Using min_loc instead of max_loc for best match.
Using image width instead of template width for rectangle.
Using a different matching method that requires different interpretation.

Practice

(1/5)
1. What is the main purpose of template matching in computer vision?
easy
A. To reduce the size of an image without losing quality
B. To classify images into different categories
C. To find a small image inside a larger image by comparing pixel patterns
D. To generate new images from existing ones

Solution

  1. Step 1: Understand template matching concept

    Template matching searches for a smaller image (template) inside a bigger image by comparing pixel patterns.
  2. Step 2: Compare with other options

    Other options describe classification, resizing, or generation, which are different tasks.
  3. Final Answer:

    To find a small image inside a larger image by comparing pixel patterns -> Option C
  4. Quick Check:

    Template matching = find small image inside big image [OK]
Hint: Template matching = locating small image inside big one [OK]
Common Mistakes:
  • Confusing template matching with image classification
  • Thinking it changes image size
  • Assuming it creates new images
2. Which of the following is the correct OpenCV function call to perform template matching?
easy
A. cv2.matchTemplate(image, template, method)
B. cv2.templateMatch(image, template)
C. cv2.findTemplate(image, template, method)
D. cv2.match(image, template)

Solution

  1. Step 1: Recall OpenCV template matching syntax

    The correct function is cv2.matchTemplate with parameters (image, template, method).
  2. Step 2: Check other options for correctness

    Other options use incorrect function names or missing parameters.
  3. Final Answer:

    cv2.matchTemplate(image, template, method) -> Option A
  4. Quick Check:

    OpenCV template matching = cv2.matchTemplate [OK]
Hint: Remember exact OpenCV function name: matchTemplate [OK]
Common Mistakes:
  • Using wrong function names like templateMatch or findTemplate
  • Omitting the method parameter
  • Confusing with other OpenCV functions
3. Given the following code snippet, what will be the shape of the result from cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED) if image is 100x100 pixels and template is 20x20 pixels?
medium
A. (100, 100)
B. (81, 81)
C. (120, 120)
D. (80, 80)

Solution

  1. Step 1: Understand output size formula

    The output size is (W - w + 1, H - h + 1) where W,H are image dims and w,h are template dims.
  2. Step 2: Calculate output shape

    For image 100x100 and template 20x20, output = (100-20+1, 100-20+1) = (81, 81).
  3. Final Answer:

    (81, 81) -> Option B
  4. Quick Check:

    Output shape = (image - template + 1) [OK]
Hint: Output shape = image size minus template size plus one [OK]
Common Mistakes:
  • Using image size directly as output shape
  • Adding template size instead of subtracting
  • Off-by-one errors in calculation
4. You run template matching but get an error: cv2.error: (-215:Assertion failed) src.type() == templ.type() in function 'matchTemplate'. What is the most likely cause?
medium
A. The template image and source image have different data types or channels
B. The template image is larger than the source image
C. The method parameter is missing in the function call
D. The images are not converted to grayscale

Solution

  1. Step 1: Analyze error message

    The error says src.type() == templ.type() failed, meaning image and template types differ.
  2. Step 2: Identify cause

    Different data types or number of channels (e.g., one grayscale, one color) cause this error.
  3. Final Answer:

    The template image and source image have different data types or channels -> Option A
  4. Quick Check:

    Image and template must have same type [OK]
Hint: Check image and template have same type and channels [OK]
Common Mistakes:
  • Assuming template size causes this error
  • Forgetting to pass method parameter causes this error
  • Thinking grayscale conversion is mandatory for all cases
5. You want to detect a rotated version of a template inside an image using template matching. Which approach is best to improve detection?
hard
A. Resize the image to match the template size
B. Use the original template only without rotation
C. Convert both images to grayscale before matching
D. Rotate the template at multiple angles and run template matching for each

Solution

  1. Step 1: Understand template matching limitation

    Template matching works best when template matches image exactly in size and orientation.
  2. Step 2: Handle rotation

    To detect rotated templates, rotate the template at different angles and match each rotated version.
  3. Final Answer:

    Rotate the template at multiple angles and run template matching for each -> Option D
  4. Quick Check:

    Rotate template for rotated detection [OK]
Hint: Try multiple rotated templates to detect rotated objects [OK]
Common Mistakes:
  • Using only original template ignores rotation
  • Resizing image does not fix rotation mismatch
  • Grayscale conversion helps but doesn't solve rotation