How to crop images and save it to matrix after vision.For​egroundDet​ector

Hello. Vision.ForegroundDetector is so amazing!
Could you let me know how to get several images inbox as a matrix?
I want to save these images as matrix.
I want to use several images like this. How can I crop this like image?
I am using this code.
videoSource = VideoReader('shahar_run.avi');
detector = vision.ForegroundDetector(...
'NumTrainingFrames', 5, ...
'InitialVariance', 30*30);
blob = vision.BlobAnalysis(...
'CentroidOutputPort', false, 'AreaOutputPort', false, ...
'BoundingBoxOutputPort', true, ...
'MinimumBlobAreaSource', 'Property', 'MinimumBlobArea', 250);
shapeInserter = vision.ShapeInserter('BorderColor','White');
videoPlayer = vision.VideoPlayer();
while hasFrame(videoSource)
frame = readFrame(videoSource);
fgMask = detector(frame);
bbox = blob(fgMask);
out = shapeInserter(fgMask,bbox);
videoPlayer(out);
pause(0.1);
end

 Accepted Answer

Check the following code
videoSource = VideoReader('denis_wave2.avi');
detector = vision.ForegroundDetector(...
'NumTrainingFrames', 5, ...
'InitialVariance', 30*30);
blob = vision.BlobAnalysis(...
'CentroidOutputPort', false, 'AreaOutputPort', false, ...
'BoundingBoxOutputPort', true, ...
'MinimumBlobAreaSource', 'Property', 'MinimumBlobArea', 50);
shapeInserter = vision.ShapeInserter('BorderColor','White');
videoPlayer = vision.VideoPlayer();
bounding_boxes = {};
while hasFrame(videoSource)
frame = readFrame(videoSource);
fgMask = detector(frame);
bbox = blob(fgMask);
for i=1:size(bbox, 1)
x = bbox(i, :);
bounding_boxes{end+1} = fgMask(x(2):sum(x([2 4])), x(1):sum(x([1 3])));
end
out = shapeInserter(fgMask,bbox);
videoPlayer(out);
pause(0.1);
end
The bounding_boxes is a cell array and its each element is a cropped image. You can check the output as
imshow(bounding_boxes{1})
imshow(bounding_boxes{2})

6 Comments

Thank you so much.
Could you explain how to save several bounding_boxes as a matrix in the video?
I want to use the matrix for next step like classification.
Kong, this code save all the bounding boxes in the cell array bounding_boxes. The basic concept is this: each row in bbox represent a represent [x y width height] around the blob. You just need to use correct indexing to extract that blob from the image. For exaple
fgMask(x(2):sum(x([2 4])), x(1):sum(x([1 3])))
is equivalent to
fgMask(y:y+height, x:x+width)
Thanks a lot.
When I used this code, I got 8 bounding_boxes images.
Could I get an idea to obtain good shape of human motion for classification?
Each image is not good for classification.
figure;
subplot(181); imshow( bounding_boxes{1} );
subplot(182); imshow( bounding_boxes{2} );
subplot(183); imshow( bounding_boxes{3} );
subplot(184); imshow( bounding_boxes{4} );
subplot(185); imshow( bounding_boxes{5} );
subplot(186); imshow( bounding_boxes{6} );
subplot(187); imshow( bounding_boxes{7} );
subplot(188); imshow( bounding_boxes{8} );
I am not sure how to do that. It requries a fairly domain specific knowledge. Maybe try to start a new question so that someone with the required expertise can answer or trying posting such a question on a forum related to computer vision.
Get state of the art algorithms in gesture recognition here in Vision Bibliography

Sign in to comment.

More Answers (0)

Asked:

on 19 Mar 2020

Commented:

on 22 Mar 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!