def apply(self, img, blur_type, **params): ksize = random.choice( np.arange(self.blur_limit[0], self.blur_limit[1] + 1, 2)) # print('ksize:', ksize) # blur_type = random.randint(0, 90) # blur_type = 1 if blur_type < 10: img = F.motion_blur(img, kernel=self.get_motion_kernel()) elif blur_type < 20: img = F.blur(img, random.choice(range(3, 16, 2))) elif blur_type < 30: img = F.median_blur(img, random.choice(range(3, 16, 2))) elif blur_type < 40: img = F.gaussian_blur(img, ksize) elif blur_type < 50: img = ghosting(img, random.uniform(0.01, 0.1), angle=random.randint(-7, 7), scale=random.uniform(0., 0.1)) elif blur_type < 60: kernel, anchor = genaratePsf(random.randint(5, 30), random.randint(-90, 90)) img = cv2.filter2D(img, -1, kernel, anchor=anchor) elif blur_type < 70: # 速度会很慢 if random.random() < 0.5: radial_num = random.randint(8, 30) radial_type = 'angle' else: radial_num = random.randint(5, 13) radial_type = 'direction' if os.name == 'posix': h, w, c = img.shape img_arr = img.ctypes.data_as(c_char_p) clib.apply_radial_blur(img_arr, h, w, radial_num, 0 if radial_type == 'angle' else 1) else: img = apply_radial_blur(img, num=radial_num, type=radial_type) elif blur_type < 80: if blur_type < 75: img = F.gaussian_blur(img, random.choice(range(3, 9, 2))) img = F.image_compression(img, random.randint(0, 15), '.jpg') elif blur_type < 85: img = gasuss_noise(img, 0, random.uniform(0.05, 0.15)) elif blur_type < 90: img = apply_resize(img, random.randint(2, 7)) else: img = F.gaussian_blur(img, ksize) end_plus = random.random() if end_plus < 0.05: img = F.gaussian_blur(img, random.choice(range(3, 9, 2))) elif end_plus < 0.1: kernel, anchor = genaratePsf(random.randint(3, 10), random.randint(-90, 90)) img = cv2.filter2D(img, -1, kernel, anchor=anchor) return img
def ghosting(img, dis, alpha=0.5, angle=0, scale=0): dx = random.uniform(-dis, dis) dy = dis - abs(dx) dy = random.choice([-dy, dy]) tmp1 = F.shift_scale_rotate(img, angle, scale + 1, dx, dy, value=0) if random.random() < 0.5: tmp1 = F.gaussian_blur(tmp1, 3) tmp2 = img if random.random() < 0.5: tmp2 = F.gaussian_blur(tmp2, 3) beta = 1 - alpha gamma = 0 return cv2.addWeighted(tmp1, alpha, tmp2, beta, gamma)
def test_gaus_blur_limits(blur_limit, sigma, result_blur, result_sigma): img = np.zeros([100, 100, 3], dtype=np.uint8) aug = A.Compose([A.GaussianBlur(blur_limit=blur_limit, sigma_limit=sigma, p=1)]) res = aug(image=img)["image"] assert np.allclose(res, F.gaussian_blur(img, result_blur, result_sigma))
def applyBlur(img): blur_type = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9]) level = np.random.choice([0, 1, 2, 3, 4, 5], size=2, replace=False) # blur_type = 1 if blur_type == 1: l_value = [0, 3, 7, 9, 11, 13] img_first = img.copy() if level[0] == 0 else F.motion_blur( img, kernel=get_motion_kernel(l_value[level[0]])) img_second = img if level[1] == 0 else F.motion_blur( img, kernel=get_motion_kernel(l_value[level[1]])) elif blur_type == 2: l_value = [0, 3, 5, 7, 9, 11] img_first = img.copy() if level[0] == 0 else F.blur( img, l_value[level[0]]) img_second = img if level[1] == 0 else F.blur(img, l_value[level[1]]) elif blur_type == 3: l_value = [0, 3, 5, 7, 9, 11] img_first = img.copy() if level[0] == 0 else F.median_blur( img, l_value[level[0]]) img_second = img if level[1] == 0 else F.median_blur( img, l_value[level[1]]) elif blur_type == 4: l_value = [0, 5, 9, 11, 15, 17] img_first = img.copy() if level[0] == 0 else F.gaussian_blur( img, l_value[level[0]]) img_second = img if level[1] == 0 else F.gaussian_blur( img, l_value[level[1]]) elif blur_type == 5: l_value = [0, 5, 11, 17, 23, 25] kernel_first, anchor_first = genaratePsf(l_value[level[0]], random.randint(-90, 90)) kernel_second, anchor_second = genaratePsf(l_value[level[1]], random.randint(-90, 90)) img_first = img.copy() if level[0] == 0 else cv2.filter2D( img, -1, kernel_first, anchor=anchor_first) img_second = img if level[1] == 0 else cv2.filter2D( img, -1, kernel_second, anchor=anchor_second) elif blur_type == 6: l_value = [0, 20, 15, 10, 5, 0] img_first = img.copy() if level[0] == 0 else F.image_compression( img, l_value[level[0]], '.jpg') img_second = img if level[1] == 0 else F.image_compression( img, l_value[level[1]], '.jpg') elif blur_type == 7: l_value = [0, 0.04, 0.05, 0.06, 0.07, 0.08] img_first = img.copy() if level[0] == 0 else ghosting( img, l_value[level[0]]) img_second = img if level[1] == 0 else ghosting(img, l_value[level[1]]) elif blur_type == 8: if random.random() < 0.3: l_value = [0, 8, 12, 15, 20, 25] radial_type = 'angle' else: l_value = [0, 5, 7, 9, 11, 12] radial_type = 'direction' if os.name == 'posix': h, w, c = img.shape img_first = img.copy() img_second = img if level[0] != 0: img_arr_f = img_first.ctypes.data_as(c_char_p) clib.apply_radial_blur(img_arr_f, h, w, l_value[level[0]], 0 if radial_type == 'angle' else 1) if level[1] != 0: img_arr_s = img_second.ctypes.data_as(c_char_p) clib.apply_radial_blur(img_arr_s, h, w, l_value[level[1]], 0 if radial_type == 'angle' else 1) else: img_first = img.copy() if level[0] == 0 else apply_radial_blur( img.copy(), num=l_value[level[0]], type=radial_type) img_second = img if level[1] == 0 else apply_radial_blur( img, num=l_value[level[1]], type=radial_type) else: l_value = [0, 0.025, 0.05, 0.075, 0.1, 0.125] img_first = img.copy() if level[0] == 0 else gasuss_noise( img, 0, l_value[level[0]]) img_second = img if level[1] == 0 else gasuss_noise( img, 0, l_value[level[1]]) return img_first, img_second, 1 if level[0] > level[1] else -1