1

I am looking for pupil detection from image using pythonwith opencvpackage. In my test images, I am able to detect pupil in (a) part but whenever there is a presence of reflection/ glare, I am unable to detect blob of pupil pixels accurately in (b) part of image. Can anybody help me out? Here is the code I am trying. Test image

import numpy as np
import cv2

name = 'two_eyes1.png'

# reading an image
img = cv2.imread(name, cv2.IMREAD_COLOR)

# inverting image
img_inv = cv2.bitwise_not(img)

gray = cv2.cvtColor(img_inv, cv2.COLOR_BGR2GRAY)

ret, threshold = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY)

#----- Blob detector parameters initiation
params = cv2.SimpleBlobDetector_Params()
#change thresholds
params.minThreshold = 0;
params.maxThreshold = 255;
#filter by area
params.filterByArea = True
params.minArea = 70
# filter by cicularity
params.filterByCircularity = True
params.minCircularity = 0.1
# filter by convexity
params.filterByConvexity = True
params.minConvexity = 0.87
# filter by inertia
params.filterByInertia = True
params.minInertiaRatio = 0.01

det = cv2.SimpleBlobDetector_create(params)

keypoints = det.detect(img)

im_with_key = cv2.drawKeypoints(img, keypoints, np.array([]),
            (0,0,255),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
#----------

titles = ['Input','Inverted','Grayscaled','Thresholded','blobpart']
images = [img, img_inv, gray, threshold, im_with_key]

for i in range(5):
    cv2.imshow(titles[i], images[i])
cv2.waitKey(0)
cv2.destroyAllWindows()
abhi1610
  • 721
  • 13
  • 28
  • You could try another color space, like HSV, Lab or Luv and threshold the V or L channels. Additionally, histogram equalization/Clahe might also work. – Rick M. May 31 '17 at 11:06
  • Thank you @RickM. Can you show me with code for using `cv2.BGR2HSV` and Histogram equalization? – abhi1610 May 31 '17 at 12:31
  • For HSV, `hsv = cv2.cvtColor(img_inv, cv2.COLOR_BGR2HSV) h,s,v = cv2.split(hsv).` Use `v` for threshold. For clahe, [link](http://docs.opencv.org/3.1.0/d5/daf/tutorial_py_histogram_equalization.html). It appears from the images you uploaded that the _(b)_ image is also a different perspective. – Rick M. May 31 '17 at 12:40

0 Answers0