2016-07-15 101 views
8

我想使用Python和OpenCV分割視網膜圖像中的血管。這裏是原始圖像:如何分割血管python opencv

enter image description here

理想我希望所有的血管是這樣的(不同圖像)非常明顯:

enter image description here

這裏是我到目前爲止已經試過。我拍攝了圖像的綠色通道。

img = cv2.imread('images/HealthyEyeFundus.jpg') 
b,g,r = cv2.split(img) 

然後我試圖通過以下this article創建一個匹配濾波器,這是輸出圖像是什麼:

enter image description here

然後我試圖做的最大熵閾值:

def max_entropy(data): 
    # calculate CDF (cumulative density function) 
    cdf = data.astype(np.float).cumsum() 

    # find histogram's nonzero area 
    valid_idx = np.nonzero(data)[0] 
    first_bin = valid_idx[0] 
    last_bin = valid_idx[-1] 

    # initialize search for maximum 
    max_ent, threshold = 0, 0 

    for it in range(first_bin, last_bin + 1): 
     # Background (dark) 
     hist_range = data[:it + 1] 
     hist_range = hist_range[hist_range != 0]/cdf[it] # normalize within selected range & remove all 0 elements 
     tot_ent = -np.sum(hist_range * np.log(hist_range)) # background entropy 

     # Foreground/Object (bright) 
     hist_range = data[it + 1:] 
     # normalize within selected range & remove all 0 elements 
     hist_range = hist_range[hist_range != 0]/(cdf[last_bin] - cdf[it]) 
     tot_ent -= np.sum(hist_range * np.log(hist_range)) # accumulate object entropy 

     # find max 
     if tot_ent > max_ent: 
      max_ent, threshold = tot_ent, it 

    return threshold 


img = skimage.io.imread('image.jpg') 
# obtain histogram 
hist = np.histogram(img, bins=256, range=(0, 256))[0] 
# get threshold 
th = max_entropy.max_entropy(hist) 
print th 

ret,th1 = cv2.threshold(img,th,255,cv2.THRESH_BINARY) 

這是我得到的結果,顯然沒有顯示所有的血管:

enter image description here

我也試着採取匹配的濾波器版本的圖像,並採取其索貝爾值的大小。

img0 = cv2.imread('image.jpg',0) 
sobelx = cv2.Sobel(img0,cv2.CV_64F,1,0,ksize=5) # x 
sobely = cv2.Sobel(img0,cv2.CV_64F,0,1,ksize=5) # y 
magnitude = np.sqrt(sobelx**2+sobely**2) 

這使得血管蹦出更多:

enter image description here

然後我嘗試它Otsu分割:

img0 = cv2.imread('image.jpg',0) 
# # Otsu's thresholding 
ret2,th2 = cv2.threshold(img0,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 

# Otsu's thresholding after Gaussian filtering 
blur = cv2.GaussianBlur(img0,(9,9),5) 
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 

one = Image.fromarray(th2).show() 
one = Image.fromarray(th3).show() 

大津並沒有給予足夠的結果。它結束了包括結果的噪音:

enter image description here

任何幫助表示讚賞血管怎麼可以細分成功。

回答

1

我幾年前在工作視網膜血管檢測了一下,並有不同的方法來做到這一點:

  • 如果你並不需要一個頂級的結果,但快速的東西,你可以使用面向開口,see herehere
  • 然後你有另一個版本使用數學形態學version here

爲了獲得更好的結果,這裏有一些想法: