Thread Subject: Need urgent help in video image processing

Subject: Need urgent help in video image processing

From: Syed Ali

Date: 29 Oct, 2009 00:32:03

Message: 1 of 12

Hi, every one. I am new to MATLAB. I am trying to make
a short program for learning, but still having difficulties.
I have a movie in AVI format of 816 frames. I want to
read each frame, each pixel in the frame and then
take out the difference of frames after every 15 frames
and show the result. I got it by using following code:

test_vid = aviread('road.avi');
siz_vid = size(test_vid);
fram_cap = imshow(test_vid(1).cdata);
           
then i stuck,
could any one can help me out in coding.
Regards
Syed

Subject: Need urgent help in video image processing

From: ImageAnalyst

Date: 29 Oct, 2009 02:04:44

Message: 2 of 12

On Oct 28, 8:32 pm, "Syed Ali" <sfaisalal...@gmail.com> wrote:
> Hi, every one. I am new to MATLAB. I am trying to make
> a short program for learning, but still having difficulties.
> I have a movie in AVI format of 816 frames. I want to
> read each frame, each pixel in the frame and then
> take out the difference of frames after every 15 frames
> and show the result. I got it by using following code:
>
> test_vid = aviread('road.avi');
> siz_vid = size(test_vid);
> fram_cap = imshow(test_vid(1).cdata);
>
> then i stuck,
> could any one can help me out in coding.
> Regards
> Syed

-------------------------------------------------------------------------------------
Please define more explicitly what "then take out the difference of
frames after every 15 frames " means.

Subject: Need urgent help in video image processing

From: Syed Ali

Date: 29 Oct, 2009 02:50:21

Message: 3 of 12

Sir,
Let suppose I have a file name traffic.avi.
I need to find out that is there any motion in it or not, for this
I need to take all the frames from the movie "traffic.avi".
Then I will select only frames from 1, 15, 30, 45,..... end,
Then I will read each frame pixel by pixel and then store them
as frame 1, frame 15, frame 30, ... end.
Later on I will start doing subtraction
so frame_60 - frame_45 , frame_45 - frame_30, ...
then display the result as a movie or images, so i can find
out that either any of the object which is in frame 1 is moving
or not. The problem is when i am using aviread, the MATLAB
creates the structure having all my frames in it,
in cdata and map, then I dont know how to get excess
of each frame and how to read the values from each frame.
Hope you can understand.
Regards
Syed,

Subject: Need urgent help in video image processing

From: Nasser M. Abbasi

Date: 29 Oct, 2009 03:17:22

Message: 4 of 12


"Syed Ali" <sfaisalalipk@gmail.com> wrote in message
news:hcavtc$au4$1@fred.mathworks.com...
> Sir,
>
The problem is when i am using aviread, the MATLAB
> creates the structure having all my frames in it,
> in cdata and map, then I dont know how to get excess
> of each frame and how to read the values from each frame.

Well, then why don't you just read the specific frames from the file?

from help

"mov = aviread(filename, index) reads only the frames specified by index.
index can be a single index or an array of indices into the video stream. In
AVI files, the first frame has the index value 1, the second frame has the
index value 2, and so on."

--Nasser

Subject: Need urgent help in video image processing

From: Syed Ali

Date: 29 Oct, 2009 05:13:05

Message: 5 of 12

Dear Brother Naseer,
Thanks for your guidence, but do guide me,
for a frame
fram_test = aviread('abc.avi');
In work space it creates a structure <1 X 816> means it can have 816 frames.
Now further if we browse it will be
fram_test(1,1) will have cdata and colormap.
Now in cdata <240 X 320 X 3> 8 units.
The problem is how I can read pixel by pixel from each frame so
I can have single value of pixels (R, G, B) from each frame.
Thanks
Regards
Syed

Subject: Need urgent help in video image processing

From: Nasser M. Abbasi

Date: 29 Oct, 2009 05:34:17

