MATLAB: How to use image segmentation in video images of folders

@image analystdigital image processingimage processingimage segmentationvideo processing

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

Best Answer

See the FAQ.
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.