Problem with defining an argument (face detection)

Hello. I downloaded a source code to learn something more about the face detection in matlab, but when I try to run the program I get this kind of massage:
??? Input argument "inputimage" is undefined.
Error in ==> face_detection at 7
[likely_skin]=get_likelyhood(inputimage,rmean,bmean,rbcov);
This is the function that is not working:
function face_detection(inputfile)
[rmean,bmean,rbcov]=make_model();
[likely_skin]=get_likelyhood(inputfile,rmean,bmean,rbcov);
[skinBW,opt_th] = segment_adaptive(likely_skin);
[erodedBW]=label_regions(skinBW);
[eulerBW]=euler_test(erodedBW);
[aspectBW]=aspect_test(eulerBW);
[templateBW]=template_test(aspectBW,inputfile,'template.jpg');
[K,P]=bwlabel(templateBW,8);
s = regionprops(bwlabel(templateBW), 'centroid');
centroids = cat(1, s.Centroid);
subplot(4,3,12);
imshow(imread(inputfile))
if(P>0)
hold on
plot(centroids(:,1), centroids(:,2), 'b*')
hold off
end
title('Final Detection')
clear all
It would be helpful if someone could help me. If u need i can post all of the sorce code. Or post link to the code.

답변 (1개)

Walter Roberson
Walter Roberson 2011년 10월 26일

0 개 추천

The source code you show does not match your error message. The error message has inputimage as the first argument to get_likelyhood, but the code you show has inputfile in that location.
Perhaps you did not save the code after you modified it?

댓글 수: 4

