help me improve this so it detects the template better and even if the image is rotated:…
- User
help me improve this so it detects the template better and even if the image is rotated:
import cv2 import numpy as np
Load the reference image and the main image
ref_image = cv2.imread("shot2Target0.png", 0) main_image = cv2.imread("shot(3).jpg", 0)
Find the reference image in the main image
res = cv2.matchTemplate(main_image, ref_image, cv2.TM_CCOEFF_NORMED)
Find the location of the match
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
Calculate the angle of rotation
angle = np.degrees(np.arctan2(max_loc[1] - ref_image.shape[0] / 2, max_loc[0] - ref_image.shape[1] / 2))
Define the rotation matrix
rotation_matrix = cv2.getRotationMatrix2D((ref_image.shape[1] / 2, ref_image.shape[0] / 2), angle, 1)
Rotate the main image
rotated_image = cv2.warpAffine(main_image, rotation_matrix, (ref_image.shape[1], ref_image.shape[0]))
Crop the main image
top_left = max_loc bottom_right = (top_left[0] + ref_image.shape[1], top_left[1] + ref_image.shape[0]) cropped_image = main_image[top_left[1] : bottom_right[1], top_left[0] : bottom_right[0]]
Display the cropped image
cv2.imshow("Cropped Image", cropped_image) cv2.waitKey(0) cv2.destroyAllWindows()
- Assistant
- User
it takes a really long time and also not able to find the template on different pictures
- Assistant
- User
nope doesn't show correctly
- Assistant
- User
rickard@pop-os:~/Documents/rs232
- Assistant
The error message you're encountering indicates that within the
main()function in your script, thegood_matchesvariable isNonewhen you are attempting to calllen(good_matches)at line 62. SinceNonedoes not have a length, this results in a TypeError.The
match_and_find_homographyfunction appears to be returning for the list when it doesn't find enough matches. To resolve this, you should account for the case when is before you attempt to take its length.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.