Search code examples
matlabimage-processingface-detectionbounding-box

How do I save images with face detection bounding box using the Batch Image Processor app in Matlab


I'm a newbie to Matlab and I'm trying to perform face detection on a batch of images. I'm using this simple face detection code which works with the Image Processing Toolbox and the Computer Vision Toolbox. I'm using the Batch Image Processor App and wrote my code as a function that I apply to a batch of images. What I need as an outcome are the four values of each bounding box (this works), and I want to save each image with the bounding box drawn, in order to check if the face is detected correctly (which doesn't work). This is the function:

function results = facedetection(im)

FDetect = vision.CascadeObjectDetector('FrontalFaceLBP','MergeThreshold', 5);
BBface = step(FDetect,im);

figure,
imbb = imshow(im); hold on
for i = 1:size(BBface,1)
rectangle('Position',BBface(i,:),'LineWidth',2,'LineStyle','- 
','EdgeColor','r');
end
hold off;

imwrite(imbb,'test.jpg'); %this is the line that doesn't work

results.face=BBface; %this gives me the values of each bounding box

end

When I apply this function it provides me with the values of the bounding box, and in the imshow it shows the image with the bounding box around the face. However, the imwrite function doesn't work and gives the following error:

Error using imwrite (line 420) Expected DATA to be one of these types:  
numeric, logical  
Instead its type was matlab.graphics.primitive.Image.  
Error in facedetection (line 13) imwrite(imbb,'test.jpg'); 

Does anyone know how to solve this issue? Is it possible to save all images with the bounding box using the Batch Image Processor app and if so, how? Is it also possible to save the images without using imshow? I don't have to see the results directly, as long as I can save them.

I'm sorry if this question is too vague, but I hope someone can help me a bit further.

EDIT: I found out that the Batch Image Processor can be used to save the images, as long as the image is put into a results. string in the function (see documentation), but I don't know how to do this. I changed my code as followed:

function results = facedetection(im)

FDetect = vision.CascadeObjectDetector('FrontalFaceLBP','MergeThreshold', 5);
BBface = step(FDetect,im);

figure,
imbb = imshow(im); hold on
for i = 1:size(BBface,1)
rectangle('Position',BBface(i,:),'LineWidth',2,'LineStyle','- 
','EdgeColor','r');
end
hold off;

BBimage = imfuse (im, BBface); %this doesn't work

results.BBValues=BBface;
results.BBimage=BBimage;

end

This code returns a complete green hued image (?!). So probably something goes wrong in the imfuse part. How do I put the im and the drawn rectangle together in the BBimage?


Solution

  • I solved the problem using the insertShape function. The complete function becomes as follows:

    function results = facedetection(im)
    
    FDetect = vision.CascadeObjectDetector('FrontalFaceLBP','MergeThreshold', 3);
    BBvalues = step(FDetect,im);
    
    
    hold on
    for i = 1:size(BBvalues,1)
    BBimage = insertShape (im,'rectangle',BBvalues(i,:),'Color','yellow');
    end
    hold off;
    
    results.Values3=BBvalues;
    results.Image3=BBimage;
    
    end
    

    When this function is applied to a batch of images in the Batch Image Processor app, the 'export results of all processed images to files'-button allows you to save the images including the bounding box rectangle. It saves each image with its original filename and an additional '_Image3'. I'm using this code now to test the most effective threshold, which is why I named them Values3 and Image3 refering to the threshold 3. It works fine!