0% found this document useful (0 votes)
119 views7 pages

Face Detection

This document analyzes an image to detect skin regions using different color models (YCbCr, HSV, RGB). It converts the image to each color model and applies thresholding to isolate skin pixels. Connected component analysis is used to identify skin regions, which are filtered using shape and size criteria. The final output image marks detected skin pixels in red.

Uploaded by

Jeremy Kipgen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views7 pages

Face Detection

This document analyzes an image to detect skin regions using different color models (YCbCr, HSV, RGB). It converts the image to each color model and applies thresholding to isolate skin pixels. Connected component analysis is used to identify skin regions, which are filtered using shape and size criteria. The final output image marks detected skin pixels in red.

Uploaded by

Jeremy Kipgen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

clc; close all img=imread('test01.

jpg'); % img as an m by n by 3 matrix imshow(img),title('original'); img; % look at the matrix m = size(img,1); % number of rows n = size(img,2); % number of columns p = size(img,3); % 3 values(RGB) for each pixel img1=rgb2ycbcr(img); ycbcr_skin = zeros(m,n); r=img(:,:,1); % r,g,b are all matrices here having dimensions m by n and contain the respective r g and b color values for each pixel g=img(:,:,2); b=img(:,:,3); y=img1(:,:,1); % y cb and cr values of each pixel after converting image into ycbcr color tone cb=img1(:,:,2); cr=img1(:,:,3); %figure; %imshow(img1); % display the ycbcr image cr1=137;% threshold values of cb and cr for skin region cr2=177; cb1=90; cb2=130; for i = 1:m%this will check all the pixel with the threshold values of cb and cr and assign value 1 for that pixel for j = 1:n if(cr(i,j)>cr1 && cr(i,j)<cr2 && cb(i,j)>cb1 && cb(i,j)<cb2) ycbcr_skin(i,j)=1; end end end %ycbcr_skin; final_ycbcr = zeros(m,n); for i = 1:m%making a matrix with original pixel values for skin region and 0 for non skin tone regions for j = 1:n if(ycbcr_skin(i,j)==1) final_ycbcr(i,j)=img(i,j); end end

end figure; subplot(221),imshow(final_ycbcr),title('binary mask for ycbcr mode'); %%%%%%% for the hsv mode ------img1=rgb2hsv(img); m=size(img1,1); n=size(img1,2); h=zeros(m,n); s=zeros(m,n); v=zeros(m,n); for i=1:m for j=1:n h(i,j)=360*img1(i,j,1); end end s=img1(:,:,2); v=img1(:,:,3); max(max(s)) hsv_skin=zeros(m,n); for i=1:m for j=1:n if(h(i,j)>0 && h(i,j)<50 && .23<s(i,j) && .68>s(i,j)) hsv_skin(i,j)=1; end end end for i=1:m for j=1:n if(hsv_skin(i,j)==1) final_hsv(i,j)=img(i,j); end end end final_hsv; subplot(222),imshow(hsv_skin),title('binary mask for hsv mode'); %%%%% from rgb mode %r1=180; %r2=249; %g1=130; %g2=195; %b1=120;

%b2=215; rgb_skin=zeros(m,n); final_rgb=zeros(m,n); for i=1:m for j=1:n if(r(i,j)>95 && g(i,j)>40 && b(i,j)>20) if(max(max(r(i,j),g(i,j)),b(i,j))min(min(r(i,j),g(i,j)),b(i,j))>15) if(abs(r(i,j)-b(i,j))>15 && r(i,j)>b(i,j) && r(i,j)>g(i,j)) rgb_skin(i,j)=1; end end end end end for i=1:m for j=1:n
if(rgb_skin(i,j)==1) final_rgb(i,j)=img(i,j); end end end subplot(223),imshow(final_rgb),title('binary mask for rgb mode'); combo_skin=zeros(m,n); for i=1:m for j=1:n combo_skin(i,j)=ycbcr_skin(i,j)+rgb_skin(i,j) +hsv_skin(i,j); end end noofskinpixels=0; final_combo=zeros(m,n); for i=1:m for j=1:n if combo_skin(i,j)~=0 final_combo(i,j)=img(i,j); noofskinpixels= noofskinpixels+1; end end end subplot(224),imshow(final_combo),title('binary mask of all 3 color modes'); %%%%%to calculate the number of connected components in the final skin tone %%%%%image obtained L = BWLABEL(combo_skin,8); L1 = max(max(L)) pixval('ON'); %%%% to count the number of pixels in each connected segment

count=zeros(1,max(max(L))); for k=1:L1 for i = 1:m for j=1:n if(k==L(i,j)) count(k) = count(k)+1; end end end end count max(count); k=61; tempimage=zeros(m,n); for i=1:m for j=1:n if(L(i,j)==k)

tempimage(i,j)=1; %%%%%%this loop is to see which portion of the image is selected at a particular value of k end end end %figure; %imshow(tempimage); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ratio=zeros(1,L1); %% ratio is the array to store the ratio of length and width of each segment x1=zeros(1,L1); x2=zeros(1,L1); y1=zeros(1,L1); y2=zeros(1,L1); length=zeros(1,L1); width=zeros(1,L1); for k=1:L1 temp=0; for i=1:m for j=1:n if(k==L(i,j)) temp=temp+1; if(temp==1) x1(k)=i; end if(temp==count(k)) x2(k)=i; end end

end end temp=0; for j=1:n for i=1:m if(k==L(i,j)) temp=temp+1; if(temp==1) y1(k)=j; end if(temp==count(k)) y2(k)=j; end end end end length(k) = x2(k)-x1(k); width(k) = y2(k)-y1(k); if(length(k) ~= 0 && width(k) ~= 0 ) ratio(k)=length(k)/width(k); end end x1; x2; y1; y2; ratio temp=0; skinthresh=zeros(1,L1); for k=1:L1 if(length(k)~=0 && width(k)~=0) skinthresh(k)=count(k)/(length(k).*width(k)); end end skinthresh %%% we define a golden ratio(gr) and tolerance(t)to check wether a given segment has %%% dimensions of the face or not finaloutput=zeros(m,n); gr= (1+sqrt(5))/2 t=.65; gr1=gr+t

gr2=gr-t figure; imshow(img); hold('on') for k=1:L1 if(gr2<=ratio(k) && ratio(k)<=gr1 && skinthresh(k)>=.56 && count(k)>100) color('red'); rectangle('position',[y1(k),x1(k),width(k),length(k)]); k count(k) ratio(k) for i=1:m for j=1:n if(L(i,j)==k) finaloutput(i,j)=1; end end end end end figure; imshow(finaloutput); %%%below is code for cb vs cr graph for skin region cb1=zeros(1,noofskinpixels+1); cr1=zeros(1,noofskinpixels+1); y1=zeros(1,noofskinpixels+1); temp=1; for i=1:m for j=1:n if(combo_skin(i,j)==1) cb1(temp)=cr(i,j); cr1(temp)=cb(i,j); y1(temp)=y(i,j); temp=temp+1; end end end %figure; %stem(cb1,'b.');title('cb'); %figure; %stem(cr1,'b.');title('cr');

%figure; %stem(y1,'b.');title('y'); figure stem(cb1,y1,'r.'); %figure; %stem(cb1,'r.'); %hold('on'); %stem(cr1,'g.'); %stem(y,'b.');

You might also like