help me improve this so it detects the template better and even if the image is rotated,…
Jump to turn (8)
- 1help me improve this so it detects the template better and even if the image is
- 2rickard@pop-os:~/Documents/rs232$ cd /home/rickard/Documents/rs232 ; /usr/bin/en
- 3like this?: import cv2 import numpy as np Load the reference image and the main
- 4"FLANNINDEXLSH" is not definedPylancereportUndefinedVariable (function) FLANNIND
- 5it just works awfully, must be a way to get way more accurate? import cv2 import
- 6I want to match the squared box shape not the numbers inside the box tho
- 7where can I decide that the box need to be a certain size minimum?
- 8wrong box gets recognised?: import cv2 import numpy as np Load the reference ima
- User
help me improve this so it detects the template better and even if the image is rotated, needs to be light driven and fast:
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