-
Notifications
You must be signed in to change notification settings - Fork 0
/
black2white.py
35 lines (29 loc) · 1.08 KB
/
black2white.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
import cv2
import glob
import os
import matplotlib.pyplot as plt
# Code to change the black background into white background (Must Provide Mask)
#Masking Real data
dir = "/home/cgal/testing/black_bg/sample/*.png"
mask = "/home/cgal/testing/black_bg/input_matrix/test/masking/*.png"
tar = "/home/cgal/testing/white_bg/sample/"
if not os.path.exists(tar):
os.makedirs(tar)
for i,j in zip(sorted(glob.glob(dir)),sorted(glob.glob(mask))):
img = cv2.imread(i)
mask = cv2.imread(j)
mask = cv2.bitwise_not(mask)
iname = i.split('/')[-1].split('.')[0]
img[mask==255]=255
cv2.imwrite(str(tar+iname)+".png", img)
#Masking Synthetic
# dir = "/home/cgal/testing/black_bg/target_train_synt_matrix/*.png"
# tar = "/home/cgal/testing/white_bg/target_train_synt_matrix/"
# if not os.path.exists(tar):
# os.makedirs(tar)
# for i in sorted(glob.glob(dir)):
# img = cv2.imread(i)
# iname = i.split('/')[-1].split('.')[0]
# ret, thresh = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 10, 5, cv2.THRESH_BINARY)
# img[thresh == 0] = 255
# cv2.imwrite(str(tar+iname)+".png", img)