From: nq on
ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <a04f9e72-d185-4764-924d-40a774774fe7(a)c10g2000yqi.googlegroups.com>...
> On Jun 23, 8:29 am, "raid " <raid2005sha...(a)yahoo.com> wrote:
> > please, help me to solve this object
>
> ----------------------------------------------------------
> See my demo:
> IMPORTANT: BE SURE TO JOIN ANY LINES THAT MAY GET SPLIT APART BY THE
> NEWSREADER.
>
> % Demo macro to extract frames and get frame means from an avi movie
> % and save individual frames to separate image files.
> 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.
> % (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.
>
> % 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;
> 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(numberOfFrames, 1);
> meanRedLevels = zeros(numberOfFrames, 1);
> meanGreenLevels = zeros(numberOfFrames, 1);
> meanBlueLevels = zeros(numberOfFrames, 1);
> 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);
>
> % Construct an output image file name.
> outputBaseFileName = sprintf('Frame %4.4d.png', frame);
> outputFullFileName = 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);
> drawnow; % Force it to refresh the window.
>
> % Write the image array to the output file, if requested.
> if writeToDisk
> % Extract the image with the text "burned into" it.
> frameWithText = 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 = rgb2gray(thisFrame);
> meanGrayLevels(frame) = mean(grayImage(:));
>
> % Calculate the mean R, G, and B levels.
> meanRedLevels(frame) = mean(mean(thisFrame(:, :, 1)));
> meanGreenLevels(frame) = mean(mean(thisFrame(:, :, 2)));
> meanBlueLevels(frame) = mean(mean(thisFrame(:, :, 3)));
>
> % Plot the mean gray levels.
> hPlot = 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 == 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] = size(thisFrame);
> end
>
> % Update user with the progress. Display in the command
> window.
> if writeToDisk
> progressIndication = sprintf('Wrote frame %4d of %d.', frame,
> numberOfFrames);
> else
> progressIndication = sprintf('Processed frame %4d of %d.',
> frame, numberOfFrames);
> end
> disp(progressIndication);
> % Increment frame count (should eventually = numberOfFrames
> % unless an error happens).
> numberOfFramesWritten = numberOfFramesWritten + 1;
> end
>
> % Alert user that we're done.
> if writeToDisk
> finishedMessage = sprintf('Done! It wrote %d frames to folder
> \n"%s"', numberOfFramesWritten, outputFolder);
> else
> finishedMessage = 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 = sprintf('Do you want to recall the individual
> frames\nback from disk into a movie?\n(This will take several
> seconds.)');
> button = 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 = 1 : numberOfFrames
> % Construct an output image file name.
> outputBaseFileName = sprintf('Frame %4.4d.png', frame);
> outputFullFileName = fullfile(outputFolder,
> outputBaseFileName);
> % Read the image in from disk.
> thisFrame = imread(outputFullFileName);
> % Convert the image into a "movie frame" structure.
> recalledMovie(frame) = 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;
> 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 = lasterror;
> strErrorMessage = sprintf('Error extracting movie frames from:\n\n%s\n
> \nError: %s\n\n)', movieFullFileName, stError.message);
> msgboxw(strErrorMessage);
> end
-----the above coding is very useful for me...thankss...
and my question over here is how to add in eye detection into this coding...
i have done the part extract a video clip into all frames...and now i dunno how to add in eye detection...thanks...
----the eyes blinking coding....
text='C:\Users\Huong\Desktop\frames\Frame 000';
for im=1:image_num
im;
filname=strcat(text,int2str(im));
filname=strcat(filname,'.jpg');
pic=imread(filname);
t=0.12;
pic_rgb=rgb2gray(pic);
ca=im2bw(pic_rgb,t);
c=~ca;

%morphology on BW image to detect location of eyes
se=strel('square',10);
eye_dil=imdilate(c,se);
% figure(1)
% imshow(eye_dil)

figure(2)
subplot(311),imshow(pic)
subplot(312),imshow(eye_dil)
sumc(1,im)=sum(sum(c)); %for subplot
subplot(313), plot(sumc)
% axis([0 833 min(min(sumc)) 23500]);
sumc1=sum(sum(c)); %for store and decision
%store data
fprintf(fid,'%f ',sumc1);
fprintf(fid,'\n');
% fprintf(fid1,'%f ',sumhor);
% fprintf(fid1,'\n');

%decision close or open

if sumc1<(5)
disp('eye is close')
else
disp('eye is open')
end
% pause(0.5);

end
From: ImageAnalyst on
In the loop over all frames, put in a call to some function, call it
EyeIsOpen(), which should return true or false (if you want). That
function would have all the code you just listed. Check out functions
in the help if you don't know how to call functions in your code.
From: nq on
ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <b40d1638-9643-405b-b2cb-bc59d4dd79c4(a)z28g2000yqh.googlegroups.com>...
> In the loop over all frames, put in a call to some function, call it
> EyeIsOpen(), which should return true or false (if you want). That
> function would have all the code you just listed. Check out functions
> in the help if you don't know how to call functions in your code.



thanks for your help...question over here is.....
clc;
close all;
imtool close all;
clear;
workspace;
fontSize = 14;

% Open the video.avi movie that ships with MATLAB.
movieFullFileName = 'C:\Users\Huong\Desktop\extract frames of avi format\VIDEO.avi';
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,'C:\Users\Huong\Desktop\New Folder');

% 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.

% 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;
else
writeToDisk = false;
end

% Extract out the various parts of the filename.
[folder, baseFileName, extentions, version] = 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 = pwd; % Make it a subfolder of the folder where this m-file lives.
outputFolder = sprintf('%s/Movie Frames from %s', folder, baseFileName);

% Loop through the movie, writing all frames out.
% Each frame will be in a separate file with unique name.
meanGrayLevels = zeros(numberOfFrames, 3);
meanRedLevels = zeros(numberOfFrames, 3);
meanGreenLevels = zeros(numberOfFrames, 3);
meanBlueLevels = zeros(numberOfFrames, 3);
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);
% Construct an output image file name.
outputBaseFileName = sprintf('Frame %4.4d.jpg', frame);
outputFullFileName = 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', 15);
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 = sprintf('Frame %4.4d.jpg', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
end

% 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', 15);

% Extract the image with the text "burned into" it.
frameWithText = 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, 'jpg');
end

% Update user with the progress. Display in the command window.
if writeToDisk
progressIndication = sprintf('Wrote frame %4d of %d.', frame, numberOfFrames);
else
progressIndication = sprintf('Processed frame %4d of %d.', frame, numberOfFrames);
end
disp(progressIndication);
% Increment frame count (should eventually = numberOfFrames
% unless an error happens).
numberOfFramesWritten = numberOfFramesWritten + 1;
% Alert user that we're done.
if writeToDisk
finishedMessage = sprintf('Done!It wrote %d frames to folder\n"%s"', numberOfFramesWritten, outputFolder);
else
finishedMessage = 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

----i modify the given coding so that it cn extract all frames from a video i recorded...but nw i dunno how to connect wit another coding so that i cn do eyes detection to all the frames.....coz the frame start with 'frame 0001,frame 0002....frame 0010, frame 0011....but it gives error msg..
-----Error using ==> imread at 315
---- File "C:\Users\Huong\Desktop\frames\Frame 00010.jpg" does not exist
it seem the numbering here got some problems...how to solve it.....???thankssss....

image_num=200;
text='C:\Users\Huong\Desktop\frames\Frame 000';
for im=1:image_num
im;
filname=strcat(text,int2str(im));
filname=strcat(filname,'.jpg');
pic=imread(filname);
t=0.12;
pic_rgb=rgb2gray(pic);
ca=im2bw(pic_rgb,t);
c=~ca;

%morphology on BW image to detect location of eyes
se=strel('square',10);
eye_dil=imdilate(c,se);
% figure(1)
% imshow(eye_dil)

figure(2)
subplot(311),imshow(pic)
subplot(312),imshow(eye_dil)
sumc(1,im)=sum(sum(c)); %for subplot
subplot(313), plot(sumc)
% axis([0 833 min(min(sumc)) 23500]);
sumc1=sum(sum(c)); %for store and decision
%store data
%fprintf(fid,'%f ',sumc1);
%fprintf(fid,'\n');
% fprintf(fid1,'%f ',sumhor);
% fprintf(fid1,'\n');

%decision close or open

if sumc1<(5)
disp('eye is close')
else
disp('eye is open')
end
% pause(0.5);

end













From: ImageAnalyst on
Too many zeros. The format was 4.4d, so you need to have
inputFolder = 'C:\Users\Huong\Desktop\frames';
for im=1:image_num
% Construct an inputimage file name.
inputBaseFileName = sprintf('Frame %4.4d.jpg', frame);
inputFullFileName = fullfile(inputFolder,
inputBaseFileName);
and so on....
From: ImageAnalyst on
Sorry clicked too soon. You changed the loop index variable name so
it should be
inputBaseFileName = sprintf('Frame %4.4d.jpg', im);