Pupil-Detection
Pupil-Detection copied to clipboard
Detects pupil of the eye from the images/video and create a circle around it.
Pupil Detection
Functionality
- Detects the pupil of an eye based on largest contour technique.
- Each frame/image is processed in grayscale format and then converted to binary image.
- Detects the maximum contour (i.e the pupil area) and creates a circle bounding box around it.
Packages used
- OpenCV v3.4
- Numpy v1.15
- os
- shutil
Steps involved for detecting pupil from images
- Loop through all the images from the input folder
- Convert the images from BGR colorspace to grayscale
- Use
cv2.GaussianBlurwith kernel size (9, 9) to remove noise to an extent - Use
cv2.medianBlurto reduce the noise further - Threshold the image to get the contours
- Use
cv2.findContoursto find all the contours visible after thresholding and select the largest contour out of them only, as it is the pupil. - Get the
x,y,w,hfrom thecv2.boundingRect. - Draw a circle using
cv2.circle - Save the results in the output folder
Steps involved for detecting pupil from a video of an eye
- Capture the video file using
cv2.VideoCapture - Loop the video continuously and save the frames of the video using
read() - Get the exact area of the eye (ROI) from the whole frame.
- Convert the ROI region into Grayscale then use GaussianBlur as well as medianBlur to reduce the noise.
- Threshold the ROI region to get the contours and select only the largest contour as it will be pupil.
- Once, contour is detected using
cv2.findContours, get thex,y,w,hfrom thecv2.boundingRect - Draw a circle using
cv2.circle
NOTE: use cap.set(cv2.CAP_PROP_POS_FRAMES, 0) to loop back the video from beginning otherwise, it will throw error as there is no frame to capture.
NOTE: At the end release() the video file.
