Converting movie2avi to VideoWriter

Hello experts,
I’m new to the community and a novice to MATLAB. I have a script that works perfectly in MATLAB 2016a but not in 2020a. The very end of the script has a movie2avi for creating video but was discontinued in 2016a. I’m having trouble re-writing it in 2020a with VideoWriter to produce video.
The background of the script is taking NITF files and converting them to images, then producing video. I will post the script here. I’m hoping that just the last part can be corrected to produce the video since everything works up to that point in 2020a
Sorry if I’m not giving enough detail as I’m new.
____________________________________________________________________________________________________________________________________
function process(fpsVal,angle)
%location of images
path = 'C:\Users\RES\Desktop\Process_EFI\input';
%list of images
dirName = input('Enter directory name: ','s');
fpath = fullfile(path,dirName);
scenelist = dir(fpath);
scenelist_r = regexprep({scenelist.name},'(\d+).*$','$1');
scenelist = unique(scenelist_r);
for i=1:length(scenelist)
disp(scenelist(i))
end
sceneNum = input('Enter scene number in format *01240_*: ','s');
imglist = dir(fullfile(fpath,sceneNum));
% sort the array
names = {imglist.name};
names_r = regexprep(names,'\d+_(\d+).*$','$1');
frameNums = str2double(names_r);
[~,I] = sort(frameNums);
imglist = names(I);
%frames per seconds for movie
fps = str2double(fpsVal);
angle = str2double(angle);
%creation of output directory if it doesn't exist
if exist('C:\Users\RES\Desktop\Process_EFI\output','dir')==0;
mkdir(fullfile(fpath,'output'));
end
%movie name/location
movieName = input('Enter movie name: ','s');
fname = fullfile('C:\Users\RES\Desktop\Process_EFI\output',strcat(movieName,'.avi'));
%set compression method, however, don't have right codecs
%codec = 'hfyu';
%for each image, read it and convert it to a movie frame and put it in an
%array
if strfind(char(imglist(1)),'IR')
disp('Processing IR!!!')
for k = 1:length(imglist);
img = nitfread(fullfile(fpath,char(imglist(k))));
t1(k) = std2(img);
%map the image to mean - 5*stdev and convert to uint8
%if the printed mean is high, modify the multiplier of std2
%until mean is around 120
img = uint8(img - (mean(img(:)) - 1.25*std2(img)));
mean(img(:))
metadata = nitfinfo(fullfile(fpath,char(imglist(k))));
img = histeq(img);
%set the filter
%h = fspecial('average', [3,3]);
h = fspecial('gaussian');
% filter image
img_f = imfilter(img,h,'conv');
if angle ~= 0
img_f = imrotate(img_f,-90,'nearest','crop');
end
m(k) = im2frame(img_f,gray(256));
if rem(k,100) == 0
disp([num2str(k) ' frames processed...'])
end
end
else
for k = 1:length(imglist);
img = nitfread(fullfile(fpath,char(imglist(k))));
metadata = nitfinfo(fullfile(fpath,char(imglist(k))));
img = histeq(img);
%set the filter
h = fspecial('average', [3,3]);
% filter image
img_f = imfilter(img,h,'conv');
if angle ~= 0
rotate_val = angle*-1;
img_f = imrotate(img_f,rotate_val,'nearest','crop');
end
% gg = gray(256);
% g2 = zeros(256,3);
% lowlim = 1;
% highlim = 0.2;
% step = (highlim-lowlim)/255;
% lowlim = 1;
% highlim = 255;
% step = (highlim-lowlim);
%
%
% g2(lowlim:highlim,1) = 0:1/step:1;
% g2(lowlim:highlim,2) = g2(lowlim:highlim,1);
% g2(lowlim:highlim,3) = g2(lowlim:highlim,1);
% g2(highlim:256,1) = ones(256-highlim+1,1);
% g2(highlim:256,2) = g2(highlim:256,1);
% g2(highlim:256,3) = g2(highlim:256,1);
% m(k) = im2frame(uint8(img_f(1:256,128:768)),g2);
% m(k) = im2frame(uint8(img_f(1:256,128:768)),gray(256));
% m(k) = im2frame(uint8(imcrop(img_f,[128,1,640,256])),gray(256));
m(k) = im2frame(uint8(imcrop(img_f,[1,1,999,999])),gray(256));
% m(k) = im2frame(uint8(img_f),gray(256));
if rem(k,100) == 0
disp([num2str(k) 'frames processed...'])
end
end
end
length(m);
movie2avi(m,fname,'compression','none','fps',fps);

