def test_calc_compare_histogram_CORREL(self):
        image_1 = np.zeros((200, 200, 3), dtype=np.uint8)
        image_2 = image_1
        image_3 = np.full((200, 200, 3), [100, 0, 0], dtype=np.uint8)

        matching_result = TSEImageUtils.calc_compare_hsv_histogram(
            image_1, image_2, cv2.cv.CV_COMP_CORREL)
        non_matching_result = TSEImageUtils.calc_compare_hsv_histogram(
            image_1, image_3, cv2.cv.CV_COMP_CORREL)

        # Matching result should be greater than a non-matching result for CORREL matching method.
        assert_true(matching_result > non_matching_result)
    def test_calc_compare_histogram_CHISQR(self):
        image_1 = np.zeros((200, 200, 3), dtype=np.uint8)
        image_2 = image_1
        image_3 = np.full((200, 200, 3), [100, 0, 0], dtype=np.uint8)

        # Check that for perfectly matching images, we get a score of exactly 0.
        assert_equal(
            TSEImageUtils.calc_compare_hsv_histogram(image_1, image_2,
                                                     cv2.cv.CV_COMP_CHISQR), 0)

        # Check that for non-matching images, we get a score > 0.
        assert_true(
            TSEImageUtils.calc_compare_hsv_histogram(
                image_1, image_3, cv2.cv.CV_COMP_CHISQR) > 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
    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