0

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. An image can look like this:

enter image description here

And the result I'm getting is this:

enter image description here

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!

2
  • Maybe you can fit an ellipse around the sclera and then intersect it with the contour of the iris. Commented May 12, 2023 at 12:41
  • crosspost: forum.opencv.org/t/… Commented May 12, 2023 at 13:38

1 Answer 1

0

Here is one approach using Python/OpenCV.

  • Read the input
  • Threshold on the iris color
  • Get Hough Circles
  • Draw the circles
  • Save the results

Input:

enter image description here

import cv2
import numpy as np

# read the image
img = cv2.imread('eye_iris.png')

# threshold on iris color
lower = (0, 0, 0)
upper = (20, 20, 25)
thresh = cv2.inRange(img, lower, upper)

# get circles
circles = cv2.HoughCircles(thresh, cv2.HOUGH_GRADIENT, 1, minDist=60, param1=100, param2=10, minRadius=15, maxRadius=40)
#print(circles)

result = img.copy()
for circle in circles[0]:
    # draw the circle on copy of input
    (x,y,r) = circle
    center = (x,y)
    radius = r
    print('center:', center, 'radius:', radius)
    x = int(x)
    y = int(y)
    radius = int(radius)
    cv2.circle(result, (x,y), radius, (255, 255, 255), 1)

# save output
cv2.imwrite('eye_iris_threshold.png', thresh)
cv2.imwrite('eye_iris_circle.png', result)

# show results
cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey(0)

Threshold Image:

enter image description here

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.