@@ -92,7 +92,6 @@ def match_image(template_path, threshold=0.8):
9292 # Capture current screen and get dimensions
9393 screenshot = capture_screen ()
9494 screenshot_height , screenshot_width = screenshot .shape [:2 ]
95-
9695 # Calculate scale factor
9796 scale_factor_x = screenshot_width / 2560
9897 scale_factor_y = screenshot_height / 1440
@@ -140,6 +139,61 @@ def match_image(template_path, threshold=0.8):
140139 # Return the list of center coordinates of all found elements or None if no elements found
141140 return []
142141
142+ def greyscale_match_image (template_path , threshold = 0.8 ):
143+ """Finds the image specified and returns the center coordinates, regardless of screen resolution,
144+ and saves screenshots of each match found."""
145+
146+ # Capture current screen and get dimensions
147+ screenshot = capture_screen ()
148+ screenshot_height , screenshot_width = screenshot .shape [:2 ]
149+ screenshot = cv2 .cvtColor (screenshot , cv2 .COLOR_BGR2GRAY )
150+ # Calculate scale factor
151+ scale_factor_x = screenshot_width / 2560
152+ scale_factor_y = screenshot_height / 1440
153+ scale_factor = min (scale_factor_x ,scale_factor_y )
154+ # Load and resize the template image according to the scale factor
155+ template = cv2 .imread (template_path , cv2 .IMREAD_GRAYSCALE )
156+ if template is None :
157+ raise FileNotFoundError (f"Template image '{ template_path } ' not found." )
158+
159+ template = cv2 .resize (template , None , fx = scale_factor , fy = scale_factor , interpolation = cv2 .INTER_LINEAR )
160+ template_height , template_width = template .shape [:2 ]
161+
162+ # Perform template matching
163+ result = cv2 .matchTemplate (screenshot , template , cv2 .TM_CCOEFF_NORMED )
164+
165+ # Get locations where the match confidence exceeds the threshold
166+ if scale_factor < 0.75 :
167+ threshold = threshold - 0.05 #Testing for detecting on lower scales
168+ locations = np .where (result >= threshold )
169+ boxes = []
170+
171+ # Loop through all the matching locations and create bounding boxes
172+ for pt in zip (* locations [::- 1 ]): # Switch columns and rows
173+ top_left = pt
174+ bottom_right = (top_left [0 ] + template_width , top_left [1 ] + template_height )
175+ boxes .append ([top_left [0 ], top_left [1 ], bottom_right [0 ], bottom_right [1 ]])
176+
177+ boxes = np .array (boxes )
178+
179+ # Apply non-maximum suppression to remove overlapping boxes
180+ filtered_boxes = non_max_suppression_fast (boxes )
181+
182+ # List to hold the center coordinates of all filtered elements
183+ found_elements = []
184+
185+ # Save a screenshot of each matched region
186+ for match_index , (x1 , y1 , x2 , y2 ) in enumerate (filtered_boxes ):
187+ center_x = (x1 + x2 ) // 2
188+ center_y = (y1 + y2 ) // 2
189+ found_elements .append ((center_x , center_y ))
190+
191+ if found_elements :
192+ #save_match_screenshot(screenshot, (x1, y1), (x2, y2), template_path, match_index)
193+ return sorted (found_elements )
194+ # Return the list of center coordinates of all found elements or None if no elements found
195+ return []
196+
143197def proximity_check (list1 , list2 , threshold ):
144198 close_pairs = set () # To store pairs of coordinates that are close
145199 for coord1 in list1 :
0 commit comments