Search code examples
matlabface-detection

Face detection and give filter


FDetect = vision.CascadeObjectDetector;

%// Read the input image

I = imread('face.jpg');

%// Returns Bounding Box values based on number of objects
BB = step(FDetect,I);

figure,
imshow(I); 
hold on
for i = 1:size(BB,1)
   rectangle('Position',BB(i,:),'LineWidth',2.5,'LineStyle','','EdgeColor','r');
end
title('Face Detection');
hold off;

filter:

E = imerode(I,strel('square',5));

figure, imshow(E);

My problem is how to give filters only IN rectangle face area.

enter image description here


Solution

  • You will want to extract just region of your image that is within the bounding box, and then apply your imerode operation on just that part.

    The Bounding Box is of the form [x,y,width,height]. We will need to convert this to pixels.

    %// Ensure that we are working with integers.
    BB(k,:) = round(BB(k,:));
    
    %// Sample all rows and columns within this bounding box
    rows = BB(k,2):sum(BB(k,[2 4]))
    cols = BB(k,1):sum(BB(k,[1 3]))
    
    %// Apply the imerode operation on just these pixels
    I(rows,cols) = imerode(I(rows,cols), strel('square', 5));