0% found this document useful (0 votes)
149 views

Code

This document contains code for face and eye detection from images and video streams. It includes algorithms for skin color detection, eye mapping, and centroid detection to locate facial features. Functions are also included for mouse control and image comparison using sum of absolute differences. The overall purpose is to develop computer vision techniques for facial feature extraction and control applications using MATLAB.

Uploaded by

nshefeek
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

Code

This document contains code for face and eye detection from images and video streams. It includes algorithms for skin color detection, eye mapping, and centroid detection to locate facial features. Functions are also included for mouse control and image comparison using sum of absolute differences. The overall purpose is to develop computer vision techniques for facial feature extraction and control applications using MATLAB.

Uploaded by

nshefeek
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Face Detection of Individual Image Frame

%Detection of Face on the basis of skin color


clear all;
close all;
clc
I=imread('salu3.jpg');
imshow(I)
cform = makecform('srgb2lab');
J = applycform(I,cform);
figure;imshow(J);
K=J(:,:,2);
figure;imshow(K);
L=graythresh(J(:,:,2));
BW1=im2bw(J(:,:,2),L);
figure;imshow(BW1);
M=graythresh(J(:,:,3));
figure;imshow(J(:,:,3));
BW2=im2bw(J(:,:,3),M);
figure;imshow(BW2);
O=BW1.*BW2;
% Bounding box
P=bwlabel(O,8);
BB=regionprops(P,'Boundingbox');
BB1=struct2cell(BB);
BB2=cell2mat(BB1);
[s1 s2]=size(BB2);
mx=0;
for k=3:4:s2-1
p=BB2(1,k)*BB2(1,k+1);
if p>mx && (BB2(1,k)/BB2(1,k+1))<1.8
mx=p;
j=k;
end
end
figure,imshow(I);
hold on;
rectangle('Position',[BB2(1,j-2),BB2(1,j-1),BB2(1,j),BB2(1,j+1)],'EdgeColor','r'
)

Eye Detection using Eyemap


clear all;
clc;
I=imread('salu3.jpg');
EyeMap = rgb2ycbcr(I);
[m n l]=size(EyeMap);
y = double(EyeMap(:,:,1));
Cb = double(EyeMap(:,:,2));
Cr = double(EyeMap(:,:,3));
Z=Cr;
Q = Cb.^2;

R = (255-Cr).^2;
G = Cb./Cr;
CrCb = Cr./Cb;
EyeC=(Q+R+G)/3;
CRS = Cr.^2;
ssCRS = sum(sum(CRS));
ssCrCb=sum(sum(CrCb));
eta = 0.95 * ssCRS/ssCrCb;
x= CRS - eta * Cr./Cb;
MM = CRS.*x.*x;
SE=strel('disk',10) ;
UP=imdilate(y,SE);
Down=imerode(y,SE);
EyeY= UP./(Down+1);
EyeMap=EyeY.*EyeC;
img = mat2gray(EyeMap);
if img<0.85
sas=0;
else
sas=img;
end
% lll=graythresh(img);
bimg=im2bw(sas);
% figure;imshow(img);
cform = makecform('srgb2lab');
J = applycform(I,cform);
K=J(:,:,2);
L=graythresh(J(:,:,2));
BW1=im2bw(J(:,:,2),L);
M=graythresh(J(:,:,3));
BW2=im2bw(J(:,:,3),M);
O=BW1.*BW2;
l1=strel('disk',8);
% Ob1=imerode(O,l1);
% O=~(Ob1);

% figure;imshow(I);
% hold on;
% plot(a1,a2,'r*');
% hold off;
[m1 n1] = size(img);
for i=1:m1
for j=1:n1
if img(i,j)<0.7
sas(i,j)=0;
else
sas(i,j)=img(i,j);
end
end
end
if img<0.8
sas=0;
else
sas=img;
end
sasb=im2bw(sas);
ll=strel('disk',15);

sasbb=imdilate(sasb,ll);
ss=regionprops(sasbb,'centroid');
cc=cat(1,ss.Centroid);
a11=cc(:,1);
a12=cc(:,2);
%
% P=bwlabel(O,8);
% BB=regionprops(P,'Boundingbox');
% BB1=struct2cell(BB);
% BB2=cell2mat(BB1);
%
% [s1 s2]=size(BB2);
% mx=0;
% for k=3:4:s2-1
%
p=BB2(1,k)*BB2(1,k+1);
%
if p>mx && (BB2(1,k)/BB2(1,k+1))<1.8
%
mx=p;
%
j=k;
%
end
% end

