【问题标题】:How to superimpose segmentation result over other image如何将分割结果叠加在其他图像上
【发布时间】:2021-11-15 14:15:37
【问题描述】:

我有原始图像及其分段蒙版。我的任务是在原始图像上绘制彩色分割图。 我尝试关注this,但它给了我与输入图像相同的输出。

import skimage.filters.rank
import skimage.morphology
import numpy as np
import cv2

# Load images as greyscale but make main RGB so we can annotate in colour
seg  = cv2.imread('segmented.png',cv2.IMREAD_GRAYSCALE)
main = cv2.imread('main.png',cv2.IMREAD_GRAYSCALE)
main = cv2.cvtColor(main,cv2.COLOR_GRAY2BGR)

# Create structuring element that defines the neighbourhood for morphology
selem = skimage.morphology.disk(1)

# Mask for edges of segment 1 and segment 2
# We are basically looking for pixels with value 1 in the segmented image within a radius of 1 pixel of a black pixel...
# ... then the same again but for pixels with a vaue of 2 in the segmented image within a radius of 1 pixel of a black pixel
seg1 = (skimage.filters.rank.minimum(seg,selem) == 0) & (skimage.filters.rank.maximum(seg, selem) == 1)
seg2 = (skimage.filters.rank.minimum(seg,selem) == 0) & (skimage.filters.rank.maximum(seg, selem) == 2)

main[seg1,:] = np.asarray([0, 0,   255]) # Make segment 1 pixels red in main image
main[seg2,:] = np.asarray([0, 255, 255]) # Make segment 2 pixels yellow in main image

# Save result
cv2.imwrite('result.png',main) 

原图:

分割后的图像:

这是我想要达到的结果的类似示例:

【问题讨论】:

    标签: python matplotlib image-processing image-segmentation medical-imaging


    【解决方案1】:

    使用 matplotlib.pyplot 可以轻松完成

    import matplotlib.pyplot as plt
    
    image = plt.imread('image.png')
    mask = plt.imread('mask.png')
    
    fig, ax = plt.subplots()
    ax.imshow(image, cmap='gray')
    ax.imshow(mask, cmap='gray', alpha=0.5)
    fig.show()
    fig.savefig('overlapped.png')
    
    

    您还可以通过更改颜色图参数 cmap 来更改颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 2019-05-24
      相关资源
      最近更新 更多