Sei sulla pagina 1di 17

DIGITAL IMAGE PROCESSING

PROJECT: 3

BY
NC Shakeela Naz
MSCS-17

DATED
5TH JAN, 2011
1. Problem definition:-

A program that reads a binary image in .PPM format after prompting the user for the file name of that
image (There should be no other input.). The program will identify regions in the input image that are
potentially a human face.

2. Summary of algorithm:-
Top-Down development approach is used for the required program.
Block diagram below shows the working of the program:-

See Appendix B for MATLAB implementation.


Reuse of outside code:
None.
3. Example image and results of running the program on it:-

Above: Input Image TestImage1.ppm


Above: After Color Segmentation
Above: After converting to binary and applying morphological operations (opening by disk of size 1,
then filing holes and then opening again by disk of size 3)
Above: After labeling
Above: After discarding areas smaller than 200 pixels
Above: After discarding arms (by discarding areas with Eccentricity greater than 0.96)
Above: Color Segmented image after Morphological Processing and Connected region analysis
Above: Snapshot of output file OutPutTestImage1.ppm
Above: zoomed in snap-shot of OutPut file OutPutTestImage1.ppm
RESULTS:-
• Adjacent (overlapping) faces merged by program because of being part of same connected
region.
• Program successfully eliminated arm and elongated hand; but not curled hands (like fist). This
happened because arm and stretched / elongated hand and fingers had high Eccentricity, while
the Eccentricity of fist/curled hands was closer to the Eccentricity of faces.
APPENDIX A
REFRENCES:-

• MATLAB Central website : http://www.mathworks.com

• MATLAB Help.
APPENDIX B
MATLAB CODE:-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%--------------------------------------------%%%%%%%%%
%%%%%~*~*~*~ NC Shakeela Naz --- MSCS-17 ~*~*~*~%%%%%
%%%%%~*~*~*~ Digital Image Processing ~*~*~*~%%%%%
%%%%%~*~*~*~ Project 3 ~*~*~*~%%%%%
%%%%%~*~*~*~ 5th January, 2011 ~*~*~*~%%%%%
%%%%%%%%%--------------------------------------------%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Reading/Loading the PPM File %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Load the Color Image(selected by user) in 'inImg' Variable(matrix).


%
clc; % clear command window
clear; % clear work space
Imgread=0;
while(Imgread==0)
[F,P]=uigetfile('*ppm;','Choose Image');
if F==0
return;
else
PF=[ P , F ];
ext=PF(findstr(PF,'.')+1:end);
try
inRGB=imread(PF,'ppm');
Imgread=1;
catch lasterr
s1=lasterr;
if(s1=='Attempt to reference field of non-structure array.')
%if format mismatch; lasterr=Attempt to reference field of non-structure array.
Imgread=0
h=errordlg('File format mis-match','ERROR!')
uiwait(h);
uiresume;
end
end
end
end

figure,imshow(inRGB);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%%%%%%%%%%%%%%%%%%%% Convert to HSV %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
InImg=inRGB;
hsvImg=rgb2hsv(inRGB);
[EndR EndC Channel]=size(hsvImg);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%%%%%%%%%% FACE EXTRACTION %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
for i=1:EndR
for j= 1:EndC %%% only H and channel is needed
if ~(((0.01<=hsvImg(i,j,1) && hsvImg(i,j,1)<=0.06) || (0.96<=hsvImg(i,j,1) && hsvImg(i,j,1)<=1.0)))
inRGB(i,j,1)=0;
inRGB(i,j,2)=0;
inRGB(i,j,3)=0;
end
end
end
figure,imshow(inRGB);

%convert color segmented img 2 grayscale......light greyshades correspond 2


%faces....others are to b discarded

graySeg=rgb2gray(inRGB);
figure,imshow(graySeg);

BinSeg=graySeg;
BinSeg(BinSeg<80)=0;
BinSeg(BinSeg>=80)=1;

%binary morphology on BinSeg

%%
BinSeg=logical(BinSeg);
figure,imshow(BinSeg);

se=strel('disk',1);
BinSeg=imopen(BinSeg,se);
figure,imshow(BinSeg);

BinSeg=imfill(BinSeg,'holes');
figure,imshow(BinSeg);
se=strel('disk',3);
BinSeg=imopen(BinSeg,se);
figure,imshow(BinSeg);

%%% Connected Region Analysis

BinSeg=bwlabel(BinSeg);
figure,imagesc(BinSeg);

%discard regions with areas less than 201

STATS = regionprops(BinSeg,'Area');
[c r]=size(STATS);

for i=1:c
if STATS(i).Area<=200 % if area of label c is less than or equal to 200
BinSeg(BinSeg==i)=0;
end
end

BinSeg2=bwlabel(BinSeg);
figure,imagesc(BinSeg2);

%discard regions with high Eccentricity

STATS2 = regionprops(BinSeg2,'Eccentricity');
[c2 r2]=size(STATS2);
for i=1:c2
if STATS2(i).Eccentricity>0.96 % if Eccentricity of label c is greater than 0.96
BinSeg2(BinSeg2==i)=0;
end
end
[BinSeg3 n]=bwlabel(BinSeg2);
figure,imagesc(BinSeg3);

%Color Segmented image after Morphological Processing and Connected region analysis

for i=1:EndR
for j= 1:EndC
if BinSeg3(i,j)==0
inRGB(i,j,1)=0;
inRGB(i,j,2)=0;
inRGB(i,j,3)=0;
end
end
end
figure,imshow(inRGB);

%Bounding boxes
STATS3 = regionprops(BinSeg3,'BoundingBox');

for j=1:n
x1=uint8(STATS3(j).BoundingBox(1,1));
y1=uint8(STATS3(j).BoundingBox(1,2));
x2=uint8(x1 + STATS3(j).BoundingBox(1,3));
y2=uint8(y1 + STATS3(j).BoundingBox(1,4));

InImg(y1,x1:x2,1:3)=255;
InImg(y2,x1:x2,1:3)=255;
InImg(y1:y2,x1,1:3)=255;
InImg(y1:y2,x2,1:3)=255;

x1=0;
x2=0;
y1=0;
y2=0;
end

% writing output file in work directory, in Output+Filename. ppm format, with bounding boxes on potential
face regions

figure,imshow(InImg);
d='OutPut';
fileOut=[d,F];
imwrite(InImg,fileOut,'ppm');

Potrebbero piacerti anche