Advertisement
Guest User

Sample of eye segmenting

a guest
Aug 14th, 2013
1,482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. //Convert to binary image by thresholding it
  2. Mat thresholded_binary;
  3. threshold(gray, thresholded_binary, 220, 255, THRESH_BINARY);
  4.  
  5. //Find all contours
  6. vector< vector<Point> > contours;
  7. findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  8.  
  9. //Define some variables for postprocessing
  10. vector<Point> pupil;
  11.  
  12. for (int i = 0; i < contours.size(); i++)
  13. {
  14.     double area = contourArea(contours[i]);
  15.     Rect rect = boundingRect(contours[i]);
  16.     int radius = rect.width/2;
  17.      
  18.     // If contour is big enough and has round shape
  19.     // Then it is the pupil --> assign it to the pupil vector
  20.     if (area >= 30 && abs(1 - ((double)rect.width / (double)rect.height)) <= 0.2)
  21.     {
  22.         pupil = contours[i];
  23.     }
  24. }
  25.  
  26. //Look for the founding box of the pupil contour
  27. Rect region_of_interest = boundingRect( pupil );
  28.  
  29. // Create a small image region and a small mask
  30. Mat small_region = gray( region_of_interest);
  31. Mat small_mask = thresholded_binary( region_of_interest);
  32.  
  33. //Now make everything except pupil area black
  34. Mat combination = small_region & (small_mask * 255);
  35.  
  36. //Show the result
  37. imshow("segmented eye", combination);
  38.  
  39. //Now you can loop over image pixels and look for white ones
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement