Image Segmentation by OpenCV

• Watershed • Graphcut • Gabor wavelet • Adaptive threshold and contour method are explored to do vessel segmentation, the best method is adaptive threshold and contour.

Original Retina Image

Python source code:

__author__ = 'pittnuts'
import cv2
from numpy import *
from matplotlib import pyplot as plt
import numpy as np

def build_filters():
    filters = []
    ksize = 31
    for theta in np.arange(0, np.pi, np.pi / 16):
        kern = cv2.getGaborKernel((ksize, ksize), 4.0, theta, 10.0, 0.5, 0, ktype=cv2.CV_32F)
        kern /= 1.5*kern.sum()
    return filters

def gabor_segment(img, filters):
    accum = np.zeros_like(img)
    for kern in filters:
        fimg = cv2.filter2D(img, cv2.CV_8UC1, kern)
        #cv2.imshow('filtered retina {}'.format(kern),fimg[::2,::2])
        np.maximum(accum, fimg, accum)
    return accum

if __name__ == "__main__":
    #imagefile = "../data/RetinaFD-L12-1024.jpg"
    imagefile = "../data/RetinaFD-R6-1024.jpg"
    img = cv2.imread(imagefile)
    orig_dimen = img.shape
    cv2.imshow('original retina',img[::2,::2])
    img = img[:,:,1]
    cv2.imshow('green retina',img[::2,::2])

    hist = cv2.calcHist([img],[0],None,[256],[0,256])

    img_segmented = gabor_segment(img,build_filters())
    cv2.imshow('gabor segmentation',img_segmented[::2,::2])

    cv2.imshow('threshold segmentation',img_segmented[::2,::2])
    im2, contours, hierarchy = cv2.findContours(img_segmented,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    # remove small contours
    for idx in range(len(contours)-1,-1,-1):
        if cv2.contourArea(contours[idx])


larger contour area
larger contour area