Ejemplo n.º 1
0
 def test_missing_transform(self):
     """
     get coordinate returns a MissingTransformError when requested a coordinate in a frame for which the transform is unknown
     """
     transform = Transform('origin', 'local', translation=[0, 0])
     point1 = Point2D([400, 100], 'origin', transform)
     self.assertRaises(MissingTransformError, point1.get_coord, 'space')
Ejemplo n.º 2
0
    def test_translation(self):
        transform = Transform('origin', 'local', translation=[3, 4])

        point1 = Point2D([0, 0], 'origin', transform)
        point2 = Point2D([0, 0], 'local', transform)

        np.testing.assert_almost_equal(point1.get_coord('origin'), [0, 0])
        np.testing.assert_almost_equal(point1.get_coord('local'), [-3, -4])

        np.testing.assert_almost_equal(point2.get_coord('origin'), [3, 4])
        np.testing.assert_almost_equal(point2.get_coord('local'), [0, 0])

        np.testing.assert_almost_equal(point1.dist(point2), 5)
Ejemplo n.º 3
0
    def test_90deg_rotation(self):

        shape = [20, 10]  # [width, height]
        angle = np.pi / 2
        transform = Transform('origin', 'local', dim=shape, angle=angle)

        point1 = Point2D([10, 5], 'origin', transform)
        point2 = Point2D([5, 10], 'local', transform)

        np.testing.assert_almost_equal(point1.get_coord('origin'), [10, 5])
        np.testing.assert_almost_equal(point1.get_coord('local'), [5, 10])

        np.testing.assert_almost_equal(point2.get_coord('origin'), [10, 5])
        np.testing.assert_almost_equal(point2.get_coord('local'), [5, 10])

        np.testing.assert_almost_equal(point1.dist(point2), 0)
Ejemplo n.º 4
0
    def test_transform_forwards_backwards(self):
        """
        get coordinate return the original coordinate after translating forwards and backwards
        """
        shape = [1000, 400]  # [width, height]
        translation = [50, -10]
        for angle_rad in np.arange(-np.pi, np.pi, 10):

            transform = Transform('origin', 'local', shape, angle_rad,
                                  translation)
            point1 = Point2D([400, 100], 'origin', transform)

            coord1 = point1.get_coord('local')
            point2 = Point2D(coord1, 'local', transform)

            np.testing.assert_almost_equal(point1.get_coord('origin'),
                                           point2.get_coord('origin'))
Ejemplo n.º 5
0
    def test_345_rotation(self):

        shape = [8, 6]  # [width, height]
        angle = -np.arctan2(3, 4)
        transform = Transform('origin', 'local', dim=shape, angle=angle)

        point1 = Point2D([4, 3], 'origin', transform)
        point2 = Point2D([5, 3.0 / 5.0 * 8], 'local', transform)

        np.testing.assert_almost_equal(point1.get_coord('origin'), [4, 3])
        np.testing.assert_almost_equal(point1.get_coord('local'),
                                       [5, 3.0 / 5.0 * 8])

        np.testing.assert_almost_equal(point2.get_coord('origin'), [4, 3])
        np.testing.assert_almost_equal(point2.get_coord('local'),
                                       [5, 3.0 / 5.0 * 8])

        np.testing.assert_almost_equal(point1.dist(point2), 0)
Ejemplo n.º 6
0
    def rotate_cut_img(self):
        """crop the image"""
        pwd = os.path.join(self.pwd, '04_crop')

        if self.peduncle.sum() == 0:
            warnings.warn("Cannot rotate based on peduncle, since it does not exist!")
            angle = 0
        else:
            angle = imgpy.compute_angle(self.peduncle)  # [rad]

        tomato_rotate = imgpy.rotate(self.tomato, -angle)
        peduncle_rotate = imgpy.rotate(self.peduncle, -angle)
        truss_rotate = imgpy.add(tomato_rotate, peduncle_rotate)

        if truss_rotate.sum() == 0:
            warnings.warn("Cannot crop based on truss segment, since it does not exist!")
            return False

        bbox = imgpy.bbox(truss_rotate)
        x = bbox[0]  # col
        y = bbox[1]  # rows to upper left corner

        translation = [x, y]
        xy_shape = [self.shape[1], self.shape[0]] # [width, height]
        self.transform = Transform(self.ORIGINAL_FRAME_ID, self.LOCAL_FRAME_ID, xy_shape, angle=-angle,
                                   translation=translation)

        self.bbox = bbox
        self.angle = angle

        self.tomato_crop = imgpy.cut(tomato_rotate, self.bbox)
        self.peduncle_crop = imgpy.cut(peduncle_rotate, self.bbox)
        self.img_rgb_crop = imgpy.crop(self.img_rgb, angle=-angle, bbox=bbox)
        self.truss_crop = imgpy.cut(truss_rotate, self.bbox)

        if self.save:
            img_rgb = self.get_rgb(local=True)
            save_img(img_rgb, pwd=pwd, name=self.name)