-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
71 lines (59 loc) · 2.22 KB
/
main.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
70
71
import yaml
import os
import cv2
import matplotlib.pyplot as plt
from matting import bayesian_matting
def load_imgs(img_name, IMAGE_FOLDER, TRIMAP_FOLDER, GT_FOLDER):
# reading the images and converting BGR image to RGB. Change image name for any other image
trimap1_path = os.path.join(TRIMAP_FOLDER, "Trimap1", img_name)
trimap2_path = os.path.join(TRIMAP_FOLDER, "Trimap2", img_name)
IMAGE_PATH = os.path.join(IMAGE_FOLDER, img_name)
trimap1 = cv2.imread(trimap1_path, cv2.IMREAD_GRAYSCALE)
trimap2 = cv2.imread(trimap2_path, cv2.IMREAD_GRAYSCALE)
input_image = cv2.imread(IMAGE_PATH)
input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
gt_path = os.path.join(GT_FOLDER, img_name)
gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE)
gt = gt / 255
return input_image, trimap1, trimap2, gt
def save_imgs(img, trimap, pred, gt, save_name):
plt.figure(figsize=(14, 10))
plt.subplot(2, 2, 1)
plt.title("Input Image")
plt.imshow(img)
plt.xticks([])
plt.yticks([])
plt.subplot(2, 2, 2)
plt.title("Combined Trimap")
plt.imshow(trimap, cmap="gray", vmin=0, vmax=255)
plt.xticks([])
plt.yticks([])
plt.subplot(2, 2, 3)
plt.title("Predicted Alpha Map")
plt.imshow(pred, cmap="gray", vmin=0, vmax=1)
plt.xticks([])
plt.yticks([])
plt.subplot(2, 2, 4)
plt.title("Actual Alpha Map")
plt.imshow(gt, cmap="gray", vmin=0, vmax=1)
plt.xticks([])
plt.yticks([])
plt.savefig(save_name)
if __name__ == "__main__":
with open("config.yaml", "r") as f:
config = yaml.safe_load(f)
IMAGE_NAME = config['img_name']
IMAGE_FOLDER = config['img_folder']
TRIMAP_FOLDER = config['trimap_folder']
GT_FOLDER = config['gt_folder']
SAVE_FOLDER = config['output_folder']
if not os.path.exists(SAVE_FOLDER):
os.mkdir(SAVE_FOLDER)
save_name = config['output_name']
save_name = os.path.join(SAVE_FOLDER, save_name)
image, trimap1, trimap2, gt = load_imgs(IMAGE_NAME, IMAGE_FOLDER, TRIMAP_FOLDER, GT_FOLDER)
pixels_to_consider = 35
combined_trimap, alpha_map = bayesian_matting(
image, trimap1, trimap2, pixels_to_consider
)
save_imgs(image, combined_trimap, alpha_map, gt, save_name)