File size: 2,629 Bytes
92f4b97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
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))