Ejemplo n.º 1
0
    def estimate_transform(self,
                           image_left,
                           image_right,
                           angle_left=0,
                           angle_right=0):
        """Estimate transformation for stitching of images based on 'rectangle' Stitching.

        Args:
            image_left (ndarray): Input left image.
            image_right (ndarray): Input right image.
            angle_left (int): Angle in degree to rotate left image.
            angle_right (int): Angle in degree to rotate right image.
        """
        # TODO(gitmirgut) set all to False
        self.size_left = image_left.shape[:2][::-1]
        self.size_right = image_right.shape[:2][::-1]

        image_left, affine_left = self._prepare_image(image_left, angle_left)
        image_right, affine_right = self._prepare_image(
            image_right, angle_right)

        pt_picker = picker.PointPicker()
        pts_left, pts_right = pt_picker.pick([image_left, image_right], False)
        assert len(pts_left) == 4 and len(pts_right) == 4
        pts_left_srt = helpers.sort_pts(pts_left)
        pts_right_srt = helpers.sort_pts(pts_right)

        target_pts_left = helpers.raw_estimate_rect(pts_left_srt)
        target_pts_right = helpers.raw_estimate_rect(pts_right_srt)
        target_pts_left, target_pts_right = helpers.harmonize_rects(
            target_pts_left, target_pts_right)

        # declare the shift of the right points
        shift_right = np.amax(target_pts_left[:, 0])
        target_pts_right[:, 0] = target_pts_right[:, 0] + shift_right
        homo_left, __ = cv2.findHomography(pts_left_srt, target_pts_left)
        homo_right, __ = cv2.findHomography(pts_right_srt, target_pts_right)

        # calculate the overall homography including the previous rotation
        homo_left = homo_left.dot(affine_left)
        homo_right = homo_right.dot(affine_right)

        # define translation matrix
        bounds = helpers.get_boundaries(self.size_left, self.size_right,
                                        homo_left, homo_right)
        homo_trans = helpers.get_transform_to_origin_mat(
            bounds.xmin, bounds.ymin)

        self.homo_left = homo_trans.dot(homo_left)
        self.homo_right = homo_trans.dot(homo_right)
Ejemplo n.º 2
0
def get_origin(image):
    """Determine origin of the image.

    The user must select one point on the image.
    The selected point will be used to define the origin of the image, this will assure that mapped
    coordinates will always be in relation to the origin and not to the image.

    Args:
        image (ndarray): Reference image.

    Returns:
        ndarray: origin (2,)
    """
    pt_picker = picker.PointPicker()
    points = pt_picker.pick([image], False)
    assert len(points[0]) == 1
    return points[0][0]
Ejemplo n.º 3
0
def test_pick_length(panorama, monkeypatch):
    def mockreturn(myself, image_list, all):
        points = np.array(
            [[94.43035126, 471.89889526], [5494.71777344, 471.83984375]],
            dtype=np.float32)
        return points

    monkeypatch.setattr(picker.PointPicker, 'pick', mockreturn)
    pt = picker.PointPicker()
    points = pt.pick([panorama], False)
    assert len(points) == 2
    start_point, end_point = points
    print(start_point.shape)
    distance_px = np.linalg.norm(end_point - start_point)
    distance_mm = 344
    px_to_mm = distance_mm / distance_px
    print(distance_px * px_to_mm)
Ejemplo n.º 4
0
def get_ratio(image):
    """Determine the ratio to convert from pixel to mm.

    The user must select two points on the image. The selected points, will be used to determine the
    ratio between px and mm.

    Args:
        image (ndarray): Reference image.

    Returns:
         float: Ratio to convert pixel to mm.
    """
    pt_picker = picker.PointPicker()
    points = pt_picker.pick([image], False)
    assert len(points[0]) == 2
    start_point, end_point = points[0]
    distance_mm = float(input('Distance in mm of the two selected points: '))
    ratio = helpers.get_ratio_px_to_mm(start_point, end_point, distance_mm)

    return ratio
Ejemplo n.º 5
0
    def _calc_image_to_world_mat(panorama):
        """Determine the matrix to convert image coordinates to world coordinates.

        The user must select two points on the image. The first point will be the origin and the
        distance between the first and the second point, will be used to determine the ratio
        between px and mm.

        Returns:
             ndarray: homography *(3,3)* to transform image points to world points.
        """
        pt_picker = picker.PointPicker()
        points = pt_picker.pick([panorama], False)
        start_point, end_point = points[0]
        distance_mm = float(
            input('Distance in mm of the two selected points: '))
        ratio = helpers.get_ratio_px_to_mm(start_point, end_point, distance_mm)

        # define matrix to convert image coordinates to world coordinates
        homo_to_world = np.array(
            [[ratio, 0, start_point[0]], [0, ratio, end_point[1]], [0, 0, 1]],
            dtype=np.float32)

        return homo_to_world
Ejemplo n.º 6
0
def test_gui(left_img_prep, right_img_prep):
    # TODO(gitmirgut): better gui test...
    pt = picker.PointPicker()
    print(pt)