问题描述
我在这里使用代码-对于图像,我使用分水岭算法进行分段-现在,我要访问这些分段,从而为此项目生成蒙版。
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage as ndi
from skimage.segmentation import watershed
from skimage.feature import peak_local_max
# Generate an initial image with two overlapping circles
x,y = np.indices((80,80))
x1,y1,x2,y2 = 28,28,44,52
r1,r2 = 16,20
mask_circle1 = (x - x1)**2 + (y - y1)**2 < r1**2
mask_circle2 = (x - x2)**2 + (y - y2)**2 < r2**2
image = np.logical_or(mask_circle1,mask_circle2)
# Now we want to separate the two objects in image
# Generate the markers as local maxima of the distance to the background
distance = ndi.distance_transform_edt(image)
local_maxi = peak_local_max(distance,indices=False,footprint=np.ones((3,3)),labels=image)
markers = ndi.label(local_maxi)[0]
labels = watershed(-distance,markers,mask=image)
fig,axes = plt.subplots(ncols=3,figsize=(9,3),sharex=True,sharey=True)
ax = axes.ravel()
ax[0].imshow(image,cmap=plt.cm.gray)
ax[0].set_title('Overlapping objects')
ax[1].imshow(-distance,cmap=plt.cm.gray)
ax[1].set_title('Distances')
ax[2].imshow(labels,cmap=plt.cm.nipy_spectral)
ax[2].set_title('Separated objects')
for a in ax:
a.set_axis_off()
fig.tight_layout()
plt.show()
我现在如何为每个对象制作遮罩? (适用于Python 3.7)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)