Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
AVI files to images

Subject: AVI files to images

From: Susan

Date: 6 Mar, 2011 14:18:06

Message: 1 of 5

Hi all,

I wish compute the ifft of each frame of an avi file. In order to do this, I have converted frames of a .avi file to images so that I can perform ifft. I have been using the function 'frame2im' but so far it is only translating the first frame only. Please see below:

-----------------------------------------------------------------------------------------------------------------------
mov = aviread('Test.avi');

for i = 1:1:size(mov)
    
% GRAB FRAME
im = frame2im(mov(i));

hold all;
plot(ifft(double(im)));
end
-----------------------------------------------------------------------------------------------------------------------

It is not necessary for me to store all the frame as images, because I merely want the info concered with computing the ifft of each frame. Is there another method that will allow me to do this? The AVI file is 803 frames in size.

Kind regards,
Susan

Subject: AVI files to images

From: ImageAnalyst

Date: 6 Mar, 2011 14:43:30

Message: 2 of 5

Susan:
It's doing all of them, you just can't see them because you're just
watching the screen for the displayed image to change. But you're in
a tight intensive loop that's going so fast that the screen is not
being updated. If you set a breakpoint you'd see a change in im.

To see the display change in the loop, even without a breakpoint being
set, insert a drawnow command after the imshow() (which you'll call
along with fftshift() instead of plot).
ImageAnalyst

Subject: AVI files to images

From: Susan

Date: 6 Mar, 2011 15:18:05

Message: 3 of 5

ImageAnalyst,

Thanks for your reply. I keep getting an error staing that only one frame is being passed to frame2im. I tried to over come this by assigning martix indices of
im(i) = frame2im(mov(i))
and stepping a frame at a time, but received an error for 'Subscripted assignment dimension mismatch'.

Is there another way that I might be missing?

Kind regards,
Susan

Subject: AVI files to images

From: Image Analyst

Date: 6 Mar, 2011 15:53:05

Message: 4 of 5

Susan:
Just try this working example:

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.
% function test2()
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 = 14;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end

% Open the rhino.avi demo movie that ships with MATLAB.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
movieFullFileName = fullfile(folder, '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);
% Determine how many frames there are.
numberOfFrames = size(mov, 2);
% 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, taking the FFT of all frames.
    for frame = 1 : numberOfFrames
% Extract the frame from the movie structure.
thisFrame = mov(frame).cdata;

        % Display it
        hImage = subplot(1,2,1);
        image(thisFrame);
        axis square;
        caption = sprintf('Frame %4d of %d.', frame, numberOfFrames);
        title(caption, 'FontSize', fontSize);

% Take the FFT
grayImage = rgb2gray(thisFrame);
fftImage = real(fftshift(fft(double(grayImage))));
        % Display it
        hFFT = subplot(1,2,2);
        imshow(log(fftImage), []);
        axis square;
        caption = sprintf('FFT of Frame %4d of %d.', frame, numberOfFrames);
        title(caption, 'FontSize', fontSize);

drawnow; % Force it to refresh the window.
% Update user with the progress. Display in the command window.
progressIndication = sprintf('Processed frame %4d of %d.', frame, numberOfFrames);
disp(progressIndication);
% Increment frame count (should eventually = numberOfFrames
% unless an error happens).
    end

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

    msgbox('Done with this demo!');
    
catch ME
% Some error happened if you get here.
stError = lasterror;
strErrorMessage = sprintf('Error FFTing movie frames from:\n\n%s\n\nError: %s\n\n)', movieFullFileName, stError.message);
fprintf(1, '%s\n', strErrorMessage);
uiwait(msgbox(strErrorMessage));
end

Subject: AVI files to images

From: Susan

Date: 7 Mar, 2011 14:54:06

Message: 5 of 5

ImageAnalyst,

Thank you very much for the code help! It seems to do exactly what I wanted to, I am very grateful.

Kind regards,
Susan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us