How do I convert .rec file to .avi file using matlab - 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.

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

Being able to read images from any directory

I'm new to Matlab and I am having some problems with reading images and dealing with directories and things like that. I had an assignment where I was to write a script that converts an image that is not grayscale into grayscale (for example, if the image is truecolor, convert to grayscale).
This was my code:
img = uigetfile('*');
imgx = imfinfo(img);
imgx.ColorType
if imgx.ColorType == 'truecolor'
img = imread(img);
img = rgb2gray(img);
end
However, I ended up getting points off for the following:
"only works if image is in the same folder as script"
I realize that my script only works for images that are in folders that are on the MATLAB path, so I don't know if that's a separate issue from what he said or if that's what he meant. I assume he wants to be able to select any image on your computer to be able to read and perform the operation, but I don't know how to approach this. Can anyone help me?
The problem is that img = uigetfile('*') returns only the file name as string. To work with pictures in folders other than matlab folder you would need to extract the full path. You can do this using the following approach:
[fileName, folderName] = uigetfile('*');
img=fullfile(folderName, fileName);
imgx = imfinfo(img);
The rest of your code should work after this small change

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?

Matlab thinks an AVI it's written is corrupt

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.

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));