how can i read all the frames/images from a .rec file?

11 views (last 30 days)
Hello. I am trying to read all the images/frames from a video. I built up the following code:
fid = fopen('C:\HiSpecAutoSave\Autosave4.rec');
fseek(fid, 1310720, -1);%move file pointer to first frame of data
img=fread(fid, [1280, 256], 'uint8=>uint8');
fclose(fid);
img2 = fliplr(img);%we need to flip it left/right for some wierd reason
img3 = imrotate(img2, 90);%and rotate, unsure why it's stored this way
My problem is that this code reads only the first image/frame. What should i do to read all the images? Thanks in advance

Accepted Answer

Geoff Hayes
Geoff Hayes on 13 Aug 2014
Nikolaos - if we know the number of frames in the video and we assume that all frames follow each other (in the rec file) then we could do something like the following
% open the file for reading
fid = fopen('C:\HiSpecAutoSave\Autosave4.rec');
% if the file descriptor is valid
if fid>0
% assume 100 frames
numFrames = 100;
% pre-allocate memory for each frame
allFrames = uint8(zeros(256,1280,numFrames));
% move file pointer to first frame of data
fseek(fid, 1310720, -1);
% read each frame
for k=1:numFrames
% read the kth frame
img=fread(fid, [1280, 256], 'uint8=>uint8');
% flip it
img = fliplr(img);
% rotate it and save to array
allFrames(:,:,k) = imrotate(img, 90);
end
fclose(fid);
end
The above will read each frame into the allFrames array. Try it and see what happens!
  4 Comments
Nikolaos
Nikolaos on 24 Aug 2014
Edited: Nikolaos on 24 Aug 2014
Thank you very very much. Your advice helped me a lot. It made me look at the right direction. Below i send you the final code.
% open the file for reading
fid = fopen('C:\HiSpecAutoSave\Autosave4.rec');
% if the file descriptor is valid
if fid>0
% move file pointer to first frame of data
fseek(fid, 1310720, -1);
k = 0;
while ~feof(fid)
[img,ctr] = fread(fid,[1280,256],'uint8=>uint8');
if ctr < (1280*256)
break; %incomplete frame so exit
else
k = k + 1;
% flip it
img = fliplr(img);
% % rotate it and save to array
allFrames(:,:,k) = imrotate(img, 90);
end
end
end

Sign in to comment.

More Answers (1)

Srimathy K SOC IT
Srimathy K SOC IT on 26 Mar 2019
sir how to embed secret data in video????
i had used interframe motion prediction
function [motion,pred_err]=inter_cons(im_old,im_new,i,j,N)
for i=1:100
rows=i+N-1;cols=j+N-1;
SAD = 1.0e+10;
for u = -N:N
for v = -N:N
sad = im_new(rows+1:rows+N,cols+1:cols+N)-im_old(rows+u+1:rows+u+N,cols+v+1:cols+v+N); %difference
sad = sum(abs(sad(:)));
if sad < SAD
SAD=sad;
x= v; y = u;
end
end
end
motion=[x y];
pred_im(1:N,1:N)=im_old(rows+y+1:rows+y+N,cols+x+1:cols+x+N);
pred_err(1:N,1:N) = im_new(rows:rows+N-1,cols:cols+N-1)-pred_im(1:N,1:N);
end
end
but the code only giving result for first frame in 8*8 matrix
i need your help sir
  1 Comment
Geoff Hayes
Geoff Hayes on 26 Mar 2019
Srimathy - please post this a new question rather than as an answer to this one.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!