Message: 6 of 12


"Syed Ali" <sfaisalalipk@gmail.com> wrote in message
news:hcb891$bhr$1@fred.mathworks.com...
>
> Now in cdata <240 X 320 X 3> 8 units.
> The problem is how I can read pixel by pixel from each frame so
> I can have single value of pixels (R, G, B) from each frame.

I do not know the avi stuff much, but the above seems like a common layout.

You have a frame which is made up of 3 channels (i.e. color frame). So the
first channel is the first page of the 3-D matrix, the second channel is the
second page, etc...

I do not know which is which, assume cdata(:,:,1) is R, cdata(:,:,2) is G
and cdata(:,:,3) is the blue.

So just read each pixel as you would normally read 3-D matrix?

the first pixel has its R pixel located at cdata(1,1,1), and has green pixel
at (1,1,2) etc...

each pixel value is 8 bits of intensity. so has value of 0..255


--Nasser

Subject: Need urgent help in video image processing

From: ImageAnalyst

Date: 29 Oct, 2009 12:19:00

Message: 7 of 12

Syed Ali:
See if this demo will help you.

%------------------------------------------------------------------------------------------------------------------
% Demo m-file to extract and write out all the frames of an avi movie
to disk.
% by ImageAnalyst
clc;
close all;
filename = 'C:\Program Files\MATLAB\R2008b\toolbox\images\imdemos
\rhinos.avi';
mov = aviread(filename);
% movie(mov);
outputFolder = fullfile(cd, 'Rhino Movie');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
numberOfFrames = size(mov, 2);
numberOfFramesWritten = 0;
for frame = 1 : numberOfFrames
thisFrame = mov(frame).cdata;
outputBaseFileName = sprintf('Frame %4.4d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite(thisFrame, outputFullFileName, 'png');
progressIndication = sprintf('Wrote frame %4d of %d.', frame,
numberOfFrames);
disp(progressIndication);
numberOfFramesWritten = numberOfFramesWritten + 1;
end
progressIndication = sprintf('Wrote %d frames to folder "%s"',
numberOfFramesWritten, outputFolder);
disp(progressIndication);


I'm not sure if you really wanted 1, 15, 30, 45, etc. instead of 1,
16, 31, 46, etc. but I'll just assume you meant what you said. You'll
need to change the for loop to something like this because of your
strange incrementing (14 for the first one and 15 after that).
for frame = 0 : 15 : numberOfFrames
  if frame == 0
thisFrame = mov(1).cdata;
  else
thisFrame = mov(frame).cdata;
  end

Now, when you subtract, you'll have to cast your frames to int16,
int32, single, or double BEFORE the subtraction, because your
differences could go from -255 to +255 and uint8 will clip any
negative values to 0 so you'll lose them. You don't have to write out
anything (original frames or subtracted ones) if you don't want to so
you can remove those lines if you want.
Good luck,
ImageAnalyst

Subject: Need urgent help in video image processing

From: Syed Ali

Date: 29 Oct, 2009 13:38:03

Message: 8 of 12

Dear Sir Image Analyst,
Thanks for your great help,
I will see how I can subtract frames and find out the motion.
I have tried this code to extract all the frames and save it
in jpg format.

close all;
clc;

%
fram_test = aviread('Rhinos.avi'); % Read avi test file
for j=1:length(fram_test) % Loop for creating frames with Name Missile and
      filename=['Rhinos-', num2str(j) '.jpg']; % save it as jpg
      imwrite(fram_test(j).cdata,filename,'jpeg');
end

But your code is more managed :)
could you please help me in how can I work with cdata and map
so it will be fast. Cause in my code it will take longer time. As when
the size of file is increased to 816 frames.

Regards
Syed

Subject: Need urgent help in video image processing

From: ImageAnalyst

Date: 30 Oct, 2009 16:31:20

Message: 9 of 12

