Ejemplo n.º 1
0
def _extract_spatial_features(image: Image) -> np.ndarray:
    shape = Shape.from_image(image)
    features: List[np.ndarray] = []

    while shape.height >= 2 and shape.width >= 2:
        shape = Shape(shape.height / 2, shape.width / 2)
        shape_int = (int(shape.height), int(shape.width))
        features += [cv2.resize(image, shape_int).ravel()]

    return np.concatenate(features)
Ejemplo n.º 2
0
def resize_image_to_size(image: ImageT,
                         target_size: Shape,
                         interpolation_method: int = cv2.INTER_AREA) -> ImageT:
    image_size = Shape.from_image(image)
    if image_size.width <= 0 or image_size.height <= 0:
        raise ImageResizingException('image shape is invalid')
    if target_size.width <= 0 or target_size.height <= 0:
        raise ImageResizingException('target_size is invalid')

    return cv2.resize(image, (target_size.width, target_size.height),
                      interpolation_method)
Ejemplo n.º 3
0
def test_scale_around_corners_maintains_corners(bbox: BoundingBox, sx: float,
                                                sy: float) -> None:
    scaled_bbox = bbox.scale(Shape(sx, sy),
                             center=Point(bbox.x_min, bbox.y_min))
    assert scaled_bbox.x_min == approx(bbox.x_min, abs=1e-6)
    assert scaled_bbox.y_min == approx(bbox.y_min, abs=1e-6)

    scaled_bbox = bbox.scale(Shape(sx, sy),
                             center=Point(bbox.x_min, bbox.y_max))
    assert scaled_bbox.x_min == approx(bbox.x_min, abs=1e-6)
    assert scaled_bbox.y_max == approx(bbox.y_max, abs=1e-6)

    scaled_bbox = bbox.scale(Shape(sx, sy),
                             center=Point(bbox.x_max, bbox.y_min))
    assert scaled_bbox.x_max == approx(bbox.x_max, abs=1e-6)
    assert scaled_bbox.y_min == approx(bbox.y_min, abs=1e-6)

    scaled_bbox = bbox.scale(Shape(sx, sy),
                             center=Point(bbox.x_max, bbox.y_max))
    assert scaled_bbox.x_max == approx(bbox.x_max, abs=1e-6)
    assert scaled_bbox.y_max == approx(bbox.y_max, abs=1e-6)
Ejemplo n.º 4
0
 def test_resize_empty_image_to_size_raises(
         self, empty_images: List[Image]) -> None:
     for empty_image in empty_images:
         with pytest.raises(ImageResizingException):
             resize_image_to_size(empty_image, Shape(30, 30))
Ejemplo n.º 5
0
class TestResizeImageToSize():
    @pytest.mark.parametrize(
        'size',
        [Shape(30, 20),
         Shape(100, 50),
         Shape(100, 100),
         Shape(2000, 5000)])
    def test_resize_image_to_valid_size(self, all_valid_images: List[Image],
                                        size: Shape) -> None:
        for image in all_valid_images:
            resized_image = resize_image_to_size(image, size)
            assert (resized_image.shape[:2] == (size.height, size.width))
            assert (len(resized_image.shape) == len(image.shape))

    @pytest.mark.parametrize('size', [
        Shape(-100, -100),
        Shape(-100, 1),
        Shape(1, -100),
        Shape(0, 0),
        Shape(1, 0),
        Shape(0, 1),
        Shape(-100, 100),
        Shape(100, -100)
    ])
    def test_resize_image_to_invalid_size_raises(self,
                                                 all_valid_images: List[Image],
                                                 size: Shape) -> None:
        for image in all_valid_images:
            with pytest.raises(ImageResizingException):
                resize_image_to_size(image, size)

    def test_resize_empty_image_to_size_raises(
            self, empty_images: List[Image]) -> None:
        for empty_image in empty_images:
            with pytest.raises(ImageResizingException):
                resize_image_to_size(empty_image, Shape(30, 30))
Ejemplo n.º 6
0
def test_scale_around_center_maintains_center(bbox: BoundingBox, sx: float,
                                              sy: float) -> None:
    scaled_bbox = bbox.scale(Shape(sx, sy), center=bbox.center)
    assert scaled_bbox.center.x == approx(bbox.center.x, abs=1e-6)
    assert scaled_bbox.center.y == approx(bbox.center.y, abs=1e-6)
Ejemplo n.º 7
0
def test_scale_around_center_changes_size(bbox: BoundingBox, sx: float,
                                          sy: float) -> None:
    scaled_bbox = bbox.scale(Shape(sx, sy), center=bbox.center)
    assert scaled_bbox.delta_x == approx(bbox.delta_x * sx, abs=1e-6)
    assert scaled_bbox.delta_y == approx(bbox.delta_y * sy, abs=1e-6)
Ejemplo n.º 8
0
def test_scale_around_origin_moves_center(bbox: BoundingBox, sx: float,
                                          sy: float) -> None:
    scaled_bbox = bbox.scale(Shape(sx, sy))
    assert scaled_bbox.center.x == approx(bbox.center.x * sx, abs=1e-6)
    assert scaled_bbox.center.y == approx(bbox.center.y * sy, abs=1e-6)