2016-12-31 108 views
0

這裏是我使用的邊緣檢測代碼Sobel operator爲什麼我的Sobel邊緣檢測代碼不起作用?

from PIL import Image 
import numpy as np 
from scipy import misc 

a = np.array([1, 2, 1]) 
b = np.array([1, 0, -1]) 
Gy = np.outer(a, b) 
Gx = np.rot90(Gy, k=3) 

def apply(X): 
    a = (X * Gx) 
    b = (X * Gy) 
    return np.abs(a.sum()) + np.abs(b.sum()) 

data = np.uint8(misc.lena()) 
data2 = np.copy(data) 
center = offset = 1 
for i in range(offset, data.shape[0]-offset): 
    for j in range(offset, data.shape[1]-offset): 
     X = data[i-offset:i+offset+1, j-offset:j+offset+1] 
     data[i, j] = apply(X) 

image = Image.fromarray(data) 
image.show() 
image = Image.fromarray(data2) 
image.show() 

導致:

enter image description here

相反的:

enter image description here

對於它的價值,我相當肯定我的循環和圖像克恩的一般想法els是正確的。例如,我能夠產生這種自定義過濾器(高斯中心減去):

enter image description here

這有什麼錯我的Sobel濾波器?

回答

0

終於明白了。我不應該一直在修改數組,因爲它顯然會改變在過濾器的後續應用程序中計算的值。這工作:

... 
new_data = np.zeros(data.shape) 
center = offset = 1 
for i in range(offset, new_data.shape[0]-offset): 
    for j in range(offset, new_data.shape[1]-offset): 
     X = data[i-offset:i+offset+1, j-offset:j+offset+1] 
     new_data[i, j] = apply(X) 
... 

主要生產:

enter image description here

+0

它始終是一個好習慣,有獨立的輸入輸出圖像。 – Piglet

相關問題