从分段图像中获取边界

时间:2017-02-21 23:50:33

标签: python image-processing computer-vision scikit-image

我正在尝试使用skimage中的find_boundaries函数来获取分段图像的边界。但是当应用以下代码时,我得到的是假或零的矩阵。 这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage.morphology import watershed
from skimage.segmentation import mark_boundaries, find_boundaries
from skimage.filters import sobel

myimage = io.imread('15746.png')
plt.imshow(myimage, cmap=plt.cm.gray)
plt.show()

hist = np.histogram(myimage, bins= np.arange(0,256))
fig, (ax1, ax2) = plt.subplots(1,2,figsize=(8,3))
ax1.imshow(myimage, cmap=plt.cm.gray, interpolation='nearest')
ax1.axis('off')
ax2.plot(hist[1][:-1], hist[0], lw=2)
ax2.set_title('histogram of grey values')
plt.show()

selected_pixles = []
for i in hist[1][:-1]:
    if hist[0][i] >= 700:
        selected_pixles.append(i)

image = sobel(myimage)
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(image, cmap=plt.cm.gray, interpolation='nearest')
ax.axis('off')
ax.set_title('elevation_map')

markers = np.zeros_like(myimage)
markers[myimage < min(selected_pixles)] = 1
markers[myimage > max(selected_pixles)] = 2
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(markers, cmap=plt.cm.gray, interpolation='nearest')
ax.axis('off')
ax.set_title('markers')

seg = watershed(image, markers=markers)
#print(np.shape(seg[1]))
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(seg, cmap=plt.cm.gray, interpolation='nearest')
ax.axis('off')
ax.set_title('segmentation')
#plt.show()



'''bound = mark_boundaries(seg,seg, mode='thick')
#print(np.shape(bound[0]))
plt.imshow(bound, cmap=plt.cm.gray)
plt.show()'''

bounder = find_boundaries(seg, mode='thick').astype(np.uint8)
print(bounder)

这是结果:

[[0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 ..., 
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]]

这是图像: enter image description here

那么我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

您的脚本运行正常,输出不仅包含零。当我绘制它时,我看到:

boundary plot

当你打印数组时,numpy总结了输出;由于外部值都是0,这就是你所看到的。