Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Convert to binary image by thresholding it
- Mat thresholded_binary;
- threshold(gray, thresholded_binary, 220, 255, THRESH_BINARY);
- //Find all contours
- vector< vector<Point> > contours;
- findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
- //Define some variables for postprocessing
- vector<Point> pupil;
- for (int i = 0; i < contours.size(); i++)
- {
- double area = contourArea(contours[i]);
- Rect rect = boundingRect(contours[i]);
- int radius = rect.width/2;
- // If contour is big enough and has round shape
- // Then it is the pupil --> assign it to the pupil vector
- if (area >= 30 && abs(1 - ((double)rect.width / (double)rect.height)) <= 0.2)
- {
- pupil = contours[i];
- }
- }
- //Look for the founding box of the pupil contour
- Rect region_of_interest = boundingRect( pupil );
- // Create a small image region and a small mask
- Mat small_region = gray( region_of_interest);
- Mat small_mask = thresholded_binary( region_of_interest);
- //Now make everything except pupil area black
- Mat combination = small_region & (small_mask * 255);
- //Show the result
- imshow("segmented eye", combination);
- //Now you can loop over image pixels and look for white ones
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement