[英]frame to video conversion matlab
So I just started with image processing/computer vision in MATLAB. 所以我刚开始在MATLAB中进行图像处理/计算机视觉。
So my first task is to convert a series of images(frames) into a video. 所以我的第一个任务是将一系列图像(帧)转换为视频。 So I went through online sources (MATLAB website more specifically) to get a way to do it. 因此,我浏览了在线资源(更具体地说是MATLAB网站)来找到一种方法。
So the one that I implemented is http://www.mathworks.com/help/matlab/examples/convert-between-image-sequences-and-video.html which solved the problem for me. 因此,我实施的是http://www.mathworks.com/help/matlab/examples/convert-between-image-sequences-and-video.html ,它为我解决了这个问题。
However, when I play it, the video seems jumpy in some places. 但是,当我播放它时,该视频在某些地方似乎有些跳动。 Like it would bring a different frame in the middle and make the whole video jumpy for that split second. 就像它将在中间带来一个不同的帧,并使整个视频在那一瞬间变得跳动起来。 It happens a couple of places in the video. 它在视频中发生了两个地方。
Any anyone knows why this happens? 任何人都知道为什么会这样吗?
Thanks 谢谢
PS below is the code I use: PS下面是我使用的代码:
myFolder = 'C:\Users\owner\Desktop\MATLAB GUI\Color\Color'; %Specify Directory
filePattern = fullfile(myFolder, '*.jpg') %identify jpg files
jpegFiles = dir(filePattern) %use dir to list jpg files
size = length(jpegFiles); % length of the size of the file
outputVideo = VideoWriter(fullfile(myFolder,'video1.avi'));
outputVideo.FrameRate = 30;
open(outputVideo);
for i = 1:length(jpegFiles) %load all the files in the directory
j = i; %%accumulating the number of jpegfiles into handles.j
baseFileName = jpegFiles(i).name;
fullFileName = fullfile(myFolder, baseFileName);
%fprintf(1, 'Now reading %s\n', fullFileName); %filename of image
imageArray = imread(fullFileName); %image being read
%imageArray = rgb2gray(imageArray);
imagecell{i} = imageArray; %storing the images in imagecells
writeVideo(outputVideo,imagecell{i});
end
close(outputVideo);
video1 = VideoReader(fullfile(myFolder,'video1.avi'));
mov(video1.NumberOfFrames) = struct('cdata',[],'colormap',[]);
for ii = 1:video1.NumberOfFrames
mov(ii) = im2frame(read(video1,ii));
end
set(gcf,'position', [150 150 video1.Width video1.Height])
set(gca,'units','pixels');
set(gca,'position',[0 0 video1.Width video1.Height])
image(mov(1).cdata,'Parent',gca);
axis off;
movie(mov,1,video1.FrameRate);
Given that there may be too many files to be renamed (padded with zeros) here is a quick function that will do it for you: you just need to provide the directory/folder where the images are stored, the padding (if less 100 files, then padding can be 2; if less than 1000 files, then padding can be 3; etc.), and a common pattern. 鉴于可能有太多文件需要重命名(用零填充),这里有个快速功能可以帮您完成:您只需要提供存储图像的目录/文件夹,填充(如果少于100个文件) ,则padding可以为2;如果文件数少于1000,则padding可以为3;以此类推),以及通用模式。 The code assumes that there is a common pattern in each file (like 'frame' or 'image') that when removed, leaves just the number: 该代码假定每个文件中都有一个公共模式(例如“框架”或“图像”),该模式在删除后仅保留数字:
renameFiles(directory,padSize,fileNamePattern)
filePattern = fullfile(directory, '*.jpg') %identify jpg files
jpegFiles = dir(filePattern) %use dir to list jpg files
for k=1:size(jpegFiles)
% get the source file that will be moved/renamed
fileSrc = jpegFiles(k).name;
% get the parts of the file
[path,name,ext] = fileparts(fileSrc);
% where does the pattern fit in the name?
idx = strfind(name,fileNamePattern);
% remove the pattern from the name
if idx==0
% pattern does not exist in file so skip it
continue;
elseif idx==1
% pattern is at the beginning of name so remove it
frameNumberStr = name(length(fileNamePattern)+1:end);
else
% pattern is at the end of name so remove it
frameNumberStr = name(1:idx-1);
end
% get the number of digits
numDigits = length(frameNumberStr);
% create the new file name
paddedName = [fileNamePattern repmat('0',1,padSize-numDigits) frameNumberStr];
fprintf('%s\n',paddedName);
% only move if the destination is different
if strcmp(paddedName,name) ~= 1
% set the destination file
fileDest = fullfile(directory,[paddedName ext]);
% move the file
movefile(fileSrc, fileDest,'f');
end
end
end
An example - if all files have the common pattern of 'frame' and there are less than 1000 files, then run this function as 例如-如果所有文件都具有“ frame”的通用模式,并且文件数少于1000,则以以下方式运行此功能:
rename(pwd,3,'frame')
All files that were named as frame1.jpg
or frame99.jpg
are now named as frame001.jpg
and frame099.jpg
. 现在,所有名为frame1.jpg
或frame99.jpg
都被命名为frame001.jpg
和frame099.jpg
。
Hope this helps! 希望这可以帮助!
If you have the Computer Vision System Toolbox you can use the vision.VideoFileWriter object. 如果您拥有计算机视觉系统工具箱,则可以使用vision.VideoFileWriter对象。 You can simply feed images into its step()
method one at a time, and they will be written to the video file as video frames. 您可以简单地一次将图像馈入其step()
方法,然后将它们作为视频帧写入视频文件。 Note that the images must all be the same size and have the same data type. 请注意,图像必须全部具有相同的大小,并具有相同的数据类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.