Mapping bounding box to the original image

11 views (last 30 days)
Hi, i have a real time skin detection algorithm where it gives me a bounding box around the skin region with rectangle('Position',bb,'EdgeColor','r','LineWidth',2) from an original image. I wish to use the code to first detect the skin region from the original image before i use Viola Jones to detect the face region from the cropped skin region. I wish to know after i crop the skin region then use the face detection algorithm to detect the face, how can i map the bounding box of the face to the original image .
The code for skin detection and VJ face detection is as below for those who want to know.
function cameraon_Callback(hObject, eventdata, handles)
% hObject handle to cameraon (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global video;
global videoFrame;
axes(handles.axes1);
video = videoinput('winvideo',1,'YUY2_320x240');
set(video,'ReturnedColorSpace','rgb');
handles.video=video;
triggerconfig(video,'manual');
video.FramesPerTrigger = 1;
guidata(hObject,handles);
faceDetector=vision.CascadeObjectDetector('FrontalFaceCART');
faceDetector.MinSize=[20 20];
faceDetector.MergeThreshold = 20;
videoFrame=getsnapshot(video);
bbox=step(faceDetector,videoFrame);
if numel(bbox) == 0
errordlg('Face not detected. Please try again.');
set(handles.cameraon,'String','Start Camera')
stop(video);
delete(video);
clear;
else
axes(handles.axes1);
start(video);
end
while(true)
frame=getsnapshot(video);
%Detect faces.
data = frame;% this is to read a image from data base. just put any image name u want to give make sure its placed in bin
diff_im = imsubtract(data(:,:,1), rgb2gray(data)); % deleting gray scale pixels from image
diff_im = medfilt2(diff_im, [3 3]); %applying filter one
diff_im = imadjust(diff_im); % adjust image function to fill small holes (check all the function's functionality to have idea of whats going on)
level = graythresh(diff_im);% extract level value
bw = im2bw(diff_im,level);
BW5 = imfill(bw,'holes');
bw6 = bwlabel(BW5, 8);
stats = regionprops(bw6,['basic']);%basic mohem nist
measurements = regionprops(bw6, 'boundingbox');
BB1=struct2cell(measurements);
BB2=cell2mat(BB1);
a = BB2(1);
b = BB2(2);
c = BB2(3);
d = BB2(4);
[N,M]=size(stats);
if (bw==0)% check if there is no skin color then exit
break;
else
tmp = stats(1);
for i = 2 : N % checking for biggest hole to mark it as face
if stats(i).Area > tmp.Area
tmp = stats(i);
end
end
bb = tmp.BoundingBox; % applying identification square to mark skin color region
bc = tmp.Centroid;
videoFrame=getsnapshot(video);
This is the place where i cannot put the bounding box back to the original image.
skinImage = imcrop(videoFrame,bb(1,:));
bbox = step(faceDetector,skinImage);
bbox(1,1:2) = bbox(1,1:2) + bb(1,1:2);
videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox,'Face');
cla;
imshow(videoOut,[]);
drawnow;
pause(0.0001);
end
end
guidata(hObject,handles);

Answers (1)

Image Analyst
Image Analyst on 4 May 2015
Why do you think that regionprops does not return bounding box coordinates in the coordinate system of the image you passed to it? It will.
  6 Comments
Jia Zhen
Jia Zhen on 5 May 2015
Hi, sorry to bother you again but i am having this error for
videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox + bb(1,:),'Face');
Error using +
Matrix dimensions must agree.
Error in realtime4>cameraon_Callback (line 153)
videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox +
bb(1,:),'Face');
When i change
bbox(1,1:2) = bbox(1,1:2) + bb(1,1:2);
to
bbox = bbox + bb(1,:);
i still get this error
Error using + Matrix dimensions must agree.
Error in realtime4>cameraon_Callback (line 153) bbox = bbox + bb(1,:);
Image Analyst
Image Analyst on 5 May 2015
Just try putting everything into single variables like x,y,width, and height, or row1, row2, col1, col2. Then examine what they are, and what makes sense - what they should be. I'm sure you can figure it out then.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!