function output = face(negative,positive)
%
%function output = face(negative,positive)
%
%Read in a labeled image 'positive' with regions potentially containing faces
%Use binary negative image containing hole regions and use to find face objects
%Output is a binary image containing only face objects
%

[neg,map] = gifread(negative);
[pos,map] = gifread(positive);

output = zeros(size(pos));

ROWS = size(pos,1);
COLS = size(pos,2);

%Make neg a binary image (Matlab makes the image with 1s and 2s, not 0s and 1s)
%
for r=1:ROWS,
 for c=1:COLS,
	
	if neg(r,c)==1
	 neg(r,c)=0;
	else
	 neg(r,c)=1;
	end
 end
end

%Remove tiny holes from binary negative image
%
neg = bwmorph(neg,'clean');

%A: dilate the negative image objects
%B: multiply image A with the labeled positive image to see which
%   objects have holes 
%
A = dilate(neg,'fatten');
B = A.*pos;

%Find out how many unique objects have holes.
%Create a vector to store the values of theses objects.
%
[dummy_image,dummy_map] = cmunique(B);
num_objs = size(dummy_map,1);
obj_val = zeros(num_objs,1);

%The pixels in image B will have values cooresponding to 
%the object the hole is inside.  Make an image with only 
%these objects
%
i=2;
obj_val(2,1)=1;

for r=1:ROWS,
 for c=1:COLS,
	
	been_found = 0;

	for z=1:size(obj_val),

		if B(r,c) == obj_val(z,1)
		 been_found = 1;
		end

	end

	if been_found == 0
	 i = i+1;
	 obj_val(i,1) = B(r,c);
	 

		for y=1:ROWS,
		 for x=1:COLS,
		
			if pos(y,x)==B(r,c)
			 output(y,x)=1;
			end
		 end
		end

	end

 end
end

end

    Source: geocities.com/jaykapur