Based on this image,
Using Hough Transformation, I managed to detect many circles. But I require only the middle circle detected around the pupil. There are several methods that I was thinking, that could remove the other circles.
1) counting the number of black pixels within the circle and the one with the most is the pupil. 2) taking the white pixels along the circumference of the pupil. the one with the most white pixels is the pupil.
I was thinking number 2 would be a more accurate solution. Hence, how do I count the number of white pixels?
No products are associated with this question.
How standard are your images? Could you choose the circle whose radius and centre coordinates are within expected ranges?
If you can't do this, I'd suggest making measurements on the original image, rather than on the edge map. There have been answers here recently showing how to make measurements on the pixels in a circular disk - add a comment if you need help finding one, but do a search first.
By the way, there's a highlight which badly affects the parameters of the circle round the pupil. If you can find some way of removing it (by preprocessing prior to the edge detection perhaps) that will improve your accuracy enormously.
Rather than counting black pixels (which means defining what is "black"), why not compare the mean of the grey-levels inside the circles? Here's a function you can use to compute these means:
function m = meanDisk(img, xc, yc, r) %meanDisk computes mean of values inside a circle % M = meanDisk(IMG, XC, YC, R) returns the mean of IMG(Y,X) for all X and % Y such that the Euclidean distance of (X,Y) from (XC,YC) is less than % R. IMG must be 2-D, R must be positive, and some elements of IMG must % lie within the circle.
% This section is for efficiency only - avoids wasting computation time on % pixels outside the bounding square [sy sx] = size(img); xmin = max(1, floor(xc-r)); xmax = min(sx, ceil(xc+r)); ymin = max(1, floor(yc-r)); ymax = min(sy, ceil(yc+r)); img = img(ymin:ymax, xmin:xmax); % trim boundaries xc = xc - xmin + 1; yc = yc - ymin + 1;
% Make a circle mask [x y] = meshgrid(1:size(img,2), 1:size(img,1)); mask = (x-xc).^2 + (y-yc).^2 < r.^2;
% Compute mean m = sum(sum(double(img) .* mask)) / sum(mask(:));
EDIT Adding code to deal with the four example images linked to from Ivan's comment of 12 Jan 2012.
This assumes the use of this Hough transform from the file exchange.
The code below works for all 4 of the example images posted (though note that cropping may affect the results as the original image isn't in quite the same place in all of them). I used the fact that the pupil is dark to detect the case where the eye is closed - this works better than playing with the Hough transform parameters.
% Read in the image as downloaded, crop out the starting image, convert to % grayscale in range 0-1 img = imread('help1.jpg'); % or whatever img = double(rgb2gray(img(28:169, 167:391, :)))/256;
% Find edges. Note that Canny method incorporates Gaussian smoothing and % gradient calculation, so no need to do these separately edgeim = edge(img, 'canny');
% Detect most prominent circle. Radius range may be overgenerous. radii = 7:2:21; [h, margin] = circle_hough(edgeim, radii); peak = circle_houghpeaks(h, radii, margin, 'npeaks', 1); cx = peak(1); cy = peak(2); r = peak(3);
% Test whether the brightness inside the circle is greater than the average % image brightness divided by the following constant. If it is, the pupil % is probably not visible. pupil_darkness_ratio = 2;
if pupil_darkness_ratio*meanDisk(img, cx, cy, r) > mean(img(:)) disp('No pupil found'); else imshow(img); hold on; [x, y] = circlepoints(r); plot(x+cx, y+cy, 'g-'); hold off end
I think you should look at what you said: "require only the middle circle." Now since you placed those circles, you know where the centers are. So you simply take the center that is closest to (numberOfRows/2, numberOfColumns/2). It's as simple as that, assuming the circle you want is the one closest to the middle of the image.