I am trying to draw the contour of the iris from some pictures of the eye using python and opencv. I do this by isolating the iris region using the Hough circle transformation to detect where the iris is, then I isolate the region of the Hough circle + 2 pixels around it to eliminate unwanted contours. I do this because the Hough circle transformation draws a perfect circle around the iris, but I need its exact contour. Then I threshold the isolated image, put in on a white canvas, get it’s contours and then draw the resulting contours on the original image. This gives an ok result, but it also draws the contours of the eyebrow and eyelid.
The result I’m getting is this:
Here is the code:
import cv2 as cv2
import numpy as np
def image_processing(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
img_blur = cv2.medianBlur(gray, 5)
#creating white image the same size as the sample image
white_image = np.full((image.shape[0], image.shape[1]), 255, dtype=np.uint8)
#detecting the iris region using hough circles
circles = cv2.HoughCircles(img_blur, cv2.HOUGH_GRADIENT, 1, 20, param1 = 200, param2 = 20, minRadius = 0)
inner_circle = np.uint16(np.around(circles[0][0])).tolist()
#adding the eye region on the white canvas
cv2.circle(white_image, (inner_circle[0], inner_circle[1]), inner_circle[2]+2, (0, 0, 0), -1)
roi = cv2.bitwise_or(gray,white_image)
#thresholding result
roi_blur = cv2.medianBlur(roi, 5)
ret, thresh = cv2.threshold(roi, 127, 255, cv2.THRESH_BINARY)
return thresh
def find_countours(image):
img = image
#finding hte contours
contours,hierarchy = cv2.findContours(img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#this iterates through the contours and removes the ones under or over a certain area to remove reflections being highlighted
list_contours = []
for contour in contours:
if cv2.contourArea(contour) > 100 and cv2.contourArea(contour) < 50000:
list_contours.append(contour)
return list_contours
#this funciton gets the center of the eye
def get_center(image):
img= image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.medianBlur(gray, 5)
circles = cv2.HoughCircles(img_blur, cv2.HOUGH_GRADIENT, 1, 20, param1 = 200, param2 = 20, minRadius = 0)
inner_circle = np.uint16(np.around(circles[0][0])).tolist()
center =(inner_circle[0],inner_circle[1])
return center
#this function draws the iris contour and the center of the eye
def draw_image(image,contours,center):
img = image
cont = contours
cv2.drawContours(img, cont, -1, (0,255,0), 1)
cv2.drawMarker(img, center, (0, 255, 0), cv2.MARKER_CROSS, 15, 1)
cv2.imshow("Result Image", img)
cv2.waitKey(0)
image = cv2.imread('eye.jpg')
center = get_center(image)
processed_image = image_processing(image)
contours = find_countours(processed_image)
draw_image(image,contours,center)
My question is, how can I remove the contour that highlights the eyelid and the eyebrows? I just want the iris contour highlighted. Thanks!