【发布时间】:2014-06-19 18:34:57
【问题描述】:
所以我刚开始使用 MATLAB 进行图像处理/计算机视觉。
所以我的第一个任务是将一系列图像(帧)转换为视频。所以我通过在线资源(更具体地说是 MATLAB 网站)找到了一种方法。
所以我实现的是http://www.mathworks.com/help/matlab/examples/convert-between-image-sequences-and-video.html,它为我解决了这个问题。
但是,当我播放它时,视频在某些地方似乎有些跳动。就像它会在中间带来一个不同的帧,让整个视频在那一瞬间变得跳跃。它发生在视频中的几个地方。
有人知道为什么会这样吗?
谢谢
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);
【问题讨论】:
-
jpg 文件的名称是什么?我问是因为你怎么知道你读取文件的顺序是正确的帧顺序?例如,如果您的文件被命名为 frame1.jpg、frame2.jpg、...、frame10.jpg、frame11.jpg 等,当您读取文件列表并写入视频对象时,图像可能会被排序如 frame1.jpg、frame10.jpg、frame11.jpg、...、frame19.jpg、frame2.jpg 等。所以这可能就是为什么每十张图片都不合适的原因。从
fullFileName = fullfile(myFolder, baseFileName)中删除分号并重新运行脚本并查看文件顺序。 -
另外 - 您已经命名并指定了文件数量的长度,如下
size = length(jpegFiles);。size是一个内置的 MATLAB 函数,因此如果您(在后面的代码行中)尝试使用size函数,给一个变量赋予相同的名称将是一个问题。请将此重命名为numOfFiles。 -
dir也有问题,它按照操作系统给出的顺序输入,可能没有按名称排序。我建议您检查您的jpegfiles单元格以查看文件是否以正确的顺序读取。 -
@GeoffHayes 你是对的。文件在每 10 帧出现一次故障。那么关于如何加载文件有什么建议吗?非常感谢你。
-
@user2441667 - 你有多少个文件/帧?如果它们的名称类似于
image1.jpg到image99.jpg,您可以手动使用0填充单个数字图像:image01.jpg,image02.jpg,等。然后当您阅读列表时,它们将按正确顺序排序文件(如果没有,您可以使用 sort 功能为您完成)。
标签: matlab image-processing video-streaming video-processing matlab-cvst