Do you know how to convert .avi file to .bin (binary) file?
I want to use it in a method "Tracking Cars Using Optical Flow".
Thaank you :) :)
one of the solutions
- a very brief example
fnam='your.avi';
ao=mmreader(fnam);
frm=read(ao);
imagesc(frm(:,:,:,1));
axis image;
% now, you can save each FRM as a binary into another file...
us
Thank you very much!! Now I can separate the .avi to its image frames..
But how can I make a final .bin file (equal to the initial .avi file) ??
Well, since as far as I know .bin is not a standard format, I define it to
be exactly the same as the AVI format to make the solution easy.
movefile('myfile.avi', 'myfile.bin')
If you have a specific format in mind when you say ".bin (binary) file",
then you will need to obtain and/or write the functions to write data to and
read data from files in that specific format.
--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
This demo :
uses 'viptraffic.bin' as the video input, but it's not clear how it was converted from the original 'viptraffic.avi' which is included as well in
C:\Program Files\MATLAB\R2010a\toolbox\vipblks\vipdemos
....In this way, I cannot use my own file in this demo...which is unbelievable!! :0
well...
NOW you're (finally) being more specific...
it would have been helpful had you mentioned this tbx from the very beginning...
in the doc, look at the help for
video.BinaryFileReader
video.BinaryFileWriter
us
But it should be done somehow...
I'm sorry but I cannot figure it out...
Thank you so much for your help.
That demo should include some mention of how to format the file that it
reads in (or at least a link to that documentation.) Please contact
Technical Support and ask them for more information on how to convert your
file to that format. When you do so, ask them to enter an enhancement
request to include that information in the demo (either directly or via
hyperlink.)
steve
it's all there, eg, the writer...
http://www.mathworks.com/access/helpdesk/help/toolbox/vipblks/ref/video.binaryfilewriterclass.html
the OP simply needs to carefully look at the:
- videoformat
- fourcharactercode
tags and the accompanying web-address...
us
You managed to use your own file at the demo???
Please tell us :) :)
By the way, I contacted the technical support...!!
If you replace the BinaryFileReader with MultimediaFileReader:
you will be able to read in other video types.
The MultimediaFileReader can handle many formats (including compressed AVIs) on Windows but only
uncompressed AVIs on Unix. The binary file viptraffic.bin is supported across all platforms,
and that's why it was used for this demo.
Witek
I used it but in the final steps of the code there was an error, which forces Matlab to restart...
I do not know why...
Thank you so much for your help...
I do not know how to use my own .avi video yet....
If you can post the error, maybe we can help you. If you are not seeing an error and instead MATLAB exits,
consider running it with -logfile switch:
matlab -logfile my_favorite_file_name.txt
and then looking into the file for errors.
I hope that you were able to incorporate your AVI file into the demo. In terms of mechanics of reading
the file in, a good start is the example that comes with the MultimediaFileReader object:
% EXAMPLE: Read a video and play it.
hmfr = video.MultimediaFileReader('viplanedeparture.avi');
hp = video.VideoPlayer;
while ~isDone(hmfr)
videoFrame = step(hmfr);
step(hp, videoFrame);
end
release(hp);
release(hmfr);
HTH,
Witek
I believe the problem is that I cannot even read and play my own video..I tried your example ( Read a video and play it), with my own .avi file in the workspace that I have chosen, eg :
C:\Program Files\MATLAB\R2010a\toolbox\vipblks\vipdemos
(where there are all the example videos!!)
but I get the following error :
MATLAB crash file:C:\Users\Chris\AppData\Local\Temp\matlab_crash_dump.4004:
------------------------------------------------------------------------
Segmentation violation detected at Thu May 06 08:09:16 2010
------------------------------------------------------------------------
Configuration:
MATLAB Version: 7.10.0.499 (R2010a)
MATLAB License: 161051
Operating System: Microsoft Windows 7
Window System: Version 6.1 (Build 7600)
Processor ID: x86 Family 6 Model 15 Stepping 2, GenuineIntel
Virtual Machine: Java 1.6.0_12-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
Default Encoding: windows-1252
Fault Count: 1
Register State:
EAX = 00000000 EBX = 0e20fac8
ECX = 00c2c600 EDX = 26f50001
ESI = 20532ccc EDI = 0e20fb24
EBP = 0e20fb30 ESP = 00c2c5e0
EIP = 30e82176 FLG = 00010216
Stack Trace:
[0] ffdshow.ax:0x30e82176(1, 83, 0, 0)
If this problem is reproducible, please submit a Service Request via:
http://www.mathworks.com/support/contact_us/ts/help_request_1.html
A technical support engineer might contact you with further information.
Thank you for your help. MATLAB may attempt to recover, but even if recovery appears successful,
we recommend that you save your workspace and restart MATLAB as soon as possible.
Then I choose "Attempt to continue" and I get the above error along with :
Caught MathWorks::System::FatalException
[Please exit and restart MATLAB]>>
Thank you sooo much :) :)
You have been really helpful :) :)
Having that stack trace below helps a lot. Notice the line about ffdshow.
The MultimediaFileReader object relies on OS libraries and codecs installed on your system to read
the multimedia files. It looks like the failure is happening in the ffdshow, which is a 3rd
party piece of software. I'm guessing that you installed a larger codec pack on your system and
either there is an issue with ffdshow or an interaction between the system object and the ffdshow.
Try uninstalling ffdshow thus allowing your AVI to be handled by another codec on your system.
BTW. Can you play your file with Windows Media Player?
HTH,
Witek
But now, at the end of the whole algorithm described here :
When I enter the code :
% Calculate and draw the motion vectors.
tmp = of(RV,CV) .* MotionVecGain;
lines = [X(:)';Y(:)';X(:)' + imag(tmp(:))';Y(:)' + real(tmp(:))'];
mv_video = step(hshapeins2, image, lines);
step(hVideo1, image); % Display Original Video
step(hVideo2, mv_video); % Display video with motion vectors
step(hVideo3, th_image); % Display Thresholded Video
step(hVideo4, image_out); % Display video with bounding boxes
I get the error message :
??? Error using ==> step
Reported by video.MultimediaFileReader: Too many output arguments; must be between 1 and 2
(3 requested).
I do not know what this means...
Thank you so much...You reallyyy helped me so much.
while ~isDone(hbfr)
..................
end
I get the error :
??? Error using ==> step
Reported by video.MultimediaFileReader: Too many output arguments; must be between 1 and 2
(3 requested).
Even if I enter only the very first part of the loop :
while ~isDone(hbfr)
[y, cb, cr] = step(hbfr); % Read input video frame
[cb, cr] = step(hcr, cb, cr);
imrgb = step(hcsc1, cat(3,y,cb,cr)); % Convert image from YCbCr to RGB
image = step(hidtc, imrgb); % Convert image to single
I = step(hcsc2, image); % Convert color image to intensity
of = step(hof, I); % Estimate optical flow
end
or
while ~isDone(hbfr)
[y, cb, cr] = step(hbfr); % Read input video frame
end
I also get the same error...
Thank you so much.
The .bin file reader was set up to output YCbCr color signal that was Chroma sub-sampled.
That signal was upsampled and converted to RGB. The YCbCr signal was output into three different
arrays: Y, Cb and Cr (where Cb and Cr are smaller in size than Y).
When you use the MultimediaFileReader, it gives you RGB output that's packed in a 3-D array, i.e.
single variable output. Therefore, you need to do the following:
1. use a single variable to store you RBB output
2. remove chroma resampling
3. remove color space conversion to RGB since you already have RGB from the MultimediaFileReader
4. apply conversion to intensity on your new RGB signal (you will no longer need the cat(3,y,cb,cr),
just feed your 3-D RGB signal directly to it).
The error was basically saying: you asked for 3 outputs but I can give you only 1 or 2 (there is a
way to get EOF indicator with this object).
See the documentation for ChromaResampler to learn more about subsampled YCbCr color format.
Your loop:
while ~isDone(hbfr)
[y, cb, cr] = step(hbfr); % Read input video frame
end
can be quickly made to work like this:
while ~isDone(hbfr)
my_rgb_frame = step(hbfr); % Read input video frame
end
Hope this helps,
Witek
I managed to add my own video using the following code :
while ~isDone(hbfr)
my_rgb_frame = step(hbfr); % Read input video frame
image = step(hidtc, my_rgb_frame);
I = step(hcsc2, image);
of = step(hof, I); % Estimate optical flow
Thank you sooooo much!! I wish you the best.
Chris
I am also struggling to use my own video. My code so far is as follows:
while ~isDone(hbfr)
% [y, cb, cr] = step(hbfr); % Read input video frame
% [cb, cr] = step(hcr, cb, cr); % Upsample chroma to construct YCbCr 4:4:4
% image = step(hcsc, cat(3,y,cb,cr)); % Convert image from YCbCr to RGB
% % for display purposes
[y] = step(hbfr); % Read input video frame
image = step(hbfr); % Convert image from YCbCr to RGB
% for display purposes
Did I complete the following correctly?
1. use a single variable to store you RBB output
2. remove chroma resampling
3. remove color space conversion to RGB since you already have RGB from the MultimediaFileReader
4. apply conversion to intensity on your new RGB signal (you will no longer need the cat(3,y,cb,cr),
just feed your 3-D RGB signal directly to it).
Thanks a million, Zach
> I am also struggling to use my own video. My code so far is as follows:
> while ~isDone(hbfr)
> % [y, cb, cr] = step(hbfr); % Read input video frame
> % [cb, cr] = step(hcr, cb, cr); % Upsample chroma to construct YCbCr 4:4:4
> % image = step(hcsc, cat(3,y,cb,cr)); % Convert image from YCbCr to RGB
> % % for display purposes
>
> [y] = step(hbfr); % Read input video frame
> image = step(hbfr); % Convert image from YCbCr to RGB
> % for display purposes
It's hard for me to tell what's happening without having more context. What kind of file are you
reading in? Can you show more of your code? Can you reproduce it with one of the example video files
shipping with MATLAB?
> [y] = step(hbfr); % Read input video frame
> image = step(hbfr); % Convert image from YCbCr to RGB
Note also that you are calling step on the same (hbfr) object. Is this what you intended?
Witek
I am facing the same problem you faced 1 year back. I can't load my own .avi file. Would you please send me your whole customized code?
I will be ever grateful to you. Thanks in advance.
Ashfaq
I am using Matlab R2011a version. It came supplied with a demo called videotrafficgmm.m where it detects cars in a video sequence using foreground detection based on gaussian mixture models. My problem is similar to the issue you have here! I am trying to adapt the code and use my own .avi video. From reading the replies in this thread, I see that it is best not to try to convert the avi into binary format but that it is easier to adapt the code where the YCbCr components are found.
Could someone please help me with this?
I understand that I need to do this correctly first
1. use a single variable to store the RGB output
2. remove chroma resampling
3. remove color space conversion to RGB since you already have RGB from the MultimediaFileReader
4. apply conversion to intensity on your new RGB signal (you will no longer need the cat(3,y,cb,cr), just feed your 3-D RGB signal directly to it).
I am unsure as to how to adapt it to suit my own avi.
Please help!!!! Here is the code from the demo supplied in the matlab package.
%%
% Create a System object to read video from a binary file.
hbfr = vision.BinaryFileReader('Filename', 'viptraffic.bin');
%%
% Create a System object to upsample the chrominance components of the
% video.
hcr = vision.ChromaResampler(...
'Resampling', '4:2:0 (MPEG1) to 4:4:4', ...
'InterpolationFilter', 'Pixel replication');
%%
% Create color space converter System objects to convert the image from
% YCbCr to RGB format.
hcsc = vision.ColorSpaceConverter('Conversion', 'YCbCr to RGB');
%%
% Create a System object to detect foreground using gaussian mixture models.
hof = vision.ForegroundDetector(...
'NumTrainingFrames', 5, ... % only 5 because of short video
'InitialVariance', (30/255)^2); % initial standard deviation of 30/255
%%
% Create a blob analysis System object to segment cars in the video.
hblob = vision.BlobAnalysis( ...
'CentroidOutputPort', false, ...
'AreaOutputPort', true, ...
'BoundingBoxOutputPort', true, ...
'OutputDataType', 'single', ...
'NumBlobsOutputPort', false, ...
'MinimumBlobAreaSource', 'Property', ...
'MinimumBlobArea', 250, ...
'MaximumBlobAreaSource', 'Property', ...
'MaximumBlobArea', 3600, ...
'FillValues', -1, ...
'MaximumCount', 80);
%%
% Create and configure two System objects that insert shapes, one for
% drawing the bounding box around the cars and the other for drawing the
% motion vector lines.
hshapeins1 = vision.ShapeInserter( ...
'BorderColor', 'Custom', ...
'CustomBorderColor', [0 255 0]);
hshapeins2 = vision.ShapeInserter( ...
'Shape','Lines', ...
'BorderColor', 'Custom', ...
'CustomBorderColor', [255 255 0]);
%%
% Create and configure a System object to write the number of cars being
% tracked.
htextins = vision.TextInserter( ...
'Text', '%4d', ...
'Location', [0 0], ...
'Color', [255 255 255], ...
'FontSize', 12);
%%
% Create System objects to display the results.
sz = get(0,'ScreenSize');
pos = [20 sz(4)-300 200 200];
hVideoOrig = vision.VideoPlayer('Name', 'Original', 'Position', pos);
pos(1) = pos(1)+220; % move the next viewer to the right
hVideoFg = vision.VideoPlayer('Name', 'Foreground', 'Position', pos);
pos(1) = pos(1)+220;
hVideoRes = vision.VideoPlayer('Name', 'Results', 'Position', pos);
line_row = 22; % Define region of interest (ROI)
%% Stream Processing Loop
% Create a processing loop to track the cars in the input video. This
% loop uses the previously instantiated System objects.
%
% When the BinaryFileReader object detects the end of the input file, the loop
% stops.
while ~isDone(hbfr)
[y, cb, cr] = step(hbfr); % Read input video frame
[cb, cr] = step(hcr, cb, cr); % Upsample chroma to construct YCbCr 4:4:4
image = step(hcsc, cat(3,y,cb,cr)); % Convert image from YCbCr to RGB
% for display purposes
% Remove the effect of sudden intensity changes due to camera's
% auto white balancing algorithm.
y = im2single(y);
y = y-mean(y(:));
fg_image = step(hof, y); % Foreground
% Estimate the area and bounding box of the blobs in the foreground
% image.
[area, bbox] = step(hblob, fg_image);
Idx = bbox(1,:) > line_row; % Select boxes which are in the ROI.
% Based on dimensions, exclude objects which are not cars. When the
% ratio between the area of the blob and the area of the bounding box
% is above 0.4 (40%) classify it as a car.
ratio = zeros(1, length(Idx));
ratio(Idx) = single(area(1,Idx))./single(bbox(3,Idx).*bbox(4,Idx));
ratiob = ratio > 0.4;
count = int32(sum(ratiob)); % Number of cars
bbox(:, ~ratiob) = int32(-1);
% Draw bounding rectangles around the detected cars.
y2 = step(hshapeins1, image, bbox);
% Display the number of cars tracked and a white line showing the ROI.
y2(22:23,:,:) = 255; % White line
y2(1:15,1:30,:) = 0; % Black background for displaying count
image_out = step(htextins, y2, count);
step(hVideoOrig, image); % Original video
step(hVideoFg, fg_image); % Foreground
step(hVideoRes, image_out); % Bounding boxes around cars
end
% Close the video file
release(hbfr);
Hopefully, this will clarify things:
New version of MATLAB has just been released. It ships with the demo that is now using AVI files
instead of BIN files. BTW. You don't need to upgrade MATLAB if you don't want to. You can find
the new demo and the code for it here:
Please note that if you run this new version of the demo in older version of MATLAB, it will not
work right away. There has been a change of coordinate system used by the toolbox to make it
consistent and easier to use together with the Image Processing Toolbox. More info can be found here:
http://www.mathworks.com/help/toolbox/vision/rn/bs03omb.html#bs03on2
Ideally, you'd use the new version, but if you can't, you'll have to tweak the code. You'll get
errors that will point you to places that need adjustment.
Please let me know if you experience any issues.
Witek