📄 detect_pupil.m
字号:
%detect pupil with bwtraceboundary
%Imput: the name of the picture
%Output:center of pupil:[xc,yc]; radius
%Jason Wang
%2005.4.7
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [xc,yc,radius]=detect_pupil(imagename);
imagename=('0020_000.bmp');%test
I_o=imread(imagename);
% imshow(I_o);figure;
I=I_o(100:380,100:540);
TWO=twovalue(I,26);%im2bw(I,0.1);
BW = ~im2bw(TWO);%objects of interest must be white
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%median filter
I_m=medfilt2(BW,[7 7]);
% %close operation,remove eyelash
% se = strel('disk',4);
% I_c = imopen(BW,se);
%fill spot on the pupil
% I_f = imfill(I_c,'holes');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%multi-boundaries
figure;imshow(~I_m);title('multi-boundaries');
hold on;
boundaries = bwboundaries(I_m);
maxlen = 0;
maxk = 0;
for k=1:size(boundaries)
b = boundaries{k};
len = length(b);
if len>maxlen
maxlen = len;
maxk=k;
end
plot(b(:,2),b(:,1),'g','LineWidth',3);
end
%----------------------------------------%
% %寻找single边缘
% dim = size(I_f);
% row = round(dim(1)/2);
% col = max(find(I_f(row,:)));
% boundary = bwtraceboundary(I_f,[row, col],'N');%找到瞳孔边缘
% figure;
% imshow(I);
% title('detect_pupil');
% hold on;
% plot(col,row,'xy');%start point
% plot(boundary(:,2),boundary(:,1),'w','LineWidth',2);%画出实际边缘,绿色线
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fit a circle
[xc,yc,radius] = fit_circle(boundaries{maxk});
%view step results
figure;imshow(~BW);title('二值化图像');
figure;imshow(~I_m);title('中值滤波图像');
% figure;imshow(~I_c);title('开操作');
% figure;imshow(~I_f);title('填充图像');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -