Converting movie2avi to VideoWriter

17 次查看(过去 30 天)
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);

回答(1 个)

Dominique
Dominique 2022-2-12
编辑:Walter Roberson 2022-2-12
% 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 个评论
Dominique
Dominique 2022-2-14
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.
Walter Roberson
Walter Roberson 2022-2-14
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.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by