how to get frames from .avi format.

  • Follow


i am trying to get frames from a video with the format is .avi. I have tried using the demo rhino, but ntg happen.Please do advice. thanks
0
Reply Jitvinder 10/29/2010 8:10:06 AM

On Oct 29, 4:10=A0am, "Jitvinder " <jit_1...@yahoo.com> wrote:
> i am trying to get frames from a video with the format is .avi. I have tr=
ied using the demo rhino, but ntg happen.Please do advice. thanks

---------------------------------------------------------------------------=
--------
Then try my rhino demo.  I know for a fact that it definitely works.
BE SURE to join any lines split into two by the newsreader!

% Demo macro to extract frames and get frame means from an avi movie
% and save individual frames to separate image files.
% Also computes the mean gray value of the color channels.
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
imtool close all;  % Close all imtool figures.
clear;  % Erase all existing variables.
workspace;  % Make sure the workspace panel is showing.
fontSize =3D 14;

% 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 =3D 'C:\Program Files\MATLAB\R2010a\toolbox\images
\imdemos\rhinos.avi';
% Check to see that it exists.
if ~exist(movieFullFileName, 'file')
	strErrorMessage =3D sprintf('File not found:\n%s\nYou can choose a new
one, or cancel', movieFullFileName);
	response =3D 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] =3D uigetfile('*.avi');
		if ~isequal(baseFileName, 0)
			movieFullFileName =3D fullfile(folderName, baseFileName);
		else
			return;
		end
	else
		return;
	end
end

try
	mov =3D aviread(movieFullFileName);
	% movie(mov);
	% Determine how many frames there are.
	numberOfFrames =3D size(mov, 2);
	numberOfFramesWritten =3D 0;
	% Prepare a figure to show the images in the upper half of the
screen.
	figure;
	screenSize =3D get(0, 'ScreenSize');
	newWindowPosition =3D [1 screenSize(4)/2 - 70 screenSize(3)
screenSize(4)/2];
	set(gcf, 'Position', newWindowPosition); % Maximize figure.
% 	set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

    % Ask user if they want to write the individual frames out to
