Vessel segmentation in retina image

Viewed 708

I am trying to trace the vessels in a retina image. Currently I am using cv2's threshold functions to make the vessels contrast more with the surrounding retina:

enter image description here

from matplotlib import pyplot as plt
import cv2

img = cv2.imread('misc images/eye.jpeg',0)
img = cv2.medianBlur(img,5)

ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
            cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv2.THRESH_BINARY,11,2)

titles = ['Original Image', 'Global Thresholding (v = 127)',
            'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]

for i in range(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

And this is the result: enter image description here

All 3 methods still have a lot of background noise from the rest of the retina. What can I do to improve the accuracy of vessel tracing?

1 Answers

Here is another way to threshold your image using division normalization followed by filtering on contour area.

  • Read the input
  • Convert to gray
  • Apply morphology dilate (or apply gaussianBlur)
  • Divide input by dilated result
  • Threshold
  • Invert so vessels are white on black background
  • Find all contours
  • Filter contours to discard ones that are too small
  • Draw remaining contours in white on black image as mask
  • Apply mask to threshold image as first result
  • Apply mask to input as second result
  • Save results


Input

enter image description here

import cv2
import numpy as np

# read the image
img = cv2.imread('retina_eye.jpg')

# convert to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_RECT , (5,5))
morph = cv2.morphologyEx(gray, cv2.MORPH_DILATE, kernel)

# divide gray by morphology image
division = cv2.divide(gray, morph, scale=255)

# threshold
thresh = cv2.threshold(division, 0, 255, cv2.THRESH_OTSU )[1] 

# invert
thresh = 255 - thresh

# find contours and discard contours with small areas
mask = np.zeros_like(thresh)
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]

area_thresh = 10000
for cntr in contours:
    area = cv2.contourArea(cntr)
    if area > area_thresh:
        cv2.drawContours(mask, [cntr], -1, 255, 2)

# apply mask to thresh
result1 = cv2.bitwise_and(thresh, mask)
mask = cv2.merge([mask,mask,mask])
result2 = cv2.bitwise_and(img, mask)

# save results
cv2.imwrite('retina_eye_division.jpg',division)
cv2.imwrite('retina_eye_thresh.jpg',thresh)
cv2.imwrite('retina_eye_mask.jpg',mask)
cv2.imwrite('retina_eye_result1.jpg',result1)
cv2.imwrite('retina_eye_result2.jpg',result2)

# show results
cv2.imshow('morph', morph)  
cv2.imshow('division', division)  
cv2.imshow('thresh', thresh)  
cv2.imshow('mask', mask)  
cv2.imshow('result1', result1)  
cv2.imshow('result2', result2)  
cv2.waitKey(0)
cv2.destroyAllWindows()


Division image:

enter image description here

Threshold image:

enter image description here

Mask image:

enter image description here

Result 1:

enter image description here

Result 2:

enter image description here

Related