0
0
Computer Visionml~10 mins

Template matching in Computer Vision - Interactive Code Practice

Choose your learning style9 modes available
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.