how to detect pupil in matlab? [closed]

狂风中的少年 提交于 2019-12-14 04:08:40

问题


Below is a source code that i download from somewhere, it is able to detect red color objects and display its center coordinate.

a = imaqhwinfo;
[camera_name, camera_id, format] = getCameraInfo(a);


% Capture the video frames using the videoinput function
% You have to replace the resolution & your installed adaptor name.
vid = videoinput(camera_name, camera_id, format);

% Set the properties of the video object
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 1;

%start the video aquisition here
start(vid)

% Set a loop that stop after 100 frames of aquisition
while(vid.FramesAcquired<=100)

% Get the snapshot of the current frame
data = getsnapshot(vid);

% Now to track red objects in real time
% we have to subtract the red component 
% from the grayscale image to extract the red components in the image.
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.17);

% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);

% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);

% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');

% Display the image
imshow(data)

hold on

%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
    bb = stats(object).BoundingBox;
    bc = stats(object).Centroid;
    rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
    plot(bc(1),bc(2), '-m+')
    a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), 'Y: ',  num2str(round(bc(2)))));
    %disp(' X-Coordinate   Y-cordinate')
    %x=gallery('uniformdata',[5 3],0);
    %disp(x)
    set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color',      'yellow');
end

hold off
end
% Both the loops end here.

% Stop the video aquisition.
stop(vid);

% Flush all the image data stored in the memory buffer.
flushdata(vid);

% Clear all variables
% clear all
sprintf('%s','That was all about Image tracking, Guess that was pretty easy :) ')

the problem is i would like to detect the pupil of the eye, so i need to detect black color in the image, but i have no idea how to modified the code to change it able to detect black color. So, any idea to this? please help me, thanks you all.


回答1:


diff_im = imsubtract(data(:,:,1), rgb2gray(data));

is where the algorithm extracts the red component of the color data. So that's where you have to make some changes.

Instead of extracting the red component (as pointed out in the comments of your code), you can just continue with the grayscale.

diff_im = rgb2gray(data);

But I think this would result in finding white objects. To counter this problem you can change the blob analysis, or just invert the input.I think it goes like this:

diff_im = imcomplement(rgb2gray(data));

I can't test it here though, cause I have no access to the Image Processing Toolbox. Can you test it out for yourself?

Test in octave with the image package

The picture I used for testing is found here.

% Get the snapshot of the current frame
  data = imread('child-eye1-560x372.jpg');

% Now to track red objects in real time we have to subtract the red component
% from the grayscale image to extract the red components in the image.
  diff_im = rgb2gray(data);
  imwrite(diff_im,'diff_im.jpg');
%Use a median filter to filter out noise
  diff_im = medfilt2(diff_im, [3 3]);
  imwrite(diff_im,'diff_im_filt1.jpg');
% Convert the resulting grayscale image into a binary image.
  diff_im = im2bw(diff_im,0.17);
  imwrite(diff_im,'diff_im_filt2.jpg');

These are just the filtering steps, the blob analysis functions are not available in octave. The resulting images are:

If I lower the filter value of im2bw to 0.07, the results is even better:

As you can see, this part of the process seems all right. The last image is binary, so that large big blob shouldn't be too difficult to find. As before I can't test it myself...

Maybe the problem isn't in the algorithm, but in the data you provide it with. If there are numerous small black blobs in the picture, the algorithm will find them all and include them in its result..



来源:https://stackoverflow.com/questions/10506393/how-to-detect-pupil-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!