I want to crop a detected faces in my code. Here is my code.
function DisplayDetections(im, dets)
imshow(im);
k = size(dets,1);
hold on;
for i=1:k
rectangle('Position', dets(i,:),'LineWidth',2,'EdgeColor', 'r');
end
imcrop(rectangle);
hold off;
Their is syntax error in cropping. Can anybody help in cropping rectangle box detected in above box.
That code only draws the rectangles in your image. If you actually want to crop out portions of the image with the defined rectangles, use imcrop
.
As such, you would do something like this to store all of your cropped rectangles. This is assuming that im
and dets
are already defined in your code from your function:
k = size(dets,1);
cropped = cell(1,k);
for i=1:k
cropped{k} = imcrop(im, dets(i,:));
end
cropped
would be a cell array where each element will store a cropped image defined by each rectangle within your dets
array. This is assuming that dets
is a 2D array where there are 4 columns, and the number of rows determines how many rectangles you have. Each row of dets
should be structured like:
[xmin ymin width height]
xmin
, ymin
are the horizontal and vertical co-ordinate of the top-left corner of the rectangle, and width
and height
are the width and height of the rectangle.
If you want to access a cropped portion in the cell array, simply do:
crp = cropped{k};
k
would be the kth rectangle detected in your image.