figure;imshow(I);
hold on;
plot(a11,a12,'r*');
% rectangle('Position',[BB2(1,j-2),BB2(1,j-1),BB2(1,j),BB2(1,j+1)],'EdgeColor','
r' )
hold off;

% Face skin extraction


% clear all;
% close all;
% clc;
% vid = videoinput('winvideo', 2, 'RGB24_320x240');
% src = getselectedsource(vid);
% preview(vid);
% vid.ReturnedColorspace = 'rgb';
% vid.FramesPerTrigger = inf;
% start(vid);
% for n=1:inf
%
%
i=getsnapshot(vid);
%
I=double(i);
% [hue,s,v]=rgb2hsv(I);
%
% cb = 0.148 * I(:,:,1) - 0.291 * I(:,:,2) + 0.439 * I(:,:,2) + 128;
% cr = 0.439 * I(:,:,1) - 0.368 * I(:,:,2) - 0.071 * I(:,:,3) + 128;
% [w h]=size(I(:,:,1));
%
% for x=1:w
%
for y=1:h
%
if 140<=cr(x,y) && cr(x,y)<=165 && 140<=cb(x,y) && cb(x,y)<=195 && 0.
01<=hue(x,y) && hue(x,y)<=0.1
%
segment(x,y)=1;
%
else

%
%
%
%
%
%
%
%
%
%

segment(x,y)=0;
end
end
end
im(:,:,1)=I(:,:,1).*segment;
im(:,:,2)=I(:,:,2).*segment;
im(:,:,3)=I(:,:,3).*segment;
imshow(uint8(im));
end

% SAD Algorithm to compare images


%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%

clear all;
close all;
clc;
tic;
a1 = imread('image201112190001.jpg');
a2 = imread('image201112190001.jpg');
[s1 s2 s3] = size(a1);
[t1 t2 t3] = size(a2);
if s1~=t1 && s2~=t2 && s3~=t3
disp('Error Image Size does not match');
else
ag1 = rgb2gray(a1);
ag2 = rgb2gray(a2);
y = imabsdiff(ag1,ag2);
sad1 = sum(y);
sad2 = sum(sad1);
if sad2==0
disp('Both Images are equal');
else
disp('Unequal Images');
end
end
toc;

Eye Detection in a Video Stream


clear all;
close all;
clc;
vid = videoinput('winvideo', 1, 'RGB24_320x240');
src = getselectedsource(vid);
preview(vid);
vid.ReturnedColorspace = 'rgb';
vid.FramesPerTrigger = inf;
start(vid);
for i=1:inf
I=getsnapshot(vid);
EyeMap = rgb2ycbcr(I);
[m n l]=size(EyeMap);
y = double(EyeMap(:,:,1));
Cb = double(EyeMap(:,:,2));
Cr = double(EyeMap(:,:,3));
Z=Cr;

%
%
%
%
%
%
%
%
%

Q = Cb.^2;
R = (255-Cr).^2;
G = Cb./Cr;
CrCb = Cr./Cb;
EyeC=(Q+R+G)/3;
CRS = Cr.^2;
ssCRS = sum(sum(CRS));
ssCrCb=sum(sum(CrCb));
eta = 0.95 * ssCRS/ssCrCb;
x= CRS - eta * Cr./Cb;
MM = CRS.*x.*x;
SE=strel('disk',4) ;
UP=imdilate(y,SE);
Down=imerode(y,SE);
EyeY= UP./(Down+1);
EyeMap=EyeY.*EyeC;
subplot(1,4,1), imagesc(I);title ('Face');axis off;
subplot(1,4,2), imagesc(Q);title ('Cb^2');axis off;
subplot(1,4,3), imagesc(R);title ('(Cr-complement)^2');axis off;
subplot(1,4,4), imagesc(EyeC);title ('Eye-Map-C'); axis off;
colormap(gray);
subplot(1,4,1), imagesc(UP);title ('Dilation'); axis off;
subplot(1,4,2), imagesc(Down);title ('Erotion'); axis off;
subplot(1,4,3), imagesc(EyeY);title ('EyeY'); axis off;
subplot(1,4,4),
imagesc(EyeMap);
colormap(gray);

end
Mouse Movement
Right Click
clc
% clear
S.ja=java.awt.Robot;
S.ja.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
S.ja.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
pause(0.001)
S.ja.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
S.ja.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
Left Click
clc
% clear all
S.ja=java.awt.Robot;
S.ja.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
S.ja.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
Left
function[]=left()
pos=get(0,'PointerLocation')
[m n]=size(pos)
set(0,'PointerLocation',[m+100 n])

Right
clc
[pos]=get(0,'PointerLocation')
m=pos(1);
n=pos(2);
set(0,'PointerLocation',[m+10 n])
Up
clc
[pos]=get(0,'PointerLocation')
m=pos(1);
n=pos(2);
set(0,'PointerLocation',[m n+10])
Down
clc
[pos]=get(0,'PointerLocation')
m=pos(1);
n=pos(2);
set(0,'PointerLocation',[m n-10])

You might also like