Habib H. Bravo Ruiz
Newbie level 1
- Joined
- Jun 28, 2013
- Messages
- 1
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Location
- Morgantown
- Activity points
- 33
Hi,
I am trying to extract and analyze frames from a video using Matlab. When I run the following script I get this message:
Error using zeros
Out of memory. Type HELP MEMORY for your options.
Anyone knows how to solve this issue? The videos that I am trying to process range in size from 90 to 500 MB for now.
Any help will be greatly appreciated.
%% Locate video file
folder = fullfile('Matlab Data', 'Video'); %Directory (e.g. D:/Matlab Data/Video)
movieFullFileName = fullfile(folder, 'Test #9.avi'); %Name of video file in D:/Matlab Data/Video
%% Determine how many frames there are
mov = VideoReader(movieFullFileName);
nFrames = read(mov, [1 inf]);
nFramesWritten = 0;
%% Ask user if they want to write the individual frames out to disk
promptMessage = sprintf('Do you want to save the individual frames out to individual disk files?');
button = questdlg(promptMessage, 'Save individual frames?', 'Yes', 'No', 'Yes');
if strcmp(button, 'Yes')
writeToDisk = true;
[~, baseFileName, extentions] = fileparts(movieFullFileName); % Extract out the various parts of the filename.
folder = pwd;
outputFolder = sprintf('%s/Movie Frames from %s', ...
folder, baseFileName); % Make it a subfolder of the folder where this m-file lives
if ~exist(outputFolder, 'dir') % Create the folder if it doesn't exist already.
mkdir(outputFolder);
end
else
writeToDisk = false;
end
%% Loop through the movie, writing all frames out. Each frame will be in a separate file with unique name.
meanGrayLevels = zeros(nFrames, 1);
meanRedLevels = zeros(nFrames, 1);
meanGreenLevels = zeros(nFrames, 1);
meanBlueLevels = zeros(nFrames, 1);
for frame = 1 : nFrames % Extract the frame from the movie structure.
thisFrame = mov(frame).cdata;
end
%% Display it
hImage = subplot(1,2,1);
image(thisFrame);
axis square;
caption = sprintf('Frame %4d of %d.', frame, nFrames);
title(caption, 'FontSize', 12);
drawnow; % Force it to refresh the window.
%% Write the image array to the output file, if requested.
if writeToDisk
outputBaseFileName = sprintf('Frame %4.4d.png', frame); % Construct an output image file name.
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
text(5, 15, outputBaseFileName, 'FontSize', 20); % Stamp the name and frame number onto the image.
frameWithText = getframe(gca); % Extract the image with the text "burned into" it.
imwrite(frameWithText.cdata, outputFullFileName, 'png'); % frameWithText.cdata is the image with the text
end % actually written into the pixel values.
%% Calculate color levels.
grayImage = rgb2gray(thisFrame); % Image conversion
meanGrayLevels(frame) = mean(grayImage));
meanRedLevels(frame) = mean(mean(thisFrame, :, 1))); % Calculate the mean R, G, and B levels.
meanGreenLevels(frame) = mean(mean(thisFrame, :, 2)));
meanBlueLevels(frame) = mean(mean(thisFrame, :, 3)));
hPlot = subplot(1,2,2); % Plot the mean gray levels.
hold off;
plot(meanGrayLevels, 'k-', 'LineWidth', 2);
hold on;
plot(meanRedLevels, 'r-');
plot(meanGreenLevels, 'g-');
plot(meanBlueLevels, 'b-');
title('Mean Gray Levels', 'FontSize', 12); % Put title back because plot() erases the existing title.
if frame == 1
xlabel('Frame Number');
yLabel('Gray Level');
[rows, columns, numberOfColorChannels] = size(thisFrame); % Get size data later for preallocation if we read the movie back in from disk.
end
I am trying to extract and analyze frames from a video using Matlab. When I run the following script I get this message:
Error using zeros
Out of memory. Type HELP MEMORY for your options.
Anyone knows how to solve this issue? The videos that I am trying to process range in size from 90 to 500 MB for now.
Any help will be greatly appreciated.
%% Locate video file
folder = fullfile('Matlab Data', 'Video'); %Directory (e.g. D:/Matlab Data/Video)
movieFullFileName = fullfile(folder, 'Test #9.avi'); %Name of video file in D:/Matlab Data/Video
%% Determine how many frames there are
mov = VideoReader(movieFullFileName);
nFrames = read(mov, [1 inf]);
nFramesWritten = 0;
%% Ask user if they want to write the individual frames out to disk
promptMessage = sprintf('Do you want to save the individual frames out to individual disk files?');
button = questdlg(promptMessage, 'Save individual frames?', 'Yes', 'No', 'Yes');
if strcmp(button, 'Yes')
writeToDisk = true;
[~, baseFileName, extentions] = fileparts(movieFullFileName); % Extract out the various parts of the filename.
folder = pwd;
outputFolder = sprintf('%s/Movie Frames from %s', ...
folder, baseFileName); % Make it a subfolder of the folder where this m-file lives
if ~exist(outputFolder, 'dir') % Create the folder if it doesn't exist already.
mkdir(outputFolder);
end
else
writeToDisk = false;
end
%% Loop through the movie, writing all frames out. Each frame will be in a separate file with unique name.
meanGrayLevels = zeros(nFrames, 1);
meanRedLevels = zeros(nFrames, 1);
meanGreenLevels = zeros(nFrames, 1);
meanBlueLevels = zeros(nFrames, 1);
for frame = 1 : nFrames % Extract the frame from the movie structure.
thisFrame = mov(frame).cdata;
end
%% Display it
hImage = subplot(1,2,1);
image(thisFrame);
axis square;
caption = sprintf('Frame %4d of %d.', frame, nFrames);
title(caption, 'FontSize', 12);
drawnow; % Force it to refresh the window.
%% Write the image array to the output file, if requested.
if writeToDisk
outputBaseFileName = sprintf('Frame %4.4d.png', frame); % Construct an output image file name.
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
text(5, 15, outputBaseFileName, 'FontSize', 20); % Stamp the name and frame number onto the image.
frameWithText = getframe(gca); % Extract the image with the text "burned into" it.
imwrite(frameWithText.cdata, outputFullFileName, 'png'); % frameWithText.cdata is the image with the text
end % actually written into the pixel values.
%% Calculate color levels.
grayImage = rgb2gray(thisFrame); % Image conversion
meanGrayLevels(frame) = mean(grayImage));
meanRedLevels(frame) = mean(mean(thisFrame, :, 1))); % Calculate the mean R, G, and B levels.
meanGreenLevels(frame) = mean(mean(thisFrame, :, 2)));
meanBlueLevels(frame) = mean(mean(thisFrame, :, 3)));
hPlot = subplot(1,2,2); % Plot the mean gray levels.
hold off;
plot(meanGrayLevels, 'k-', 'LineWidth', 2);
hold on;
plot(meanRedLevels, 'r-');
plot(meanGreenLevels, 'g-');
plot(meanBlueLevels, 'b-');
title('Mean Gray Levels', 'FontSize', 12); % Put title back because plot() erases the existing title.
if frame == 1
xlabel('Frame Number');
yLabel('Gray Level');
[rows, columns, numberOfColorChannels] = size(thisFrame); % Get size data later for preallocation if we read the movie back in from disk.
end