def test_center_crop(target): img = np.array([[1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 0, 1]], dtype=np.uint8) expected = np.array([[1, 1], [0, 1]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) cropped_img = A.center_crop(img, 2, 2) assert np.array_equal(cropped_img, expected)
def test_shift_y_float_from_shift_scale_rotate(target): img = np.array( [ [0.01, 0.02, 0.03, 0.04], [0.05, 0.06, 0.07, 0.08], [0.09, 0.10, 0.11, 0.12], [0.13, 0.14, 0.15, 0.16], ], dtype=np.float32, ) expected = np.array( [ [0.00, 0.00, 0.00, 0.00], [0.00, 0.00, 0.00, 0.00], [0.01, 0.02, 0.03, 0.04], [0.05, 0.06, 0.07, 0.08], ], dtype=np.float32, ) img, expected = convert_2d_to_target_format([img, expected], target=target) shifted_along_y_img = F.shift_scale_rotate( img, angle=0, scale=1, dx=0, dy=0.5, interpolation=cv2.INTER_NEAREST, border_mode=cv2.BORDER_CONSTANT, ) assert_array_almost_equal_nulp(shifted_along_y_img, expected)
def test_rot90_float(target): img = np.array([[0.0, 0.0, 0.4], [0.0, 0.0, 0.4], [0.0, 0.0, 0.4]], dtype=np.float32) expected = np.array([[0.4, 0.4, 0.4], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], dtype=np.float32) img, expected = convert_2d_to_target_format([img, expected], target=target) rotated = F.rot90(img, factor=1) assert_array_almost_equal_nulp(rotated, expected)
def test_pad(target): img = np.array([[1, 2], [3, 4]], dtype=np.uint8) expected = np.array( [[4, 3, 4, 3], [2, 1, 2, 1], [4, 3, 4, 3], [2, 1, 2, 1]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) padded = F.pad(img, min_height=4, min_width=4) assert np.array_equal(padded, expected)
def test_pad_float(target): img = np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32) expected = np.array([[0.4, 0.3, 0.4, 0.3], [0.2, 0.1, 0.2, 0.1], [0.4, 0.3, 0.4, 0.3], [0.2, 0.1, 0.2, 0.1]], dtype=np.float32) img, expected = convert_2d_to_target_format([img, expected], target=target) padded_img = F.pad(img, min_height=4, min_width=4) assert_array_almost_equal_nulp(padded_img, expected)
def test_hflip_float(target): img = np.array([[0.4, 0.4, 0.4], [0.0, 0.4, 0.4], [0.0, 0.0, 0.4]], dtype=np.float32) expected = np.array([[0.4, 0.4, 0.4], [0.4, 0.4, 0.0], [0.4, 0.0, 0.0]], dtype=np.float32) img, expected = convert_2d_to_target_format([img, expected], target=target) flipped_img = F.hflip(img) assert_array_almost_equal_nulp(flipped_img, expected)
def test_center_crop_float(target): img = np.array([[0.4, 0.4, 0.4, 0.4], [0.0, 0.4, 0.4, 0.4], [0.0, 0.0, 0.4, 0.4], [0.0, 0.0, 0.0, 0.4]], dtype=np.float32) expected = np.array([[0.4, 0.4], [0.0, 0.4]], dtype=np.float32) img, expected = convert_2d_to_target_format([img, expected], target=target) cropped_img = A.center_crop(img, 2, 2) assert_array_almost_equal_nulp(cropped_img, expected)
def test_longest_max_size(target): img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], dtype=np.uint8) expected = np.array([[2, 3], [6, 7], [10, 11]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) scaled = FGeometric.longest_max_size(img, max_size=3, interpolation=cv2.INTER_LINEAR) assert np.array_equal(scaled, expected)
def test_resize_different_height_and_width(target): img = np.ones((100, 100), dtype=np.uint8) img = convert_2d_to_target_format([img], target=target) resized_img = FGeometric.resize(img, height=20, width=30) height, width = resized_img.shape[:2] assert height == 20 assert width == 30 if target == "image": num_channels = resized_img.shape[2] assert num_channels == 3
def test_resize_nearest_interpolation(target): img = np.array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]], dtype=np.uint8) expected = np.array([[1, 1], [3, 3]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) resized_img = FGeometric.resize(img, 2, 2, interpolation=cv2.INTER_NEAREST) height, width = resized_img.shape[:2] assert height == 2 assert width == 2 assert np.array_equal(resized_img, expected)
def test_resize_default_interpolation(target): img = np.array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]], dtype=np.uint8) expected = np.array([[2, 2], [4, 4]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) resized_img = F.resize(img, 2, 2) height, width = resized_img.shape[:2] assert height == 2 assert width == 2 assert np.array_equal(resized_img, expected)
def test_resize_default_interpolation_float(target): img = np.array([[0.1, 0.1, 0.1, 0.1], [0.2, 0.2, 0.2, 0.2], [0.3, 0.3, 0.3, 0.3], [0.4, 0.4, 0.4, 0.4]], dtype=np.float32) expected = np.array([[0.15, 0.15], [0.35, 0.35]], dtype=np.float32) img, expected = convert_2d_to_target_format([img, expected], target=target) resized_img = FGeometric.resize(img, 2, 2) height, width = resized_img.shape[:2] assert height == 2 assert width == 2 assert_array_almost_equal_nulp(resized_img, expected)
def test_resize_nearest_interpolation_float(target): img = np.array([[0.1, 0.1, 0.1, 0.1], [0.2, 0.2, 0.2, 0.2], [0.3, 0.3, 0.3, 0.3], [0.4, 0.4, 0.4, 0.4]], dtype=np.float32) expected = np.array([[0.1, 0.1], [0.3, 0.3]], dtype=np.float32) img, expected = convert_2d_to_target_format([img, expected], target=target) resized_img = FGeometric.resize(img, 2, 2, interpolation=cv2.INTER_NEAREST) height, width = resized_img.shape[:2] assert height == 2 assert width == 2 assert np.array_equal(resized_img, expected)
def test_smallest_max_size(target): img = np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]], dtype=np.uint8) expected = np.array([[2, 4, 5, 7], [10, 11, 13, 14], [17, 19, 20, 22]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) scaled = FGeometric.smallest_max_size(img, max_size=3, interpolation=cv2.INTER_LINEAR) assert np.array_equal(scaled, expected)
def test_random_crop(target): img = np.array( [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], dtype=np.uint8) expected = np.array([[5, 6], [9, 10]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) cropped_img = A.random_crop(img, crop_height=2, crop_width=2, h_start=0.5, w_start=0) assert np.array_equal(cropped_img, expected)
def test_random_crop_float(target): img = np.array( [[0.01, 0.02, 0.03, 0.04], [0.05, 0.06, 0.07, 0.08], [0.09, 0.10, 0.11, 0.12], [0.13, 0.14, 0.15, 0.16]], dtype=np.float32, ) expected = np.array([[0.05, 0.06], [0.09, 0.10]], dtype=np.float32) img, expected = convert_2d_to_target_format([img, expected], target=target) cropped_img = A.random_crop(img, crop_height=2, crop_width=2, h_start=0.5, w_start=0) assert_array_almost_equal_nulp(cropped_img, expected)
def test_shift_x_from_shift_scale_rotate(target): img = np.array( [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], dtype=np.uint8) expected = np.array( [[0, 0, 1, 2], [0, 0, 5, 6], [0, 0, 9, 10], [0, 0, 13, 14]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) shifted_along_x_img = F.shift_scale_rotate(img, angle=0, scale=1, dx=0.5, dy=0, interpolation=cv2.INTER_NEAREST, border_mode=cv2.BORDER_CONSTANT) assert np.array_equal(shifted_along_x_img, expected)
def test_scale_from_shift_scale_rotate(target): img = np.array( [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], dtype=np.uint8) expected = np.array( [[6, 7, 7, 8], [10, 11, 11, 12], [10, 11, 11, 12], [14, 15, 15, 16]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) scaled_img = FGeometric.shift_scale_rotate(img, angle=0, scale=2, dx=0, dy=0, interpolation=cv2.INTER_NEAREST, border_mode=cv2.BORDER_CONSTANT) assert np.array_equal(scaled_img, expected)
def test_scale(target): img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], dtype=np.uint8) expected = np.array( [ [1, 1, 2, 2, 3, 3], [2, 2, 2, 3, 3, 4], [3, 3, 4, 4, 5, 5], [5, 5, 5, 6, 6, 7], [6, 6, 7, 7, 8, 8], [8, 8, 8, 9, 9, 10], [9, 9, 10, 10, 11, 11], [10, 10, 11, 11, 12, 12], ], dtype=np.uint8, ) img, expected = convert_2d_to_target_format([img, expected], target=target) scaled = FGeometric.scale(img, scale=2, interpolation=cv2.INTER_LINEAR) assert np.array_equal(scaled, expected)
def test_random_flip_float(code, func, target): img = np.array([[0.4, 0.4, 0.4], [0.0, 0.4, 0.4], [0.0, 0.0, 0.4]], dtype=np.float32) img = convert_2d_to_target_format([img], target=target) assert_array_almost_equal_nulp(F.random_flip(img, code), func(img))
def test_random_flip(code, func, target): img = np.array([[1, 1, 1], [0, 1, 1], [0, 0, 1]], dtype=np.uint8) img = convert_2d_to_target_format([img], target=target) assert np.array_equal(F.random_flip(img, code), func(img))
def test_rot90(target): img = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]], dtype=np.uint8) expected = np.array([[1, 1, 1], [0, 0, 0], [0, 0, 0]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) rotated = F.rot90(img, factor=1) assert np.array_equal(rotated, expected)
def test_hflip(target): img = np.array([[1, 1, 1], [0, 1, 1], [0, 0, 1]], dtype=np.uint8) expected = np.array([[1, 1, 1], [1, 1, 0], [1, 0, 0]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) flipped_img = F.hflip(img) assert np.array_equal(flipped_img, expected)