Pawe?
Pawe? 2011년 10월 26일
이동: DGM 2023년 12월 29일
I will add the whole source code of this program. I tried to change few things, but i get always the same results.
[??? Input argument "inputfile" is undefined.
Error in ==> face_detection at 7
[likely_skin]=get_likelyhood(inputfile,rmean,bmean,rbcov);]
Code:
%This function detect faces in an image
function face_detection(inputfile)
[rmean,bmean,rbcov]=make_model();
[likely_skin]=get_likelyhood(inputfile,rmean,bmean,rbcov);
[skinBW,opt_th] = segment_adaptive(likely_skin);
[erodedBW]=label_regions(skinBW);
[eulerBW]=euler_test(erodedBW);
[aspectBW]=aspect_test(eulerBW);
[templateBW]=template_test(aspectBW,inputfile,'template.jpg');
[K,P]=bwlabel(templateBW,8);
s = regionprops(bwlabel(templateBW), 'centroid');
centroids = cat(1, s.Centroid);
subplot(4,3,12);
imshow(imread(inputfile))
if(P>0)
hold on
plot(centroids(:,1), centroids(:,2), 'b*')
hold off
end
title('Final Detection')
clear all
%***********************************FUNCTIONS******************************
%**************************************************************************
% Following function returns cromatic values of an input image file
function [cr, cb] = get_crcb(filename)
im= imread(filename);
imycc = rgb2ycbcr(im);
lpf = 1/9 * ones(3);
cr = imycc(:,:,3);
cb = imycc(:,:,2);
cr = filter2(lpf, cr);
cb = filter2(lpf, cb);
cr = reshape(cr, 1, prod(size(cr)));
cb = reshape(cb, 1, prod(size(cb)));
%**************************************************************************
%Following function plots 2D Chromatic Histogram
function plot_hist2d(cr, cb)
hist2d = zeros(256);
cr = round(cr);
cb = round(cb);
for i = 1:length(cr)
hist2d(cr(i),cb(i)) = hist2d(cr(i),cb(i))+1;
end
surf(hist2d)
%**************************************************************************
%Following fnction fit the statistics of sample chrominance values into
%2D Gaussian model and returns the parameters of model
function [rmean,bmean,rbcov]=make_model()
[cr1, cb1] = get_crcb('sampleset/1.jpg');
[cr2, cb2] = get_crcb('sampleset/2.jpg');
[cr3, cb3] = get_crcb('sampleset/3.jpg');
[cr4, cb4] = get_crcb('sampleset/4.jpg');
[cr5, cb5] = get_crcb('sampleset/5.jpg');
[cr6, cb6] = get_crcb('sampleset/6.jpg');
[cr7, cb7] = get_crcb('sampleset/7.jpg');
[cr8, cb8] = get_crcb('sampleset/8.jpg');
[cr9, cb9] = get_crcb('sampleset/9.jpg');
[cr10, cb10] = get_crcb('sampleset/10.jpg');
[cr11, cb11] = get_crcb('sampleset/11.jpg');
[cr12, cb12] = get_crcb('sampleset/12.jpg');
[cr13, cb13] = get_crcb('sampleset/13.jpg');
%concatenate all values
cr = [cr1 cr2 cr3 cr4 cr5 cr6 cr7 cr8 cr9 cr10 cr11 cr12 cr13];
cb = [cb1 cb2 cb3 cb4 cb5 cb6 cb7 cb8 cb9 cb10 cb11 cr12 cb13];
rmean = mean(cr);
bmean = mean(cb);
rbcov = cov(cr,cb);
jointchart = zeros(256);
for r = 0:255
for b = 0:255
x = [(r - rmean);(b - bmean)];
jointchart(r+1,b+1) = [power(2*pi*power(det(rbcov),0.5),-1)]*exp(-0.5* x'*inv(rbcov)* x);
end
end
subplot(4,3,1);
plot_hist2d(cr,cb)
title('2D Chromatic Histogram Model')
subplot(4,3,2);
surf(jointchart)
title('Joint Gaussian Distribution Model')
%**************************************************************************
%Following function convert original image into normalized skin likelyhood image
function[likely_skin]=get_likelyhood(filename,rmean,bmean,rbcov)
img = imread(filename);
imycbcr = rgb2ycbcr(img);
[m,n,l] = size(img);
likely_skin = zeros(m,n);
for i = 1:m
for j = 1:n
cr = double(imycbcr(i,j,3));
cb = double(imycbcr(i,j,2));
x = [(cr-rmean);(cb-bmean)];
likely_skin(i,j) = [power(2*pi*power(det(rbcov),0.5),-1)]*exp(-0.5* x'*inv(rbcov)* x);
end
end
lpf= 1/9*ones(3);
likely_skin = filter2(lpf,likely_skin);
likely_skin = likely_skin./max(max(likely_skin));
subplot(4,3,3);
imshow(img, [0 1])
title('Original RGB Image')
subplot(4,3,4);
imshow(likely_skin, [0 1])
title('Skin Likelyhood Image')
%**************************************************************************
%Following function take likelyhood image as input and segment it into binary image by
%setting the threshold adaptively
function [binary_skin,opt_th] = segment_adaptive(likely_skin)
[m,n] = size(likely_skin);
temp = zeros(m,n);
diff_list = [];
high=0.55;
low=0.01;
step_size=-0.1;
bias_factor=1;
indx_count=[(high-low)/abs(step_size)]+2;
for threshold = high:step_size:low
binary_skin = zeros(m,n);
binary_skin(find(likely_skin>threshold)) = 1;
diff = sum(sum(binary_skin - temp));
diff_list = [diff_list diff];
temp = binary_skin;
end
[C, indx] = min(diff_list);
opt_th = (indx_count-indx)*abs(step_size)*bias_factor;
binary_skin = zeros(m,n);
binary_skin(find(likely_skin>opt_th)) = 1;
subplot(4,3,5);
imshow(binary_skin, [0 1])
title('Skin Segmented Image')
%**************************************************************************
%Following function applies erosion to binary image and mark the regions
function[labelBW]=label_regions(binary_skin)
[m,n] = size(binary_skin);
% %Apply Minimal Erosion to open up some small holes
% se0 = strel('disk',2);
% erodedBW0=zeros(m,n);
% erodedBW0= imerode(binary_skin,se0);
%Fill the regions
filledBW=zeros(m,n);
filledBW = imfill(binary_skin,'holes');
se2 = strel('disk',10);
erodedBW=zeros(m,n);
erodedBW = imerode(filledBW,se2);
subplot(4,3,6);
imshow(erodedBW)
title({'After Erosion';'(disk size: 10)'})
se1 = strel('disk',8);
dilateBW=zeros(m,n);
dilateBW=imdilate(erodedBW,se1);
dilateBW = immultiply(dilateBW,binary_skin);
subplot(4,3,7);
imshow(dilateBW)
title({'After Dilation';'(disk size: 8)'})
labelBW=zeros(m,n);
[labelBW,num] = bwlabel(dilateBW,8);
color_regions=zeros(m,n);
color_regions= label2rgb(labelBW, 'hsv', 'black', 'shuffle');
subplot(4,3,8);
imshow(color_regions)
title({'Labeled Regions';['(',num2str(num),' regions)']})
%**************************************************************************
%Following function filters regions which has at least one hole
function [eulerBW]=euler_test(labelBW)
e = regionprops(labelBW,'EulerNumber');
eulers=cat(1,e.EulerNumber);
holes=1-eulers;
region_index = find(holes>=1);
[m,n]=size(labelBW);
eulerBW=zeros(m,n);
for i=1:length(region_index)
[x,y] = find(bwlabel(labelBW) == region_index(i));
bwsegment = bwselect(labelBW,y,x,8);
eulerBW=eulerBW+bwsegment;
end
subplot(4,3,9);
imshow(eulerBW)
title({'After Euler Test';['(',num2str(length(region_index)),' regions)']})
%**************************************************************************
%Following function determines whether the region aspect ratio is within
%range of being a face region
function [aspectBW]=aspect_test(eulerBW)
[m,n]=size(eulerBW);
filledBW = imfill(eulerBW,'holes');
se1 = strel('disk',3);
growBW=zeros(m,n);
growBW=imdilate(filledBW,se1);
[labels,num] = bwlabel(growBW,8);
[aspect_ratio]=get_aspect(labels);
region_index = find(aspect_ratio<=3.5 & aspect_ratio>=1);
aspectBW=zeros(m,n);
for i=1:length(region_index)
[x,y] = find(bwlabel(filledBW) == region_index(i));
bwsegment = bwselect(filledBW,y,x,8);
aspectBW=aspectBW+bwsegment;
end
subplot(4,3,10);
imshow(aspectBW)
title({'After Aspect Ratio Test';['(',num2str(length(region_index)),' regions)']})
%**************************************************************************
%Following function returns image regions which pass template matching test
function [template_passed]=template_test(aspectBW,originalRGB,template)
imgray=rgb2gray(imread(originalRGB));
imtemplate=imread(template);
[labels,num] = bwlabel(aspectBW,8);
[m,n]=size(aspectBW);
orient = regionprops(labels,'Orientation');
angles=cat(1,orient.Orientation);
c = regionprops(labels,'Centroid');
centroids=cat(1,c.Centroid);
template_passed=zeros(m,n);
gray_matched=zeros(m,n);
for j=1:num,
[x,y] = find(labels == j);
bwsegment = bwselect(aspectBW,y,x,8);
oneface=immultiply(bwsegment,imgray);
cx1=centroids(j,1);
cy1=centroids(j,2);
p=regionprops(bwlabel(bwsegment),'BoundingBox');
boxdim=cat(1,p.BoundingBox);
regw=boxdim(3);
regh=boxdim(4);
ratio=regh/regw;
if(ratio>1.6)
regh=1.5*regw;
cy1=cy1-(0.1*regh);
end
gmodel_resize=imresize(imtemplate,[regh regw],'bilinear');
if(angles(j)>0)
gmodel_rotate=imrotate(gmodel_resize,angles(j)-90,'bilinear','loose');
else
gmodel_rotate=imrotate(gmodel_resize,90+angles(j),'bilinear','loose');
end
bwmodel=im2bw(gmodel_rotate,0);
[g,h]=size(bwmodel);
bwmorphed = bwmorph(bwmodel,'clean');
[L,no]=bwlabel(bwmorphed,8);
if(no==1)
bwsingle=bwmorphed;
else
ar=regionprops(bwlabel(bwmorphed),'Area');
areas=cat(1,ar.Area);
[C,I]=max(areas);
[x1,y1] = find(bwlabel(bwmorphed)== I);
bwsingle = bwselect(bwmorphed,y1,x1,8);
end
filledmodel=regionprops(bwlabel(bwsingle),'FilledImage');
bwcrop=filledmodel.FilledImage;
[modh,modw]=size(bwcrop);
gmodel_crop=imresize(gmodel_rotate,[modh modw],'bilinear');
cenmod=regionprops(bwlabel(bwcrop),'Centroid');
central=cat(1,cenmod.Centroid);
cx2=central(1,1);
cy2=central(1,2);
mfit = zeros(size(oneface));
mfitbw = zeros(size(oneface));
[limy, limx] = size(mfit);
startx = cx1-cx2;
starty = cy1-cy2;
endx = startx + modw-1;
endy = starty + modh-1;
startx = checklimit(startx,limx);
starty = checklimit(starty,limy);
endx = checklimit(endx,limx);
endy = checklimit(endy,limy);
for i=starty:endy,
for j=startx:endx,
mfit(round(i),round(j)) = gmodel_crop(round(i-starty+1),round(j-startx+1));
end;
end;
gray_matched=gray_matched+mfit;
crosscorr =corr2(mfit,oneface);
if(crosscorr>=0.6)
template_passed=template_passed+bwsegment;
end;
subplot(4,3,11);
imshow(gray_matched,[0 255])
title('Template Matching')
end;
function [ratiolist] = get_aspect(inputBW)
major = regionprops(inputBW,'MajorAxisLength');
major_length=cat(1,major.MajorAxisLength);
minor = regionprops(inputBW,'MinorAxisLength');
minor_length=cat(1,minor.MinorAxisLength);
ratiolist=major_length./minor_length;
function newcoord = checklimit(coord,maxval)
newcoord = coord;
if (newcoord<1) newcoord=1; end;
if (newcoord>maxval) newcoord=maxval; end;
Walter Roberson
Walter Roberson 2011년 10월 26일
이동: DGM 2023년 12월 29일
How exactly are you invoking your code? If you are running it by pressing F5, or by clicking on Run, or by giving the command
face_detection
at the command line, then no value would be passed in for the inputfile and you would get the error you see.
You need to use the command line and invoke something like
face_detection('lena.jpg')
Pawe?
Pawe? 2011년 11월 4일
이동: DGM 2023년 12월 29일
Thank you wery much. Now i know what i was doing wrong. I have 1 more question how to change the centroid to rectangle? Is there a easy way to do it ?
Walter Roberson
Walter Roberson 2011년 11월 4일
이동: DGM 2023년 12월 29일
You can use the rectangle() function to plot rectangles.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Downloads에 대해 자세히 알아보기

질문:

2011년 10월 26일

이동:

DGM
2023년 12월 29일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by