function detectface(test_image)
%Function:detectface(arg1)
%Takes a single argument i.e the name of image
%syntax:detectface('test1.jpg');
%%
close all;
clc;
warning off;
%%
%reading a test image
filename=sprintf('%s',test_image);
%%
%hsv segmentation
img_orig=imread(filename);
img=img_orig; %copy of original image
gray=rgb2gray(img_orig);
%finding width and height of the image
img_width=size(img,1);
img_height=size(img,2);
hsv=rgb2hsv(img);
h=hsv(:,:,1); %separating h component
s=hsv(:,:,2); %separating s component
[row col v]=find( (h>0.5& h<0.9) & (s>=0 & s<=1));
numid=size(row,1); %finding nuber of non-skin pixels present
for i=1:numid
img(row(i),col(i),:)=0; %replacing non-skin pixels values as 0
end
%figure(n)
%subplot(2,2,1),imshow(img_orig);
%title('original image');
%subplot(2,2,2),imshow(img);
%title('hsv segmentation');
%%
%ycbcr segmentation
img_ycbcr=img; %image from the previous segmentation result
ycbcr=rgb2ycbcr(img_ycbcr);
cb=ycbcr(:,:,2);
cr=ycbcr(:,:,3);
%Detect Skin
[row col v] = find(cb<=77 | cb >=127 | cr<=133 | cr>=173); %non-skin
numid = size(row,1);
%Mark Skin Pixels
for i=1:numid
img_ycbcr(row(i),col(i),:) = 0; %replacing non-skin pixels values as 0
% bin(r(i),c(i)) = 1;
end
% subplot(2,2,3),imshow(img_ycbcr);
%title('ycbcr segmentation');
%%
%rgb segmentation
img_rgb=img_ycbcr;
r=double (img_rgb(:,:,1));
g=double (img_rgb(:,:,2));
b=double (img_rgb(:,:,3));
k=r+g+b;
nr=r./k;
ng=g./k;
nb=b./k;
[row col v]= find((( nr<=0.37) | (nr>=0.6)) & (ng>=0 & ng<=0.39 ) |(nb<0.6 & nb>=0.8));
numid=size(row,1);
for i=1:numid
img_rgb(row(i),col(i),:)=0; %replacing non-skin pixels values as 0
end
%h=subplot(2,2,4);imshow(img_rgb);
%title('rgb segmentation');
%%
%performing morphological operations
SE=strel('disk',3);
img_gray=rgb2gray(img_rgb);
ime=imerode(img_gray,SE); %erosion
imd=imdilate(ime,SE); %dilation
%second level morphological operations
%img_gray=rgb2gray(img_rgb);
ime=imerode(img_gray,SE); %erosion
imd=imdilate(ime,SE); %dilation
%%
% second file
%after new morphlogical operaton masking grayscale image
imf=imfill(imd,'holes');
se=strel('disk',6);
imd1=imerode(imf,se);
imd1=imdilate(imd1,se);
[row col]=find(imd1==0);
ind=size(row,1);
for i=1:ind
imd(row(i),col(i))=0; %eliminates tiny cluttered regions
end
%region segmentation
bw=im2bw(imd,0.1);
[no_bound bound]=bwboundaries(bw,8,'noholes');
label_rgb=label2rgb(bound);
total_bound=size(no_bound,1);
%for i=1:total_bound
% k=no_bound{i};
% xmin=min(k(:,1));
% xmax=max(k(:,1));
% ymin=min(k(:,2));
% ymax=max(k(:,2));
% width=xmax-xmin;
% height=ymax-ymin;
%
% if(width==0 || height ==0)
% continue;
% end
% rectangle('Position',[ymin,xmin,height,width]);
%end
%threshold values
width_th=32;
height_th=28;
for i1=1:total_bound
matx=no_bound{i1};
%calculating rectangle for the boundary
xmin=min(matx(:,1));
ymin=min(matx(:,2));
xmax=max(matx(:,1));
ymax=max(matx(:,2));
%ymin=min(matx(:,1));
%xmin=min(matx(:,2));
%ymax=max(matx(:,1));
%xmax=max(matx(:,2));
width=xmax-xmin;
height=ymax-ymin;
%checking the regions against thresholds
if(width<=width_th)
%if the region is to narrow
%remove that region
imd(xmin:xmax,ymin:ymax)=0;
elseif(height<=height_th)
%if the region is to short
%remove that region
imd(xmin:xmax,ymin:ymax)=0;
elseif(width<=width_th && height>=150)
%if the image is too narrow and tall i.e height greater than half
imd(xmin:xmax,ymin:ymax)=0;
elseif(height<=height_th && width>=150)
%if the image is too short and wide
imd(xmin:xmax,ymin:ymax)=0;
end
width=xmax-xmin;
height=ymax-ymin;
asp=width/height;
if(asp<0.61 && asp>1.4)
imd(xmin:xmax,ymin:ymax)=0;
end
%calculating euler no
imgeuler=imd(xmin:xmax,ymin:ymax);
%imgeuler=gray(xmin:xmax,ymin:ymax);
% imgeuler=imd(ymin:ymax,xmin:xmax);
eulerno=bweuler(imgeuler,8);
if(eulerno>=0)
bw(xmin:xmax,ymin:ymax)=0;
imd(xmin:xmax,ymin:ymax)=0;
end
end
%figure
%imshow(imd);
%bw=im2bw(imd,0.1);
%[no_bound bound]=bwboundaries(bw,8,'noholes');
%label_rgb=label2rgb(bound);
%total_bound=size(no_bound,1);
%%fouth file
bw=im2bw(imd,0.1);
[no_bound bound]=bwboundaries(bw,8,'noholes');
total_bound=size(no_bound,1);
%%template matching and normalization
load template_generation %loads average_face
for i=1:total_bound
k=no_bound{i};
xmin=min(k(:,1));
xmax=max(k(:,1));
ymin=min(k(:,2));
ymax=max(k(:,2));
width=xmax-xmin;
height=ymax-ymin;
x=imd(xmin:xmax,ymin:ymax);
x=imresize(x,[100 100]);
%average_face=imresize(average_face,[width height]);
c=normxcorr2(average_face,x);
peak=max(abs(c(:)));
%fprintf('%f\n',peak);
if(peak<0.6)
imd(xmin:xmax,ymin:ymax)=0;
end
end
%close all;
%imshow(imd);
%calculating rectgular box around the remaining areas in original image
bw=im2bw(imd,0.1);
[no_bound bound]=bwboundaries(bw,8,'noholes');
total_bound=size(no_bound,1);
figure
imshow(img_orig);
for i=1:total_bound
k=no_bound{i};
xmin=min(k(:,1));
xmax=max(k(:,1));
ymin=min(k(:,2));
ymax=max(k(:,2));
%finding midpoint of the region to draw rectangle
% xmid=uint16 ( (xmin+xmax)/2 );
% ymid=uint16 ( (ymin+ymax)/2 );
%xmin=uint16 (xmid-50);
%ymin=uint16 (ymid-50);
%xmax=uint16 (xmid+50);
%if(xmax>=img_width)
% xmax=img_width-1;
%end
%ymax=uint16 (ymid+50);
%if(ymax>=img_height)
% ymax=img_height-1;
%end
width=xmax-xmin;
height=ymax-ymin;
if(width==0 || height ==0)
continue;
end
rect=rectangle('Position',[ymin,xmin,height,width]);
set(rect,'EdgeColor',[0 1 0]);
end
end
Link to download the template(average face) data file
> Place it in the same folder of project and execute the code
http://www.mediafire.com/download/hovoi1n9dwfogdh/template_generation.mat
No comments:
Post a Comment