I want to read an AVI File in Matlab. I tried it according to this link: http://inside.mines.edu/~whoff/courses/EENG512/lectures/other/Matlab_movies.pdf :
clear all
close all
movieObj = VideoReader('ap001_BL0_SP2_cam03_compressed.avi'); % open file
get(movieObj) % display all information about movie
nFrames = movieObj.NumberOfFrames; %shows 310 in my case
for iFrame=1:2:nFrames
I = read(movieObj,iFrame); % get one RGB image
imshow(I,[]); % Display image
end
I get the following error:
Error using VideoReader/read (line 145) The frame index requested is
beyond the end of the file.
Error in test_video_read (line 9) I = read(movieObj,iFrame); % get
one RGB image
(Shortened) Output from "get(movieObj)" is:
General Settings:
Duration = 10.3333
Name = ap001_BL0_SP2_cam03_compressed.avi
Type = VideoReader
Video Settings:
BitsPerPixel = 24
FrameRate = 30
Height = 1280
NumberOfFrames = 310
VideoFormat = RGB24
Width = 960
So it should be possible to read the first frame, as there are 310 available!
I can play the AVI file in VLC-Player, so the codec should be already installed, right?
I'm using MATLAB R2013a, Windows 7. Can anyone please help, thank you!
VLC player is built using the ffmpeg codecs. VideoReader uses DirectShow and Media Foundation API's that are Windows Platform API's and are different from ffmpeg. So, if a file plays using VLC, it is not guaranteed to be opened by VideoReader. Couple of things you can do:
Can the file be viewed on Windows Media Player? If so, in most cases it should work with VideoReader. If not, then you do not have the suitable codecs. Try installing ffsdhow or K-lite codec pack.
If file works on Windows Media Player but VideoReader does not support, it would indicate a bug. A workaround that has worked for me in the past is that I install codecs mentioned above and give it a try again.
If (1) and (2) do not help, use software like handbrake or Mirro to transcode the file into MP4 which works win VideoReader.
Hope this helps.
Dinesh
I have tested your Matlab code using some avi files and I have no problem with them. So, I think it is your avi file that is causing the error.
I have a similar problem before, where my (mp4) movies can be played in any media players but matlab cannot open them. In my case, the problem is the pixel format when the mp4 movies were compiled (by ffmpeg). By default, my movies were compiled with the High 4:4:4 Predictive (yuv444p) format but Matlab cannot handle this. When I switched to an older pixel format (yuv420p), I don't have any problem loading the movies into Matlab.
You can check if this is the problem using ffprobe, which is part of ffmpeg and you can download them from https://www.ffmpeg.org.
Otherwise, have you tried with an uncompressed avi?
Related
I am trying to convert a high resolution image (30in width x 60in height) to a pdf file in MATLAB. I tried print, exportgraphics, and couple scripts online but I keep getting low quality output. I also tried setting the resolution to 300dpi but it didnt work. Please if you have any suggestions, share with me and I will test. Many thanks!
Image file used (renamed to map.png): https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/Political_map_of_the_World_%28January_2015%29.svg/9444px-Political_map_of_the_World_%28January_2015%29.svg.png
MATLAB commands used:
world=imread('map.png');
imshow(world)
exportgraphics(gcf,'world.pdf','ContentType','vector','Resolution',300)
#Texts in picture is blurry
print -dpdf 'world.pdf'
#Texts in picture is still blurry
exportfig(gcf, 'world.pdf', 'format','pdf','Resolution', 300,'Renderer', 'painters');
#this is a script from the MATLAB file exchange. Texts still blurry
I managed to do it by importing pdfbox (java) and importing the image as a bufferedimage then creating a document with pdmodel.PDDocument then adding a page with a custom size using the bufferedimage.getWidth and same for length then I streamed the bufferedimage to the page and saved the document to a pdf file. The code is on my work PC if anyone is interested I will copy it here.
I need to get an mpeg4 file to use in another application, from an original mpeg4 video I loaded into matlab and edited (frame by frame).
To do so, I tried using VideoWriter, setting the quality to 100%:
newVid = VideoWriter(outputfilename, 'MPEG-4');
newVid.FrameRate = fps;
newVid.Quality = 100;
However, the result I'm getting is very poor and if the original unedited video size was ~50MB, the one I get post-edit in matlab is around ~20MB, and I don't know how to keep the quality and size as they were.
I also tried saving as .avi and converting to mpeg4 with ffmpeg, but it gave even poorer results.
Any ideas?
MPEG-4is a compressed format so there is information loss when you save it in this format. Quality is the quality of the compression but you do not want any compression. To force Matlab not to use compression my guess is to use the statement below as the default is H.264
newVid.VideoCompressionMethod = 'none'
I'm using matlab to interface with a scientific camera using mex, and my matlab program uses VideoWriter() to write the file to disc. The camera is RGB-capable, and if I write the file as such, the video is fine. However, for the current application, I need grayscale images, and so I'm using rgb2gray() to convert it. Unfortunately, when the analysis code tried to read the video file again, I get the error:
Error using VideoReader/init (line 450)
Unable to read the file. The file appears to be corrupt.
and attempting to read the video with VLC confirms it to be corrupt. The only difference in my code between they grayscale and colour versions is the line:
frame = rgb2gray(frame);
My whole writing section of code is:
vid = VideoWriter('testVid.avi');
vid.FrameRate = framerate;
vid.Quality = 100;
open(vid);
for i = 1 : frames;
%read frame data into variable 'frame'
frame = rgb2gray(frame);
writeVideo(vid,frame);
end
I've spent far too long fighting with this, any ideas?
You need to close the video object, using close(vid) after writing the last frame.
I am new in matlab and i would like your help. I am working on a project where i use a Hispec camera to record moving objects(The main goal is to measure their velocity). The camera saves the video(actually the sequence of frames) as "autosave1.rec" . I have already created a code but it works with .avi file. Is there any way my code to read the "autosave1.rec" and convert it to "autosave.avi"?
Thanks in advance.
filename = 'C:\Users\kornikos\Documents\MATLAB\Autosave4.avi';
hVidReader = vision.VideoFileReader(filename, 'ImageColorSpace',
'RGB','VideoOutputDataType', 'single');
mov = VideoReader(filename);
VidFrames = read(mov);
nFrames = mov.NumberOfFrames;
Here are the first lines of my code. In order to run the code i created the .avi file manually. Hispec camera's menu gives you the opportunity to create an avi file from the rec file that you had recorded.
Now what i am looking for is an algorythm to do this "automatically". Is this feasible?
To read a video file you can use either VideoReader or vision.VideoFileReader. However, I don't know whether they will be able to read your .rec format. To write frames into an .avi video file you can use vision.VideoFileWriter.
Using my Windows 7 64bit machine, I'm trying to generate an avi file from Matlab using the sequence
aviobj = avifile('test.avi', 'fps', 25);
% a loop of aviobj = addframe(aviobj, frame)
close(aviobj)
However, the file generated is corrupt - with VLC it looks sheared and with bad colors, with Media Player it was a black screen, and with Divx Plus player it looks okay but generates a warning.
I've tried specifying other codec types (via avifile('test.avi', 'fps', 25, 'compression', 'TYPE') but Matlab never seems to be able to find that codec - I've tried Indeo and cvid and MSVC and MRLE and many more, but Matlab just generates a "not a supported compression method" warning, and then outright fails when addframe is invoked.
How can I solve the above problem, or alternatively, is there a different, simple way to just generate an avi by adding a frame at a time?
How to generate a video from matlab without codec errors?
try to use mmwrite
http://www.mathworks.com/matlabcentral/fileexchange/15881-mmwrite
Well after a few more searches online and experimentation, looks like others have encountered this problem as well, and suggested just using a different program to compress it and that should also fix the file, so that's what I did:
Generated an uncompressed file
Opened it in VirtualDub(good open-source video software which doesn't require installation)
From the video menu I chose "compression" and selected the "Microsoft Video 1" option
Saved the file via the save-as menu option
It now looks correct in all the players I've tried, and the file is smaller in size.
What you are describing what happens in VLC is probably related to having a resolution that is not supported by the codec. Try resolutions that are multiples of 2,4,8,16 and see what works. VirtualDub probably takes care of that somehow.
Otherwise it's really simple. All you have to do:
aviobj = avifile('example.avi', 'compression', 'none', 'fps', 25);
for i=1:1000
%generating the filenames
filename = strcat(FolderName,'/frame', sprintf('%05d', i),'.bmp');
I = imread(filename);
aviobj = addframe(aviobj,I);
end;
aviobj = close(aviobj);
I know I am replying to a very old thread, but I helped my brother with this and learned a few things that might help others:
My brother had a very small video, the result of an MRI scan, 51x51 pixels. Using VideoWriter with profile "Uncompressed AVI" worked, but did not play well in VLC. Virtualdub also threw errors.
What fixed it, is making sure that the video dimensions where a multiple of 2.
A 96x96 video played fine and could also be transcoded to XVID in virtualdub.
Also, with very small videos, set the video output mode to wingdi in VLC. With OpenGL or Direct3D and overlay mode, the GPU will do the scaling and interpolate pixels in between.