如何将分割结果叠加到其他图像上
How to superimpose segmentation result over other image
我有原始图像及其分割蒙版。我的任务是在原始图像上绘制彩色分割图。
我试过 ,但它给我的输出与输入图像相同。
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)
原图:
分割后的图像:
这是我想要实现的结果的类似示例:
使用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 来更改颜色。
我有原始图像及其分割蒙版。我的任务是在原始图像上绘制彩色分割图。
我试过
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)
原图:
分割后的图像:
这是我想要实现的结果的类似示例:
使用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 来更改颜色。