Complete the code to read an image using OpenCV.
import cv2 image = cv2.[1]('image.jpg', cv2.IMREAD_COLOR)
The cv2.imread function reads an image from a file.
Complete the code to perform template matching using OpenCV.
result = cv2.matchTemplate(image, template, cv2.[1])TM_CCOEFF_NORMED is a common method for template matching that normalizes the correlation coefficient.
Fix the error in the code to find the best match location after template matching.
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
best_match = [1]For TM_CCOEFF_NORMED, the best match is at the location with the maximum value, so max_loc is correct.
Fill both blanks to draw a rectangle around the matched region.
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)
The width of the rectangle is the template's width (template.shape[1]) and the height is the template's height (template.shape[0]).
Fill all three blanks to complete the template matching and display the result.
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()
The method TM_CCOEFF_NORMED is used for matching. The best match location is max_loc. The rectangle width is the template's width template.shape[1].