0
0
Computer Visionml~20 mins

Template matching in Computer Vision - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Matching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Template Matching Basics

What is the main purpose of template matching in computer vision?

ATo find the location of a smaller image (template) inside a larger image
BTo classify images into different categories based on content
CTo reduce the noise in an image by smoothing
DTo segment an image into multiple regions based on color
Attempts:
2 left
💡 Hint

Think about matching a small picture inside a bigger one.

Predict Output
intermediate
2:00remaining
Output of Template Matching Result

Given the following Python code using OpenCV, what is the shape of the result matrix?

Computer Vision
import cv2
import numpy as np
img = np.zeros((100, 100), dtype=np.uint8)
template = np.zeros((20, 20), dtype=np.uint8)
result = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
print(result.shape)
A(81, 81)
B(100, 100)
C(120, 120)
D(20, 20)
Attempts:
2 left
💡 Hint

The result size is the input image size minus the template size plus one.

Model Choice
advanced
1:30remaining
Choosing the Correct Matching Method

Which OpenCV template matching method is best when you want to find the location with the highest similarity score?

Acv2.TM_SQDIFF
Bcv2.TM_CCOEFF_NORMED
Ccv2.TM_SQDIFF_NORMED
Dcv2.TM_CCORR
Attempts:
2 left
💡 Hint

Look for the method that normalizes and uses correlation coefficient.

Metrics
advanced
1:30remaining
Interpreting Template Matching Scores

When using cv2.TM_SQDIFF method, what does a lower score in the result matrix indicate?

ANo correlation between template and image region
BA worse match between template and image region
CA better match between template and image region
DThe template is larger than the image region
Attempts:
2 left
💡 Hint

Think about squared differences and what a small value means.

🔧 Debug
expert
2:00remaining
Debugging Template Matching Code

What error will this code produce when run, and why?

import cv2
import numpy as np
img = np.zeros((50, 50), dtype=np.uint8)
template = np.zeros((60, 60), dtype=np.uint8)
result = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
print(result.shape)
ANo error, prints (1, 1)
BIndexError: index out of range
CTypeError: unsupported operand type(s) for -: 'int' and 'str'
Dcv2.error: (-215:Assertion failed) (image.cols >= templ.cols) && (image.rows >= templ.rows) in function 'matchTemplate'
Attempts:
2 left
💡 Hint

Check the sizes of the image and template.