def test_elastic_transform_interpolation(monkeypatch, interpolation): image = np.random.randint(low=0, high=256, size=(100, 100, 3), dtype=np.uint8) mask = np.random.randint(low=0, high=2, size=(100, 100), dtype=np.uint8) monkeypatch.setattr( 'albumentations.augmentations.transforms.ElasticTransform.get_params', lambda *_: {'random_state': 1111}) aug = ElasticTransform(alpha=1, sigma=50, alpha_affine=50, interpolation=interpolation, p=1) data = aug(image=image, mask=mask) expected_image = F.elastic_transform_fast( image, alpha=1, sigma=50, alpha_affine=50, interpolation=interpolation, border_mode=cv2.BORDER_REFLECT_101, random_state=np.random.RandomState(1111)) expected_mask = F.elastic_transform_fast( mask, alpha=1, sigma=50, alpha_affine=50, interpolation=cv2.INTER_NEAREST, border_mode=cv2.BORDER_REFLECT_101, random_state=np.random.RandomState(1111)) assert np.array_equal(data['image'], expected_image) assert np.array_equal(data['mask'], expected_mask)
def spacialAg(img1,img2): # 水平翻转 if np.random.random() < 0.5: img1 = functional.hflip(img1) img2 = functional.hflip(img2) # 垂直翻转 if np.random.random() < 0.5: img1 = functional.vflip(img1) img2 = functional.vflip(img2) # 绕图像中心旋转 if np.random.random() < 0.5: angle = np.random.uniform(-20, 20) scale = np.random.uniform(1 - 0.1, 1 + 0.1) dx = np.random.uniform(-0.0625, 0.0625) dy = np.random.uniform(-0.0625, 0.0625) img1 = functional.shift_scale_rotate(img1, angle, scale, dx, dy, interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_CONSTANT) img2 = functional.shift_scale_rotate(img2, angle, scale, dx, dy, interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_CONSTANT) # 网格扭曲 if np.random.random() < 0.5: num_steps = 5 distort_limit = (-0.3, 0.3) stepsx = [1 + np.random.uniform(distort_limit[0], distort_limit[1]) for i in range(num_steps + 1)] stepsy = [1 + np.random.uniform(distort_limit[0], distort_limit[1]) for i in range(num_steps + 1)] img1 = functional.grid_distortion(img1, num_steps, stepsx, stepsy, interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_CONSTANT) img2 = functional.grid_distortion(img2, num_steps, stepsx, stepsy, interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_CONSTANT) # 弹性扭曲 if np.random.random() < 0.5: alpha = 1 sigma = 50 alpha_affine = 50 interpolation = cv2.INTER_LINEAR random_state = np.random.randint(0, 10000) img1 = functional.elastic_transform_fast(img1, alpha, sigma, alpha_affine, interpolation, cv2.BORDER_CONSTANT, np.random.RandomState(random_state)) img2 = functional.elastic_transform_fast(img2, alpha, sigma, alpha_affine, interpolation, cv2.BORDER_CONSTANT, np.random.RandomState(random_state)) return img1, img2