コード例 #1
0
    def test_calc_measure_scale_factor(self):

        current_value = 10
        target_value = 100

        current_value2 = 100
        target_value2 = 9

        assert_equal(
            TSEGeometry.calc_measure_scale_factor(current_value, target_value),
            10.0)

        assert_equal(
            TSEGeometry.calc_measure_scale_factor(current_value2,
                                                  target_value2), 0.09)
コード例 #2
0
    def test_calc_ssd_slow(self):
        # Create a sample test image that is empty.
        original_image = np.full((400, 400, 3), [0, 200, 0],  dtype=np.uint8)

        # Calculate the scale factor (MAKING SURE TO SUBTRACT '1' from the max height/width to account for array index out of bounds issue)
        scale_factor_width = TSEGeometry.calc_measure_scale_factor(200, (400 - 1))

        # Calculate the scaled indices to identify the pixels in the larger image that we will want to make GREEN to provide evidence for the test succeeding.
        original_image_scaled_indices = np.rint((np.arange(0, 200) * scale_factor_width)).astype(int)

        rows_cols_cartesian_product = np.hsplit(TSEDataUtils.calc_cartesian_product([original_image_scaled_indices, original_image_scaled_indices]), 2)

        rows_to_extract = rows_cols_cartesian_product[0].astype(int)
        cols_to_extract = rows_cols_cartesian_product[1].astype(int)

        # We now want to set each fo the pixels THAT WE EXPECT TO BE EXTRACTED BY THE TEST to GREEN to show that the test has passed.
        original_image[rows_to_extract, cols_to_extract] = [0, 200, 0]

        # Once we have performed the pixel extraction, we expect that all of the pixels returned will be GREEN (based ont he setup above)
        matching_image = np.full((200, 200, 3), [0, 200, 0],  dtype=np.uint8)

        non_matching_image = np.full((200, 200, 3), [200, 0, 0],  dtype=np.uint8)

        # Check that for perfectly matching images, we get a score of exactly 0.
        assert_equal(TSECImageUtils.calc_ssd_slow(matching_image, original_image, matching_image.shape[0], matching_image.shape[1], scale_factor_width, scale_factor_width), 0)

        # Check that for non-matching images, we get a score > 0.
        assert_true(TSECImageUtils.calc_ssd_slow(non_matching_image, original_image, matching_image.shape[0], matching_image.shape[1], scale_factor_width, scale_factor_width) > 0)
コード例 #3
0
    def test_calc_vec_magnitude(self):

        point_1 = (10, 20)
        point_2 = (50, 5)

        calculated_vector = ((point_2[0] - point_1[0]),
                             (point_2[1] - point_1[1]))

        expected_result = math.sqrt((calculated_vector[0]**2) +
                                    (calculated_vector[1]**2))

        assert_equal(TSEGeometry.calc_vec_magnitude(point_1, point_2),
                     expected_result)
コード例 #4
0
    def test_scale_coordinate_relative_centre(self):

        origin_point = (0, 0)
        centre_point = (10, 10)

        scale_factor = 20

        scaled_x = centre_point[0] + (
            (origin_point[0] - centre_point[0]) * scale_factor)
        scaled_y = centre_point[1] + (
            (origin_point[1] - centre_point[1]) * scale_factor)

        expected_result = (scaled_x, scaled_y)

        assert_equal(
            TSEGeometry.scale_coordinate_relative_centre(
                origin_point, centre_point, scale_factor), expected_result)
コード例 #5
0
    def test_calc_line_points(self):

        startpoint_1 = (75, 0)
        endpoint_1 = (0, 5)

        startpoint_2 = (225, 0)
        endpoint_2 = (300, 5)

        expected_origin_line_1 = [(75, 0), (60, 1), (45, 2), (30, 3), (15, 4),
                                  (0, 5)]
        expected_origin_line_2 = [(225, 0), (240, 1), (255, 2), (270, 3),
                                  (285, 4), (300, 5)]

        calculated_lines = TSEGeometry.calc_line_points(
            startpoint_1, endpoint_1, startpoint_2, endpoint_2, endpoint_2[1])

        assert_true(np.array_equal(calculated_lines[0],
                                   expected_origin_line_1))
        assert_true(np.array_equal(calculated_lines[1],
                                   expected_origin_line_2))
コード例 #6
0
    def test_calc_line_points_reflection(self):

        image_width = 300
        image_x_centre = image_width / 2

        origin_startpoint = (75, 0)
        origin_endpoint = (0, 5)

        expected_origin_line = [(75, 0), (60, 1), (45, 2), (30, 3), (15, 4),
                                (0, 5)]
        expected_origin_line_reflected = [(225, 0), (240, 1), (255, 2),
                                          (270, 3), (285, 4), (300, 5)]

        calculated_lines = TSEGeometry.calc_line_points_horizontal_reflection(
            origin_startpoint, origin_endpoint, image_x_centre,
            origin_endpoint[1])

        assert_true(np.array_equal(calculated_lines[0], expected_origin_line))
        assert_true(
            np.array_equal(calculated_lines[1],
                           expected_origin_line_reflected))
コード例 #7
0
    def test_calc_line_points_straight_line(self):

        startpoint_1 = (75, 0)
        endpoint_1 = (75, 5)

        startpoint_2 = (225, 0)
        endpoint_2 = (225, 5)

        expected_origin_straight_line_1 = [(75, 0), (75, 1), (75, 2), (75, 3),
                                           (75, 4), (75, 5)]
        expected_origin_straight_line_2 = [(225, 0), (225, 1), (225, 2),
                                           (225, 3), (225, 4), (225, 5)]

        calculated_straight_lines = TSEGeometry.calc_line_points(
            startpoint_1, endpoint_1, startpoint_2, endpoint_2, endpoint_2[1])

        assert_true(
            np.array_equal(calculated_straight_lines[0],
                           expected_origin_straight_line_1))
        assert_true(
            np.array_equal(calculated_straight_lines[1],
                           expected_origin_straight_line_2))
コード例 #8
0
    def test_scale_image_interpolation_man(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)

        # Calculate the scale factor based on the widths of the two images (as the width/height are equal, we can just use the width)
        scale_factor = TSEGeometry.calc_measure_scale_factor(
            original_image.shape[1], (larger_target_image.shape[1]))

        scaled_result = TSEImageUtils.scale_image_interpolation_man(
            original_image, scale_factor)

        # 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]))
コード例 #9
0
 def test_constructor(self):
     test_geometry = TSEGeometry()
     assert_true(test_geometry is not None)
コード例 #10
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