Parth1503 commited on
Commit
92f4b97
·
verified ·
1 Parent(s): 44d58b3

Upload upstream_FetReg2021_segmentation_visualisation.py with huggingface_hub

Browse files
upstream_FetReg2021_segmentation_visualisation.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Fetoscopy placental vessel segmentation and registration challenge (FetReg)
3
+ EndoVis - MICCAI2021
4
+ Challenge link: https://www.synapse.org/#!Synapse:syn25313156
5
+ Visualization script for image and mask for the semantic segmentation task
6
+ """
7
+
8
+ import os
9
+ import cv2
10
+ import numpy as np
11
+ import matplotlib.pyplot as plt
12
+
13
+
14
+ def get_colormap():
15
+ """
16
+ Returns FetReg colormap
17
+ """
18
+ colormap = np.asarray(
19
+ [
20
+ [0, 0, 0], # 0 - background
21
+ [255, 0, 0], # 1 - vessel
22
+ [0, 0, 255], # 2 - tool
23
+ [0, 255, 0], # 3 - fetus
24
+
25
+ ]
26
+ )
27
+ return colormap
28
+
29
+ def plot_image_n_label(img_path_fname, mask_path_fname):
30
+ """
31
+ Plot of image and RGB mask for visualisation
32
+ Params
33
+ img_path_fname : Input image path
34
+ mask_path_fname: Input segmentation mask path
35
+ Return
36
+ plot of image and RGB mask
37
+ """
38
+
39
+ img = cv2.imread(img_path_fname)
40
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
41
+ mask = cv2.imread(mask_path_fname, cv2.COLOR_BGR2GRAY)
42
+
43
+ colormap = get_colormap()
44
+
45
+ mask_rgb = np.zeros(mask.shape[:2] + (3,), dtype=np.uint8)
46
+ for cnt in range(len(colormap)):
47
+ mask_rgb[mask == cnt] = colormap[cnt]
48
+
49
+
50
+ fig, axs = plt.subplots(1, 2, figsize=(14, 7))
51
+ axs[0].imshow(img)
52
+ axs[0].axis("off")
53
+
54
+
55
+ axs[1].imshow(mask_rgb)
56
+ axs[1].axis("off")
57
+ fig.tight_layout()
58
+ #plt.show()
59
+
60
+ return fig
61
+
62
+
63
+ if __name__ == "__main__":
64
+ import argparse
65
+
66
+ parser = argparse.ArgumentParser()
67
+ parser.add_argument("--root", help="Path to root (video) folder that contains images and labels subfolders", required=True)
68
+ parser.add_argument("--output", help="Output path to save plot", required=True)
69
+ args = parser.parse_args()
70
+
71
+ assert os.path.isdir(args.root), f"{args.root} directory does not exist"
72
+
73
+ img_path = os.path.join(args.root, 'images')
74
+ mask_path = os.path.join(args.root, 'labels')
75
+ assert os.path.exists(img_path), f"{img_path} images/labels do not exist."
76
+ assert os.path.exists(mask_path), f"{mask_path} images/labels do not exist."
77
+
78
+
79
+ Img_list = np.sort(os.listdir(img_path)) # List all image names
80
+
81
+
82
+ for cnt in range(len(Img_list)):
83
+ fname = Img_list[cnt]
84
+ img_path_fname = os.path.join(img_path, fname)
85
+ mask_path_fname = os.path.join(mask_path, fname)
86
+
87
+ fig = plot_image_n_label(img_path_fname, mask_path_fname)
88
+ if not os.path.isdir(args.output):
89
+ os.makedirs(args.output)
90
+ fname2 = fname.replace('png','jpg')
91
+ fig.savefig(os.path.join(args.output, fname2))
92
+