Overlay an image segmentation with numpy and matplotlib

后端 未结 1 1850
予麋鹿
予麋鹿 2020-12-08 11:03

I am trying to overlay two images. The first one is a 512x512 NumPy array (from a CT image). The second one is also a 512x512 NumPy array but I am just interested in the pix

1条回答
  •  醉梦人生
    2020-12-08 11:39

    Why don't you use imshow instead?

    You can plot a 2D image by doing:

    plt.imshow(Image1, cmap='gray') # I would add interpolation='none'
    

    Afterwards, you can easily overlay the segmentation by doing:

    plt.imshow(Image2_mask, cmap='jet', alpha=0.5) # interpolation='none'
    

    Changing the alpha will change the opacity of the overlay.

    Additionaly, why do you create 2 masks? Only one should be enough, you can do:

    Image2_mask = ma.masked_array(Image2 > 0, Image2)
    

    Practical example:

    import numpy as np
    mask = np.zeros((10,10))
    mask[3:-3, 3:-3] = 1 # white square in black background
    im = mask + np.random.randn(10,10) * 0.01 # random image
    masked = np.ma.masked_where(mask == 0, mask)
    
    import matplotlib.pyplot as plt
    plt.figure()
    plt.subplot(1,2,1)
    plt.imshow(im, 'gray', interpolation='none')
    plt.subplot(1,2,2)
    plt.imshow(im, 'gray', interpolation='none')
    plt.imshow(masked, 'jet', interpolation='none', alpha=0.7)
    plt.show()
    

    0 讨论(0)
提交回复
热议问题