From: Rakesh bagul on
Hi all,
I am new to MATLAB programming. I want to calculate mean gray scale of all frames in a .AVI video file and plot them w.r.t frame number. Please guide me.

Rakesh
From: ImageAnalyst on
On May 12, 3:39 pm, "Rakesh bagul" <bagulr2...(a)gmail.com> wrote:
> Hi all,
> I am new to MATLAB programming. I want to calculate mean gray scale of all frames in a .AVI video file and plot them w.r.t frame number. Please guide me.
>
> Rakesh

---------------------------------------------------------------------------------------------------------------
Rakesh
It's just an easy adaption of a demo I've posted many times before.
Run this and see if it does what you want.
You can comment out the imwrite() if you don't want to write the
frames to disk.
It does do the plotting of the mean gray levels like you want.
IMPORTANT: BE SURE TO JOIN ANY LINES SPLIT INTO TWO BY THE
NEWSREADER!!!
-ImageAnalyst

% Demo macro to extract frames and get frame means from an avi movie
% and save individual frames to separate image files.
% by ImageAnalyst
clc;
close all;

% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Open the rhino.avi demo movie that ships with MATLAB.
movieFullFileName = 'C:\Program Files\MATLAB\R2010a\toolbox\images
\imdemos\rhinos.avi';
% Check to see that it exists.
if ~exist(movieFullFileName, 'file')
strErrorMessage = sprintf('File not found:\n%s\nYou can choose a new
one, or cancel', movieFullFileName);
response = questdlg(strErrorMessage, 'File not found', 'OK - choose a
new movie.', 'Cancel', 'OK - choose a new movie.');
if strcmpi(response, 'OK - choose a new movie.')
[baseFileName, folderName, FilterIndex] = uigetfile('*.avi');
if ~isequal(baseFileName, 0)
movieFullFileName = fullfile(folderName, baseFileName);
else
return;
end
else
return;
end
end
try
mov = aviread(movieFullFileName);
% movie(mov);
% Make a special output folder to hold all the separate movie frames
% as their own image files.
outputFolder = fullfile(cd, 'Rhino Movie');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
% Determine how many frames there are.
numberOfFrames = size(mov, 2);
numberOfFramesWritten = 0;
% Prepare a figure to show the images in the upper half of the
screen.
figure;
screenSize = get(0, 'ScreenSize');
newWindowPosition = [1 screenSize(4)/2 - 70 screenSize(3)
screenSize(4)/2];
set(gcf, 'Position', newWindowPosition); % Maximize figure.
% set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

% Loop through the movie, writing all frames out.
% Each frame will be in a separate file with unique name.
meanGrayLevels = zeros(numberOfFrames, 1);
for frame = 1 : numberOfFrames
% Extract the frame from the movie structure.
thisFrame = mov(frame).cdata;

% Display it
subplot(1,2,1);
image(thisFrame);
drawnow; % Force it to refresh the window.
axis square;

% Calculate the mean gray level.
grayImage = rgb2gray(thisFrame);
meanGrayLevels(frame) = mean(grayImage(:));
% Plot the mean gray levels.
subplot(1,2,2);
plot(meanGrayLevels);
if frame == 1
title('Mean Gray Levels');
xlabel('Frame Number');
yLabel('Gray Level');
end

% Construct an output image file name.
outputBaseFileName = sprintf('Frame %4.4d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
% Write the image array to the output file.
imwrite(thisFrame, outputFullFileName, 'png');
% Update user with the progress. Display in the command window.
progressIndication = sprintf('Wrote frame %4d of %d.', frame,
numberOfFrames);
disp(progressIndication);
% Increment frame count (should eventually = numberOfFrames
% unless an error happens).
numberOfFramesWritten = numberOfFramesWritten + 1;
end
catch ME
% Some error happened if you get here.
stError = lasterror;
strErrorMessage = sprintf('Error extracting movie frames from:\n\n%s\n
\nError: %s\n\n)', movieFullFileName, stError.message);
msgboxw(strErrorMessage);
end

% Alert user that we're done.
finishedMessage = sprintf('Done! It wrote %d frames to folder\n"%s"',
numberOfFramesWritten, outputFolder);
disp(finishedMessage); % Write to command window.
msgbox(finishedMessage); % Also pop up a message box.

From: Rakesh bagul on
Hi Image Analyst,
Thank you so much for guiding me. But as I tried to calculate mean gray scale value of 5-7 ultrasound .avi files, but it is same i.e. 13.6053 for all of them. Can you explain me why it is same for all videos.

Thanks

ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <c5df03ad-cb75-421a-aece-fd7ea054cfd4(a)h39g2000yqn.googlegroups.com>...
> On May 12, 3:39 pm, "Rakesh bagul" <bagulr2...(a)gmail.com> wrote:
> > Hi all,
> > I am new to MATLAB programming. I want to calculate mean gray scale of all frames in a .AVI video file and plot them w.r.t frame number. Please guide me.
> >
> > Rakesh
>
> ---------------------------------------------------------------------------------------------------------------
> Rakesh
> It's just an easy adaption of a demo I've posted many times before.
> Run this and see if it does what you want.
> You can comment out the imwrite() if you don't want to write the
> frames to disk.
> It does do the plotting of the mean gray levels like you want.
> IMPORTANT: BE SURE TO JOIN ANY LINES SPLIT INTO TWO BY THE
> NEWSREADER!!!
> -ImageAnalyst
>
> % Demo macro to extract frames and get frame means from an avi movie
> % and save individual frames to separate image files.
> % by ImageAnalyst
> clc;
> close all;
>
> % Change the current folder to the folder of this m-file.
> % (The line of code below is from Brett Shoelson of The Mathworks.)
> if(~isdeployed)
> cd(fileparts(which(mfilename)));
> end
>
> % Open the rhino.avi demo movie that ships with MATLAB.
> movieFullFileName = 'C:\Program Files\MATLAB\R2010a\toolbox\images
> \imdemos\rhinos.avi';
> % Check to see that it exists.
> if ~exist(movieFullFileName, 'file')
> strErrorMessage = sprintf('File not found:\n%s\nYou can choose a new
> one, or cancel', movieFullFileName);
> response = questdlg(strErrorMessage, 'File not found', 'OK - choose a
> new movie.', 'Cancel', 'OK - choose a new movie.');
> if strcmpi(response, 'OK - choose a new movie.')
> [baseFileName, folderName, FilterIndex] = uigetfile('*.avi');
> if ~isequal(baseFileName, 0)
> movieFullFileName = fullfile(folderName, baseFileName);
> else
> return;
> end
> else
> return;
> end
> end
> try
> mov = aviread(movieFullFileName);
> % movie(mov);
> % Make a special output folder to hold all the separate movie frames
> % as their own image files.
> outputFolder = fullfile(cd, 'Rhino Movie');
> if ~exist(outputFolder, 'dir')
> mkdir(outputFolder);
> end
> % Determine how many frames there are.
> numberOfFrames = size(mov, 2);
> numberOfFramesWritten = 0;
> % Prepare a figure to show the images in the upper half of the
> screen.
> figure;
> screenSize = get(0, 'ScreenSize');
> newWindowPosition = [1 screenSize(4)/2 - 70 screenSize(3)
> screenSize(4)/2];
> set(gcf, 'Position', newWindowPosition); % Maximize figure.
> % set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
>
> % Loop through the movie, writing all frames out.
> % Each frame will be in a separate file with unique name.
> meanGrayLevels = zeros(numberOfFrames, 1);
> for frame = 1 : numberOfFrames
> % Extract the frame from the movie structure.
> thisFrame = mov(frame).cdata;
>
> % Display it
> subplot(1,2,1);
> image(thisFrame);
> drawnow; % Force it to refresh the window.
> axis square;
>
> % Calculate the mean gray level.
> grayImage = rgb2gray(thisFrame);
> meanGrayLevels(frame) = mean(grayImage(:));
> % Plot the mean gray levels.
> subplot(1,2,2);
> plot(meanGrayLevels);
> if frame == 1
> title('Mean Gray Levels');
> xlabel('Frame Number');
> yLabel('Gray Level');
> end
>
> % Construct an output image file name.
> outputBaseFileName = sprintf('Frame %4.4d.png', frame);
> outputFullFileName = fullfile(outputFolder, outputBaseFileName);
> % Write the image array to the output file.
> imwrite(thisFrame, outputFullFileName, 'png');
> % Update user with the progress. Display in the command window.
> progressIndication = sprintf('Wrote frame %4d of %d.', frame,
> numberOfFrames);
> disp(progressIndication);
> % Increment frame count (should eventually = numberOfFrames
> % unless an error happens).
> numberOfFramesWritten = numberOfFramesWritten + 1;
> end
> catch ME
> % Some error happened if you get here.
> stError = lasterror;
> strErrorMessage = sprintf('Error extracting movie frames from:\n\n%s\n
> \nError: %s\n\n)', movieFullFileName, stError.message);
> msgboxw(strErrorMessage);
> end
>
> % Alert user that we're done.
> finishedMessage = sprintf('Done! It wrote %d frames to folder\n"%s"',
> numberOfFramesWritten, outputFolder);
> disp(finishedMessage); % Write to command window.
> msgbox(finishedMessage); % Also pop up a message box.
From: ImageAnalyst on
I don't know. Maybe it's a class issue, like you're not working in
floating point or something. Upload the videos to http://drop.io, or
your own website, if you want someone to download them and try it.