how to crop the detected object in image with Object's bounding box using matlab

Saravanan picture Saravanan · Feb 21, 2014 · Viewed 7k times · Source

Hi. I am new to image processing. I want to extract detected region of image as new image. I detect the eye region of face using caseCadeObjectDetector method. Now I would like to know, how to extract the detected region of image. Here is what I did.

i=imread('test.jpg');
Eyedetect =  vision.CascadeObjectDetector('RightEye','MergeThreshold',24);
bbox=step(Eyedetect,i);

I plot that bounding box using insertObjectAnnotation method. It plots the line over the eye. But I want to crop that eye as new image. bbox is 1x4 matrix contains x , y, height, width. Can anybody help me? I am using MATLAB r2013a.

Answer

herohuyongtao picture herohuyongtao · Feb 21, 2014

To extract the sub-image with bbox in format [x, y, height, width], you can use:

subImage = i(bbox(1):bbox(1)+bbox(3), bbox(2):bbox(2)+bbox(4), :);

P.S. If bbox is in format [x, y, width, height], you can simply call imcrop:

subImage = imcrop(i, bbox);