def test_scale_image_interpolation_auto(self): original_image = np.full((200, 200, 3), [0, 200, 0], dtype=np.uint8) # Set the centre pixel fo the original image to a different colour for comparison once scaling is complete. original_image[100, 100] = [200, 0, 0] larger_target_image = np.zeros((400, 400, 3), dtype=np.uint8) scaled_result = TSEImageUtils.scale_image_interpolation_auto( original_image, larger_target_image) # We would expect the centre pixel of the scaled image NOT to be GREEN, as in the original non-scaled image this was set to RED. assert_false(np.array_equal(scaled_result[200, 200], [0, 200, 0])) # We would expect all other pixels (apart from immediate neighbours around the centre pixel due to the interpolation) to still be GREEN. assert_true(np.array_equal(scaled_result[0, 0], [0, 200, 0])) assert_true(np.array_equal(scaled_result[399, 399], [0, 200, 0])) assert_true(np.array_equal(scaled_result[195, 195], [0, 200, 0])) assert_true(np.array_equal(scaled_result[205, 205], [0, 200, 0]))
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