def test_extract_image_sub_window(self): test_image = np.zeros((200, 200, 3), dtype=np.uint8) # Set 50x50px section to Red (top-left corner) test_image[0:50, 0:50] = [0, 0, 200] expected_result = np.full((50, 50, 3), [0, 0, 200], dtype=np.uint8) extracted_slice = TSEImageUtils.extract_image_sub_window( test_image, TSEPoint(0, 0), TSEPoint(50, 50)) assert_true(np.array_equal(expected_result, extracted_slice))
def scan_search_window_scaling(self, template_patch, template_patch_origin, match_method, force_cont_search=False): image_height, image_width = self._hsv_img2.shape[:2] image_centre_x = math.floor(image_width / 2) template_patch_height, template_patch_width = template_patch.shape[:2] new_localised_window_height = image_height - template_patch_height best_score = -1 best_position = 0 stop = False last_width = template_patch_width prev_current_window_scaled_coords = None for i in range(template_patch_origin.y, new_localised_window_height): score = 0 if i >= (template_patch_origin.y + 1): last_width = self._calibration_lookup[i - 1] calibrated_patch_width = self._calibration_lookup[i] patch_half_width = math.floor(calibrated_patch_width / 2) scale_factor = TSEGeometry.calc_measure_scale_factor( last_width, calibrated_patch_width) if prev_current_window_scaled_coords is None: current_window_scaled_coords = TSEImageUtils.scale_image_roi_relative_centre( ((image_centre_x - patch_half_width), i), ((image_centre_x + patch_half_width), (i + template_patch_height)), scale_factor) else: # We add +1 to the 'Y' coordinate as we are moving the search window down the ROI by one pixel each time we increase the width. current_window_scaled_coords = TSEImageUtils.scale_image_roi_relative_centre( (prev_current_window_scaled_coords[0].x, prev_current_window_scaled_coords[0].y + 1), (prev_current_window_scaled_coords[1].x, prev_current_window_scaled_coords[1].y + 1), scale_factor) prev_current_window_scaled_coords = current_window_scaled_coords scaled_search_window = TSEImageUtils.extract_image_sub_window( self._hsv_img2, current_window_scaled_coords[0], current_window_scaled_coords[1]) if match_method.match_type == tse_match_methods.DISTANCE_ED: score = TSEImageUtils.calc_ed_template_match_score_scaled_compiled( template_patch, scaled_search_window) elif match_method.match_type == tse_match_methods.DISTANCE: score = TSEImageUtils.calc_template_match_compare_cv2_score_scaled( template_patch, scaled_search_window, match_method.match_id) elif match_method.match_type == tse_match_methods.HIST: scaled_template_patch = TSEImageUtils.scale_image_interpolation_auto( template_patch, scaled_search_window) score = TSEImageUtils.calc_compare_hsv_histogram( scaled_template_patch, scaled_search_window, match_method.match_id) # If lower score means better match, then the method is a 'reverse' method. if match_method.reverse_score: if best_score == -1 or score < best_score: best_score = score best_position += 1 else: stop = True else: if best_score == -1 or score > best_score: best_score = score best_position += 1 else: stop = True if (force_cont_search is False) and (stop is True): break # We need to return the 'Y' with the best score (i.e. the displacement) return best_position