MATLAB: How to crop detected face part

image analysis

a=imread('C:\Users\SanaUllah\Documents\MATLAB\face.jpg');
a=imresize(a,1);
subplot(3,3,1);
imshow(a);
detector=vision.CascadeObjectDetector('mouth');
detector.MergeThreshold=60;
boundingbox=step(detector,a);
out=insertObjectAnnotation(a,'rectangle',boundingbox,'mouth Detected','LineWidth',3,'TextColor','black');
subplot(3,3,2);
imshow(out)

Best Answer

Try this:
rgbImage = imread('face.jpeg');
rgbImage = imresize(rgbImage,1);
subplot(2, 3, 1);
imshow(rgbImage);
axis('on', 'image');
drawnow;
detector=vision.CascadeObjectDetector('mouth');
detector.MergeThreshold=60;
allBoundingBoxes=step(detector,rgbImage)
out = insertObjectAnnotation(rgbImage,'rectangle',allBoundingBoxes,'mouth Detected','LineWidth',3,'TextColor','black');
subplot(2, 3, 2);
imshow(out)
axis('on', 'image');
% Crop out each bounding box to a new image.
for k = 1 : size(allBoundingBoxes, 1)
% Crop the image.
thisBoundingBox = allBoundingBoxes(k, :);
croppedImage = imcrop(rgbImage, thisBoundingBox);
% Display it
subplot(2, 3, k+2);
imshow(croppedImage)
axis('on', 'image');
end
0000 Screenshot.png