disk.
    promptMessage =3D sprintf('Do you want to save the individual frames
out to individual disk files?');
    button =3D questdlg(promptMessage, 'Save individual frames?', 'Yes',
'No', 'Yes');
    if strcmp(button, 'Yes')
        writeToDisk =3D true;

        % Extract out the various parts of the filename.
        [folder, baseFileName, extentions, version] =3D
fileparts(movieFullFileName);
        % Make up a special new output subfolder for all the separate
        % movie frames that we're going to extract and save to disk.
        % (Don't worry - windows can handle forward slashes in the
folder name.)
        folder =3D pwd;   % Make it a subfolder of the folder where this
m-file lives.
        outputFolder =3D sprintf('%s/Movie Frames from %s', folder,
baseFileName);
        % Create the folder if it doesn't exist already.
        if ~exist(outputFolder, 'dir')
            mkdir(outputFolder);
        end
    else
        writeToDisk =3D false;
    end

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

        % Display it
        hImage =3D subplot(1,2,1);
        image(thisFrame);
        axis square;
        caption =3D sprintf('Frame %4d of %d.', frame, numberOfFrames);
        title(caption, 'FontSize', fontSize);
        drawnow; % Force it to refresh the window.

		% Write the image array to the output file, if requested.
        if writeToDisk
             % Construct an output image file name.
            outputBaseFileName =3D sprintf('Frame %4.4d.png', frame);
            outputFullFileName =3D fullfile(outputFolder,
outputBaseFileName);

            % Stamp the name and frame number onto the image.
            % At this point it's just going into the overlay,
            % not actually getting written into the pixel values.
            text(5, 15, outputBaseFileName, 'FontSize', 20);

            % Extract the image with the text "burned into" it.
            frameWithText =3D getframe(gca);
            % frameWithText.cdata is the image with the text
            % actually written into the pixel values.
            % Write it out to disk.
            imwrite(frameWithText.cdata, outputFullFileName, 'png');
        end

		% Calculate the mean gray level.
		grayImage =3D rgb2gray(thisFrame);
		meanGrayLevels(frame) =3D mean(grayImage(:));

		% Calculate the mean R, G, and B levels.
		meanRedLevels(frame) =3D mean(mean(thisFrame(:, :, 1)));
		meanGreenLevels(frame) =3D mean(mean(thisFrame(:, :, 2)));
		meanBlueLevels(frame) =3D mean(mean(thisFrame(:, :, 3)));

		% Plot the mean gray levels.
		hPlot =3D subplot(1,2,2);
        hold off;
		plot(meanGrayLevels, 'k-', 'LineWidth', 2);
        hold on;
        plot(meanRedLevels, 'r-');
        plot(meanGreenLevels, 'g-');
        plot(meanBlueLevels, 'b-');

        % Put title back because plot() erases the existing title.
		title('Mean Gray Levels', 'FontSize', fontSize);
        if frame =3D=3D 1
			xlabel('Frame Number');
			yLabel('Gray Level');
            % Get size data later for preallocation if we read
            % the movie back in from disk.
            [rows columns numberOfColorChannels] =3D size(thisFrame);
        end

        % Update user with the progress.  Display in the command
window.
        if writeToDisk
    		progressIndication =3D sprintf('Wrote frame %4d of %d.', frame,
numberOfFrames);
        else
    		progressIndication =3D sprintf('Processed frame %4d of %d.',
frame, numberOfFrames);
        end
		disp(progressIndication);
		% Increment frame count (should eventually =3D numberOfFrames
		% unless an error happens).
		numberOfFramesWritten =3D numberOfFramesWritten + 1;
    end

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

    % Exit if they didn't write any individual frames out to disk.
    if ~writeToDisk
        return;
    end

    % Ask user if they want to read the individual frames from the
disk,
    % that they just wrote out, back into a movie and display it.
    promptMessage =3D sprintf('Do you want to recall the individual
frames\nback from disk into a movie?\n(This will take several
seconds.)');
    button =3D questdlg(promptMessage, 'Recall Movie?', 'Yes', 'No',
'Yes');
    if strcmp(button, 'No')
        return;
    end

    % Read the frame back in, and convert them to a movie.
    % I don't know of any way to preallocate recalledMovie.
    for frame =3D 1 : numberOfFrames
        % Construct an output image file name.
        outputBaseFileName =3D sprintf('Frame %4.4d.png', frame);
        outputFullFileName =3D fullfile(outputFolder,
outputBaseFileName);
        % Read the image in from disk.
        thisFrame =3D imread(outputFullFileName);
        % Convert the image into a "movie frame" structure.
        recalledMovie(frame) =3D im2frame(thisFrame);
    end
    % Get rid of old image and plot.
    delete(hImage);
    delete(hPlot);
    % Create new axes for our movie.
    subPlot(1, 3, 2);
    axis off;  % Turn off axes numbers.
    title('Movie recalled from disk', 'FontSize', fontSize);
    % Play the movie in the axes.
    movie(recalledMovie);
    % Note: if you want to display graphics or text in the overlay
    % as the movie plays back then you need to do it like I did at
first
    % (at the top of this file where you extract and imshow a frame at
a time.)
    msgbox('Done with this demo!');

catch ME
	% Some error happened if you get here.
	stError =3D lasterror;
	strErrorMessage =3D sprintf('Error extracting movie frames from:\n\n%s\n
\nError: %s\n\n)', movieFullFileName, stError.message);
	msgboxw(strErrorMessage);
end

0
Reply ImageAnalyst 10/29/2010 10:41:29 AM


1 Replies
299 Views

(page loaded in 0.055 seconds)

Similiar Articles:













7/24/2012 6:12:25 PM


Reply: