To crop the detected face parts
1 次查看(过去 30 天)
显示 更早的评论
This is the pgm for face parts detection.In this i wanted to crop the detected parts. So i wanted to knw how to use "imcrop" in this pgm..my pgm uses a computer vision toolbox. Can anyone please help me out..
function [bbox,bbX,faces,bbfaces] = detectFaceParts(detector,X,thick)
if( nargin < 3 )
thick = 1;
end
% disp(nargin);
%%%%%%%%%%%%%%%%%%%%%%%detect face %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Detect faces
bbox = step(detector.detector{5}, X);
% disp(bbox);
% figure,imshow(bbox);
bbsize = size(bbox);
% disp(bbsize);
% figure,imshow(bbsize);
partsNum = zeros(size(bbox,1),1);
% disp(partsNum);
%%%%%%%%%%%%%%%%%%%%%%%detect parts %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nameDetector = {'LeftEye'; 'RightEye'; 'Mouth'; 'Nose'; };
% disp(nameDetector);
mins = [[12 18]; [12 18]; [15 25]; [15 18]; ];
% disp(mins);
stdsize = detector.stdsize;
% disp(stdsize);
for k=1:4
if( k == 1 )
region = [1,int32(stdsize*2/3); 1, int32(stdsize*2/3)];
elseif( k == 2 )
region = [int32(stdsize/3),stdsize; 1, int32(stdsize*2/3)];
elseif( k == 3 )
region = [1,stdsize; int32(stdsize/3), stdsize];
elseif( k == 4 )
region = [int32(stdsize/5),int32(stdsize*4/5); int32(stdsize/3),stdsize];
else
region = [1,stdsize;1,stdsize];
end
% disp(region);
% figure,imshow(region);
bb = zeros(bbsize);
for i=1:size(bbox,1)
XX = X(bbox(i,2):bbox(i,2)+bbox(i,4)-1,bbox(i,1):bbox(i,1)+bbox(i,3)-1,:);
XX = imresize(XX,[stdsize, stdsize]);
% XX = imcrop(XX,bbox);
% disp(bbox);
% figure,imshow(bbox);
XX = XX(region(2,1):region(2,2),region(1,1):region(1,2),:);
b = step(detector.detector{k},XX);
% disp(b);
% figure,imshow(b);
% disp(size(b,1));
if( size(b,1) > 0 )
partsNum(i) = partsNum(i) + 1;
if( k == 1 )
b = sortrows(b,1);
elseif( k == 2 )
b = flipud(sortrows(b,1));
elseif( k == 3 )
b = flipud(sortrows(b,2));
elseif( k == 4 )
b = flipud(sortrows(b,3));
end
ratio = double(bbox(i,3)) / double(stdsize);
% disp(ratio);
b(1,1) = int32( ( b(1,1)-1 + region(1,1)-1 ) * ratio + 0.5 ) + bbox(i,1);
b(1,2) = int32( ( b(1,2)-1 + region(2,1)-1 ) * ratio + 0.5 ) + bbox(i,2);
b(1,3) = int32( b(1,3) * ratio + 0.5 );
b(1,4) = int32( b(1,4) * ratio + 0.5 );
bb(i,:) = b(1,:);
% disp(ratio);
end
end
bbox = [bbox,bb];
% disp(bbox);
% figure,imshow(bbox);
p = ( sum(bb') == 0 );
bb(p,:) = [];
end
% figure(7),imshow(bb);
%%%%%%%%%%%%%%%%%%%%%%%draw faces %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bbox = [bbox,partsNum];
% disp(partsNum);
% figure,imshow(partsNum);
bbox(partsNum<=2,:)=[];
if( thick >= 0 )
t = (thick-1)/2;
t0 = -int32(ceil(t));
t1 = int32(floor(t));
else
t0 = 0;
t1 = 0;
end
bbX = X;
boxColor = [[0,255,0]; [255,0,255]; [255,0,255]; [0,255,255]; [255,255,0]; ];
for k=5:-1:1
shapeInserter = vision.ShapeInserter('BorderColor','Custom','CustomBorderColor',boxColor(k,:));
% disp(shapeInserter);
for i=t0:t1
bb = int32(bbox(:,(k-1)*4+1:k*4));
bb(:,1:2) = bb(:,1:2)-i;
bb(:,3:4) = bb(:,3:4)+i*2;
bbX = step(shapeInserter, bbX, bb);
% disp(bbX);
% figure,imshow(bbX);
% X=imcrop(bbX,bbox);
end
end
% figure(8),imshow(bbX);
%%%%%%%%%%%%%%%%%%%%%%%faces %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if( nargout > 2 )
faces = cell(size(bbox,1),1);
boxfaces = cell(size(bbox,1),1);
% disp(size(bbox,1));
% disp(boxfaces);
% figure,imshow(boxfaces);
for i=1:size(bbox,1)
faces{i,1} = X(bbox(i,2):bbox(i,2)+bbox(i,4)-1,bbox(i,1):bbox(i,1)+bbox(i,3)-1,:);
bbfaces{i,1} = bbX(bbox(i,2):bbox(i,2)+bbox(i,4)-1,bbox(i,1):bbox(i,1)+bbox(i,3)-1,:);
% figure,imshow(bbfaces{i,1});
% pause;
end
end
0 个评论
采纳的回答
Walter Roberson
2013-3-18
You can pass the cropping rectangle into imcrop()
6 个评论
Walter Roberson
2013-3-18
What is size(X) and class(X) and size(bbox) ? Does it make a difference if you use bbox(i,:).' instead of bbox(i,:) there?
I looked through your code but I could not figure out from your variable names and comments (ahem!) which variables represent the detected eyes, nose, and mouth ?
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!