def test_calc_template_match_compare_cv2_score_CCORR(self): image_1 = np.full((200, 200, 3), [0, 200, 0], dtype=np.uint8) image_2 = image_1 image_3 = np.full((200, 200, 3), [200, 0, 0], dtype=np.uint8) matching_result = TSEImageUtils.calc_template_match_compare_cv2_score( image_1, image_2, cv2.cv.CV_TM_CCORR_NORMED) non_matching_result = TSEImageUtils.calc_template_match_compare_cv2_score( image_1, image_3, cv2.cv.CV_TM_CCORR_NORMED) # For CCORR, we would expect that a perfectly matching image will score HIGHER than a non-matching image assert_true(matching_result > non_matching_result)
def test_calc_template_match_compare_cv2_score_SQDIFF(self): image_1 = np.zeros((200, 200, 3), dtype=np.uint8) image_2 = image_1 image_3 = np.full((200, 200, 3), [0, 0, 200], dtype=np.uint8) # Check that for perfectly matching images, we get a score of exactly 0. assert_equal( TSEImageUtils.calc_template_match_compare_cv2_score( image_1, image_2, cv2.cv.CV_TM_SQDIFF), 0) # Check that for non-matching images, we get a score > 0. assert_true( TSEImageUtils.calc_template_match_compare_cv2_score( image_1, image_3, cv2.cv.CV_TM_SQDIFF) > 0)
def scan_search_window(self, template_patch, template_patch_origin, match_method, force_cont_search=False): image_height, image_width = self._hsv_img2.shape[:2] template_patch_height, template_patch_width = template_patch.shape[:2] localised_window = self._hsv_img2[template_patch_origin.y:image_height, template_patch_origin.x:( template_patch_origin.x + template_patch_width)] localised_window_height, localised_window_width = localised_window.shape[: 2] best_score = -1 best_position = 0 stop = False for i in range(0, (localised_window_height - template_patch_height)): current_window = localised_window[i:(i + template_patch_height), 0:template_patch_width] score = 0 if match_method.match_type == tse_match_methods.DISTANCE_ED: score = TSEImageUtils.calc_euclidean_distance_cv2_norm( template_patch, current_window) elif match_method.match_type == tse_match_methods.DISTANCE: score = TSEImageUtils.calc_template_match_compare_cv2_score( template_patch, current_window, match_method.match_id) elif match_method.match_type == tse_match_methods.HIST: score = TSEImageUtils.calc_compare_hsv_histogram( template_patch, current_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 = i else: stop = True else: if best_score == -1 or score > best_score: best_score = score best_position = i 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