【问题标题】:Overlay an image segmentation with numpy and matplotlib使用 numpy 和 matplotlib 覆盖图像分割
【发布时间】:2015-10-30 21:01:48
【问题描述】:

我正在尝试叠加两个图像。第一个是 512x512 NumPy 阵列(来自 CT 图像)。第二个也是一个 512x512 NumPy 数组,但我只对值大于 0 的像素感兴趣(功能图像)。

为此,我正在尝试创建一个掩码数组。

import numpy as np 
import numpy.ma as ma
import matplotlib.pyplot as plt

# Both images are loaded from a dicom. Both are numpy arrays of (512,512) 
Image1 = readimage(path)
Image2 = readimage(path)
# Create image 2 mask
mask = ma.masked_where(Image2>0, Image2)
Image2_mask = ma.masked_array(Image2,mask)

# Plot images
plt.figure(dpi=300)
y, x = np.mgrid[1:513,1:513]
plt.axes().set_aspect('equal', 'datalim')
plt.set_cmap(plt.gray())
plt.pcolormesh(x, y, Image1,cmap='gray')
plt.pcolormesh(x, y, Image2_mask,cmap='jet')
plt.axis([x.min(), x.max(), y.min(), y.max()])
plt.colorbar()
plt.show()

此代码不显示任何叠加层。我做错了什么?有没有直接的方法?我来自 Matlab 环境,对 Python 还是很陌生。

【问题讨论】:

  • 能否请您也提供这段代码的开头? e.g. ma = ...
  • 我编辑了帖子以包含代码的第一部分。这两个图像都是从一个从 CT dicom 中提取的函数加载的,我相信这并不重要。图片类型为:Image.dtype Out[33]: dtype('int16')

标签: python arrays image numpy matplotlib


【解决方案1】:

你为什么不改用imshow

您可以通过以下方式绘制 2D 图像:

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

之后,您可以通过以下方式轻松叠加分割:

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

更改 alpha 将更改叠加层的不透明度。

另外,你为什么要创建 2 个面具?一个就够了,你可以这样做:

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

实际例子:

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()

【讨论】:

    【解决方案2】:

    我可以给你我的函数两个重叠的图片和数据集的掩码:

    def get_overlapped_img(filename, img_folder, mask_folder):
    # Import orginal img
    img = cv2.imread(img_folder+"/"+filename+".jpg")
    
    # Import and convert the mask from binary to RGB
    mask = Image.open(mask_folder+"/"+filename+".png").convert('RGB')
    width, height = mask.size
    
    # Convert the white color (for blobs) to magenta
    mask_colored = change_color(mask, width, height, (255, 255, 255), (186,85,211))
    # Convert the black (for background) to white --> important to make a good overlapping
    mask_colored = change_color(mask_colored, width, height, (0, 0, 0), (255,255,255))
    
    return cv2.addWeighted(np.array(img),0.4,np.array(mask_colored),0.3,0)
    

    改变图片中每个像素颜色的功能:

    def change_color(picture, width, height, ex_color, new_color):
    # Process every pixel
    for x in range(width):
        for y in range(height):
            current_color = picture.getpixel( (x,y) )
            if current_color == ex_color:
                picture.putpixel( (x,y), new_color)
    return picture
    

    【讨论】:

      【解决方案3】:

      虽然不是直接使用 matplotlib,但一种替代方法是使用构建在 matplotlib 之上的nilearn。如果使用 nifti 文件(神经影像学中的典型扩展名),您可以使用函数 plot_roiadd_overlay

      例如,正如this thread 中所建议的,您可以这样写:

      >>> from nilearn import plotting
      >>> display = plotting.plot_anat('path/to/volume.nii.gz')  # plot volume 
      >>> display.add_overlay('path/to/mask.nii.gz',cmap='hot', colorbar=True)  # add mask
      

      如果您只对某些平面/视图感兴趣,您可以使用参数display_modecut_coords

      最终结果将类似于:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-30
        • 2018-02-06
        • 2012-03-27
        • 2022-10-17
        • 2020-10-03
        • 2012-01-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多