FetReg / upstream_FetReg2021_segmentation_visualisation.py
Parth1503's picture
Upload upstream_FetReg2021_segmentation_visualisation.py with huggingface_hub
92f4b97 verified
Raw
History Blame Contribute Delete
2.63 kB
"""
Fetoscopy placental vessel segmentation and registration challenge (FetReg)
EndoVis - MICCAI2021
Challenge link: https://www.synapse.org/#!Synapse:syn25313156
Visualization script for image and mask for the semantic segmentation task
"""
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
def get_colormap():
"""
Returns FetReg colormap
"""
colormap = np.asarray(
[
[0, 0, 0], # 0 - background
[255, 0, 0], # 1 - vessel
[0, 0, 255], # 2 - tool
[0, 255, 0], # 3 - fetus
]
)
return colormap
def plot_image_n_label(img_path_fname, mask_path_fname):
"""
Plot of image and RGB mask for visualisation
Params
img_path_fname : Input image path
mask_path_fname: Input segmentation mask path
Return
plot of image and RGB mask
"""
img = cv2.imread(img_path_fname)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
mask = cv2.imread(mask_path_fname, cv2.COLOR_BGR2GRAY)
colormap = get_colormap()
mask_rgb = np.zeros(mask.shape[:2] + (3,), dtype=np.uint8)
for cnt in range(len(colormap)):
mask_rgb[mask == cnt] = colormap[cnt]
fig, axs = plt.subplots(1, 2, figsize=(14, 7))
axs[0].imshow(img)
axs[0].axis("off")
axs[1].imshow(mask_rgb)
axs[1].axis("off")
fig.tight_layout()
#plt.show()
return fig
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--root", help="Path to root (video) folder that contains images and labels subfolders", required=True)
parser.add_argument("--output", help="Output path to save plot", required=True)
args = parser.parse_args()
assert os.path.isdir(args.root), f"{args.root} directory does not exist"
img_path = os.path.join(args.root, 'images')
mask_path = os.path.join(args.root, 'labels')
assert os.path.exists(img_path), f"{img_path} images/labels do not exist."
assert os.path.exists(mask_path), f"{mask_path} images/labels do not exist."
Img_list = np.sort(os.listdir(img_path)) # List all image names
for cnt in range(len(Img_list)):
fname = Img_list[cnt]
img_path_fname = os.path.join(img_path, fname)
mask_path_fname = os.path.join(mask_path, fname)
fig = plot_image_n_label(img_path_fname, mask_path_fname)
if not os.path.isdir(args.output):
os.makedirs(args.output)
fname2 = fname.replace('png','jpg')
fig.savefig(os.path.join(args.output, fname2))