python - 从分割图像中获取边界

标签 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

那么我在这里缺少什么?

最佳答案

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

boundary plot

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

关于python - 从分割图像中获取边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42380170/

相关文章:

python - 使用行为标签仅执行此类标签的子案例

python - python如何区分有符号和无符号数据类型?

c++ - 使用 CImg 对单 channel 进行复杂修改

matlab - Lucas Kanade 光流,方向矢量

opencv - 相机标定体积

geometry - 极几何

python - 尝试使用 str.contains 和 boolean 掩码,但仅针对 Pandas 中的两个单值实例

python - Django 默认表

java - 如何确定图像 (java.awt.Image) 是否为黑白且没有颜色?

python - 在 python 中使用 cv2.findContours() 时出错