如何在matlab中检测瞳孔? [英] how to detect pupil in matlab?

查看:710
本文介绍了如何在matlab中检测瞳孔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我从某处下载的源代码,它能够检测到红色对象并显示其中心坐标。

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.

推荐答案

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);

但我认为这会导致找到白色物体。要解决这个问题,你可以改变blob分析,或者只是反转输入。我认为它是这样的:

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?

我用于测试的图片是这里

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');

这些只是过滤步骤,blob分析功能在八度音程中不可用。生成的图像是:

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




如果我将 im2bw 的过滤值降低到0.07,结果会更好:

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..

这篇关于如何在matlab中检测瞳孔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