Detect optic disk in a retina image using contour detection in opencv?

Viewed 459

I have the following retina image and I'm trying to draw a circle around the optic disk (the white round shape in retinal image). Here is the original image:

enter image description here

I applied adaptive thresholding then cv2.findcontour:

import cv2
def detectBlob(file):
    # read image
    img = cv2.imread(file)
    imageName = file.split('.')[0]
    # convert img to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # do adaptive threshold on gray image
    thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 101, 3)

    # apply morphology open then close
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
    blob = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (20,20))
    blob = cv2.morphologyEx(blob, cv2.MORPH_CLOSE, kernel)

    # invert blob
    blob = (255 - blob)

    # Get contours
    cnts,hierarchy = cv2.findContours(blob, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        # write results to disk
    result = img.copy()
    cv2.drawContours(result, cnts, -1, (0, 0, 255), 3)
    cv2.imwrite(imageName+"_threshold.jpg", thresh)
    cv2.imwrite(imageName+"_blob.jpg", blob)
    cv2.imwrite(imageName+"_contour.jpg", result)

detectBlob('16.png')

Here is the what the threshold looks like:

enter image description here

Here is the final output of contours:

enter image description here

Ideally I'm looking for such an output:

enter image description here

2 Answers

Adaptive thresholding fails because the filter size is much too small. And though we don't figure this out, the waves in the background are quite perturbating.

I obtained an interesting result by reducing the image resolution by a factor 16 and applying an adaptive filter of extent 99x99.

enter image description here

You need to identify larger structures. Ideally you need a structure size about 1/4 of the radius of the optic disk to balance results and processing time (experiment with larger sizes until acceptable).

Or you could downsample the image (reduce the resolution and make the picture smaller), which is more or less the same thing, even if you lose precision on the optic disk borders.

Related