On Oct 29, 9:38 am, "Syed Ali" <sfaisalal...@gmail.com> wrote:
> Dear Sir Image Analyst,
> Thanks for your great help,
> I will see how I can subtract frames and find out the motion.
> I have tried this code to extract all the frames and save it
> in jpg format.
>
> close all;
> clc;
>
> %
> fram_test = aviread('Rhinos.avi'); % Read avi test file
> for j=1:length(fram_test)    % Loop for creating frames with Name Missile and
>       filename=['Rhinos-', num2str(j) '.jpg']; % save it as jpg
>       imwrite(fram_test(j).cdata,filename,'jpeg');
> end
>
> But your code is more managed :)
> could you please help me in how can I  work with cdata and map
> so it will be fast. Cause in my code it will take longer time. As when
> the size of file is increased to 816 frames.
>
> Regards
> Syed
----------------------------------------------------------------------------------------------------------------------
Syed:
The Image Processing Toolbox has a demo on "Detecting Cars in a Video
of Traffic."
Have you gone through that tutorial?

Subject: Need urgent help in video image processing

From: Syed Ali

Date: 4 Nov, 2009 17:04:07

Message: 10 of 12

Yes thanks, I have seen the Rhino and Traffic both tutorials.
 I did the frame subtraction and got the results. Now I need
to store the result as Image, can it be possible.
2nd, Reading long movies like I do have 900 frames will take
long time so is it possible that I can store the frames as MAT
file,... Your hint is required.
Thanks

Subject: Need urgent help in video image processing

From: ImageAnalyst

Date: 4 Nov, 2009 17:36:43

Message: 11 of 12

On Nov 4, 12:04 pm, "Syed Ali" <sfaisalal...@gmail.com> wrote:
> Yes thanks, I have seen the Rhino and Traffic both tutorials.
>  I did the frame subtraction and got the results. Now I need
> to store the result as Image, can it be possible.
> 2nd, Reading long movies like I do have 900 frames will take
> long time so is it possible that I can store the frames as MAT
> file,... Your hint is required.
> Thanks

------------------------------------------------------------------------------
Yes you can save images as images.

2nd, Yes you can save any variable in MATLAB to a mat file. Just use
the save() function.
But I'm not sure why reading 900 frames would take that long, and I
don't know if it would be any faster if you converted it to a mat
file. I mean 900 frames is around 30 seconds so since you can play it
in a media player program it shouldn't take any longer than 30
seconds. What kind of hyper speed do you require? By the way, 900
frames is NOT a long movie.

Subject: Need urgent help in video image processing

From: Syed Ali

Date: 5 Nov, 2009 03:50:03

Message: 12 of 12

Well actually I need to do some operations on Video Frames
and later on I will try to do real time processing so for the learning aspect
the 900 frames will take more time, some of my friends told me that
I can try to store all the segmented frames as jpg and later on
save it as MAT file, as MAT files will take less time.
But again it is a problem that I can read selected frames like
with equal difference of 15 frames I can read the R, G, and B components
of each frame but how can I take them in a movie again.
Like from 900 frames with capturing every 15th frame we can have
60 frames so we start taking difference of these frames and store it
again as a movie to find out how they are working, so there we have
4 movies as outputs
1. Real Avi movie and its difference
2. Only R Component difference movie
3.Only G Component difference movie
4. Only B Component difference movie.

And how can these be done with MAT file. I try to save it as Mat file
but it gives me error.

Regards
Syed

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
storing data as... Syed Ali 4 Nov, 2009 12:09:05
mat file Syed Ali 4 Nov, 2009 12:09:05
frame subtracti... Syed Ali 29 Oct, 2009 01:14:06
map Syed Ali 28 Oct, 2009 22:54:09
cdata Syed Ali 28 Oct, 2009 22:54:09
motion detection Syed Ali 28 Oct, 2009 22:54:09
rssFeed for this Thread

Contact us at files@mathworks.com