Diff of /utils/visualizer.py [000000] .. [4cda31]

Switch to unified view

a b/utils/visualizer.py
1
# Manuel A. Morales (moralesq@mit.edu)
2
# Harvard-MIT Department of Health Sciences & Technology  
3
# Athinoula A. Martinos Center for Biomedical Imaging
4
5
import numpy as np
6
from skimage import measure
7
import matplotlib.pylab as plt
8
from scipy.ndimage.morphology import binary_fill_holes
9
10
def Contours(mask, tissue_labels=[1,2,3]):
11
    contours = []
12
    for i in tissue_labels:
13
        mask_ = binary_fill_holes(mask==i)
14
        c = measure.find_contours(mask_,0.8)
15
        c = c[np.argmax([len(c) for c in c])]
16
        contours.append(c)
17
    return contours
18
19
def PlotContours(ax, mask, tissue_labels=[1,2,3], 
20
                 contour_colors=['lime','magenta','red'],
21
                 contour_labels=['RV','LVM','LV'],
22
                 tolerance=0.1,
23
                 alpha=1,
24
                 linewidth=2,
25
                 legend=False):
26
    
27
    contours = Contours(mask, tissue_labels=tissue_labels)
28
    for i, contour in enumerate(contours):
29
        ax.plot(contour[:, 1], contour[:, 0],alpha=alpha, linewidth=linewidth, color=contour_colors[i],label=contour_labels[i])
30
    if legend:
31
        ax.legend(fontsize=26)  
32
        
33
        
34
def Plot(image, mask=None, figsize=(7,7), crop=False):
35
    
36
    if crop:
37
        nx, ny = image.shape
38
        image = image[nx//2-64:nx//2+64,ny//2-64:ny//2+64]
39
        if mask is not None:
40
            mask = mask[nx//2-64:nx//2+64,ny//2-64:ny//2+64]
41
    
42
    fig, ax = plt.subplots(1,1, figsize=figsize)
43
    ax.imshow(image, cmap='gray')
44
    if mask is not None:
45
        try:
46
            PlotContours(ax, mask, alpha=1, linewidth=1.3)  
47
        except:
48
            print('No contours found!')
49
            
50
51
            
52