How to use image segmentation in video images of folders
11 views (last 30 days)
Show older comments
Hello.
I have several videos. After convert videos to binary images, I want to convert binary images to centered images(cropped).
Binary images ---> cropped image
1.I will use this code to convert video to binary images.
clear all
close all
%// read the video:
reader = VideoReader('person01_boxing_d1_uncomp.avi');
vid = {};
while hasFrame(reader)
vid{end+1} = im2single(readFrame(reader));
end
%// simple background estimation using mean:
bg = mean( cat(4, vid{:}), 4);
%// estimate foreground as deviation from estimated background:
for i=1:40
fIdx(i) = i; %// do it for frame 1 ~ 60
fg{i} = sum( abs( vid{fIdx(i)} - bg ), 3 );
fg{i} = imresize(fg{i}, 0.2);
fg{i} = reshape(fg{i},[],1);
end
X = cell2mat(fg);
data = double(X);
2. And then I want to convert binary images to centered image(cropped) using image segmentation
Please let me know how to use these codes when I use several videos as input.
I attached sample video.
message = sprintf('Would you like to crop out each coin to individual images?');
reply = questdlg(message, 'Extract Individual Images?', 'Yes', 'No', 'Yes');
% Note: reply will = '' for Upper right X, 'Yes' for Yes, and 'No' for No.
if strcmpi(reply, 'Yes')
figure; % Create a new figure window.
% Maximize the figure window.
set(gcf, 'Units','Normalized','OuterPosition',[0 0 1 1]);
for k = 1 : numberOfBlobs % Loop through all blobs.
% Find the bounding box of each blob.
thisBlobsBoundingBox = blobMeasurements(k).BoundingBox; % Get list of pixels in current blob.
% Extract out this coin into it's own image.
subImage = imcrop(originalImage, thisBlobsBoundingBox);
% Determine if it's a dime (small) or a nickel (large coin).
if blobMeasurements(k).Area > 2200
coinType = 'nickel';
else
coinType = 'dime';
end
% Display the image with informative caption.
subplot(3, 4, k);
imshow(subImage);
caption = sprintf('Coin #%d is a %s.\nDiameter = %.1f pixels\nArea = %d pixels', ...
k, coinType, blobECD(k), blobMeasurements(k).Area);
title(caption, 'FontSize', textFontSize);
end
0 Comments
Accepted Answer
Image Analyst
on 18 Mar 2020
Put your code for one video file inside the loop over all video files. It might be best to put it all inside a function that takes the video filename.
9 Comments
Image Analyst
on 19 Mar 2020
That was your variable. Remember when you had
%// read the video:
list = dir('*.avi')
but now you decided not to use it anymore. It's also a built-in function in a toolbox that you don't have so it's warning you about that. I suggest you use another name like fileList instead.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!