-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblurred_images.py
69 lines (57 loc) · 2.56 KB
/
blurred_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import shutil
import cv2
import argparse
import numpy as np
import albumentations as albu
def make_dir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
def replace_dir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
else:
shutil.rmtree(dir)
os.makedirs(dir)
def get_args_parser():
parser = argparse.ArgumentParser('Image Blurring', add_help=False)
parser.add_argument('--src_dir', default="", type=str,
help='source directory for images needed to be blurred.')
parser.add_argument('--dst_dir', default="", type=str,
help='destination directory for blurred images to be saved.')
parser.add_argument('--method', default='gaussian', type=str,
help='blurring method (gaussian, mean, median, motion, defocus).')
parser.add_argument('--sigma', default=1.1, type=float,
help='blurriness')
parser.add_argument('--seed', default=0, type=int)
return parser
def main(args):
np.random.seed(args.seed)
make_dir(args.dst_dir)
for dir_1 in os.listdir(args.src_dir):
make_dir(os.path.join(args.dst_dir, dir_1))
for dir_2 in os.listdir(os.path.join(args.src_dir, dir_1)):
make_dir(os.path.join(args.dst_dir, dir_1, dir_2))
for file_name in os.listdir(os.path.join(args.src_dir, dir_1, dir_2)):
img = cv2.imread(os.path.join(args.src_dir, dir_1, dir_2, file_name),
cv2.IMREAD_GRAYSCALE)
if args.method == "gaussian":
img_blured = cv2.GaussianBlur(img, (0, 0), args.sigma)
elif args.method == "mean":
img_blured = cv2.blur(img, (5, 5))
elif args.method == "median":
img_blured = cv2.medianBlur(img, (5, 5))
elif args.method == "motion":
transform = albu.Compose([
albu.MotionBlur(blur_limit=(5, 5), p=1), ], p=1)
img_blured = transform(image=img)['image']
elif args.method == "defocus":
transform = albu.Compose([
albu.Defocus(radius=(5, 5), alias_blur=(0.01, 0.01),
always_apply=True, p=1),], p=1)
img_blured = transform(image=img)['image']
cv2.imwrite(os.path.join(args.dst_dir, dir_1, dir_2, file_name), img_blured)
if __name__ == '__main__':
args = get_args_parser()
args = args.parse_args()
main(args)