Matlab thinks an AVI it's written is corrupt - matlab

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.

Related

MATLAB - imwrite error in certain folders

this is my first question so I hope I am writing in the right place.
I am currently working with a Grasshopper3 camera, model: GS3-U3-50S5M-C from Point Grey Research connected to MATLAB.
I am recording a lot of frames at a time in grayscale and in order to avoid filling up my memory I would like to move some of the frames to my disk as stacked TIFFs during the recordings. The stacked TIFF format is needed for other programs used.
I am doing this by moving the recorded frames from the camera to the workspace using getdata. This creates a 4-D uint8 object containing height, width, color plane and frame number of the frames. In order to save the frames as a stacked TIFF I use a for loop with imwrite to save one frame at a time as shown below. However when I do this I most often get the error:
Error using imwrite (line 454) Unable to open file "C:\Users\My_User\Desktop\CraneStuff3\Name.tif" for writing. You might not have write permission.
When this happens MATLAB has typically saved some of the frames, e.g. if I have recorded 500 frames it might save 300 before giving me the error. Sometimes it manages to save every frame with no error, sometimes it only saves 30 frames before failing. I do not understand how it can save some frames and then claim that it cannot open the file for writing.
I have noticed, however, that if I instead save the file in "C:\Users\My_User\Documents\MATLAB" it seems to never give any error and save every frame as wanted.
Does anyone know why this is the case or how I can fix my problem?
I have included my code below here.
Note: I tried saving the frames as a .mat-file. To do this I had to use version '-v7.3' in save.
Thank you advance.
% Connect to camera:
vid = videoinput('pointgrey',1,'F7_Mono8_2448x2048_Mode0');
vid.TriggerRepeat = 100;
vid.FrameGrabInterval = 1;
% Make a recording:
start(vid)
pause(50)
stop(vid)
% Get data from camera to workspace:
data = getdata(vid,vid.FramesAvailable);
% Save data to disk one image at a time:
num = size(data);
output_name = fullfile('C:\Users\My_User\Desktop\CraneStuff3', ['Name' '.tif']);
for k = 1:num(4)
imwrite(data(:,:,:,k),output_name,'WriteMode','append');
end
One possible solution is to try saving each of the frames separately into a tiff file.
If you save a .mat file, better use savefast

Read avi video - Matlab

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?

How do I convert .rec file to .avi file using matlab

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.

Record video with web cam and get first frame of that and compare it with next frames in matlab

how can i record video by webcam in matlab and take frame of that and compare it with next frames
this is my code:
camvid1 = videoinput('winvideo',1,'YUY2_640x480');
subplot(211)
h1 = image;
axis ij
preview(camvid1,h1)
start(camvid1);
pause(2),camfrm1=getdata(camvid1);
camfrm1=rgb2gray(imread(camfrm1));
for i=1:n
camfrm2=getdata(camvid1);
camfrm2=rgb2gray(camfrm2);
bt=abs(double(camfrm2)-bouble(camfrm1));
imread is used to read a file from a folder which is already in a particular compressed or uncompressed format. In your case, you have raw data, on which you can perform operations. So, try using:
camfrm1 = rgb2gray(camfrm1);
This should work.
There is a second typo:
bt = abs(double(camfrm2)-double(camfrm1));

How to generate a video from matlab without codec errors?

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.