2017-02-21 266 views
0

我在這裏試圖使用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

那我在這裏失蹤?

+0

請發表相關圖片。 –

+0

我編輯我的文章,並添加圖像 –

回答

1

你的腳本工作正常,輸出不僅僅由零組成。當我繪製它,我看到:

boundary plot

當您打印陣列,numpy的總結輸出;並且由於外部值全部爲0,這就是你所看到的。

+0

我可以使用輸出數組作爲描述符?...如果我想爲這樣的圖像做一個形狀描述符,你認爲它的最佳方式是什麼? –

+0

要製作一個描述符,可以邊界化邊界,提取其座標,然後使用其中一種標準機制,如傅立葉係數(http://fourier.eng.hmc.edu/e161/lectures/fd/node1.html) 。 –