Answers (1)

Dominique
Dominique on 12 Feb 2022
Edited: Walter Roberson on 12 Feb 2022
% 2022-02-11 : copié-collé par Dominique.Beaulieu@gmail.com
%
% Source : aide Matlab.
% But : pour compenser la disparition de movie2avi.
%
% Programmé en mode "quick and dirty".
%
% Entrées :
% StrMovie : structure Matlab contenant le "movie"
% Exemple :
% StrMov = MonMovie
% MonMovie ==> 1×124 struct array with fields:
% Champs :
% cdata: [428×531×3 uint8]
% colormap: []
%
% sAvi : chaîne de caractères (s pour string) contenant le nom du fichier
% Exemple :
% sAvi = 'MonAnimation.avi'
%
% Fichier : Mov2Avi.m
function Mov2Avi(StrMov, sAvi)
N = size(StrMov,2);
% Prepare the new file.
vidObj = VideoWriter(sAvi);
open(vidObj);
% Create an animation.
axis tight manual;
set(gca,'nextplot','replacechildren');
for k = 1:N
h=figure;
imshow(StrMov(k).cdata);
currFrame = getframe(h);
writeVideo(vidObj,currFrame);
close(h);
end
% Close the file.
close(vidObj);

7 Comments

Displaying the movie and capturing frames is unnecessary and will degrade the images.
function Mov2Avi(StrMov, sAvi)
N = size(StrMov,2);
% Prepare the new file.
vidObj = VideoWriter(sAvi);
open(vidObj);
for k = 1:N
currFrame = StrMov(k).cdata;
cmap = StrMov(k).colormap;
if ~isempty(cmap); currFrame = ind2rgb(currFrame, cmap); end
writeVideo(vidObj,currFrame);
end
% Close the file.
close(vidObj);
Thank you very much Walter Roberson :)
Hey, huys, use the Walter's version, it is much better than mine :)
Walter, will you upload it? If yes, I will remove my contribution.
Nah, too much trouble to upload it -- I would have to spend too much time adding error checking and error handling. What happens if the filename is not valid? What happens if StrMov is not a struct? Or does not have the right fields? What happens if it is an empty struct? What happens if it only has a single entry?
(There is an oddity that (at least historically) writeVideo needed two frames to create an AVI, that a single-frame AVI would not get saved.)
What happens? Just write in the header : "StrMov is a movie created by Matlab commands gca and getframe with at least 2 frames and there is no warrantee" :)
May I upload it? I will give you credit :)
But that's not the only way to create a "movie" structure !
One thing to consider is whether it is necessary (or advisable) to support a mix of RGB and pseudocolor frames. If only pure RGB or pure pseudocolor need to be handled, then the code could be split into two branches, by testing in advance whether the first frame had a non-empty colormap, and if the colormap was empty then entry a code loop that does not bother to look at the colormap. Also, if the struct does not have a colordata field then that branch would be used too.
Maybe, but 99% of beginners will use gca and getframe, a copy-paste from help.
When people come here, ... well ... I will talk about myself ... I like simple scripts or functions that works immediately, and preferably, if possible, with a demo containing or generating input data and calling the function. Plug-and-play, quick-and-dirty. Then, I will manage the modifications and improvements. But first, simple and immediately usable.
Well, write it all up in the form you prefer, complete with demo and so on, and post it. But if it doesn't have error checking added, then credit Alan Smithee not me.

Sign in to comment.

Tags

Asked:

on 25 Mar 2021

Commented:

on 14 Feb 2022

Community Treasure Hunt

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

Start Hunting!