所以我剛開始用MATLAB中的圖像處理/計算機視覺。幀到視頻轉換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)'中移除分號,然後重新運行腳本並查看文件順序。 – Geoff
另外 - 您已經命名並指定了文件數的長度,如下所示:size = length(jpegFiles);'。 'size'是一個內置的MATLAB函數,所以如果你曾經(在代碼的後面的行中)嘗試使用'size'函數,給一個變量賦一個相同的名字將會成爲一個問題。請將其重命名爲'numOfFiles'。 – Geoff
'dir'也存在問題,它按照操作系統給出的順序輸入,可能不會按名稱排序。我建議你檢查你的'jpegfiles'單元格,看看這些文件是否按正確的順序讀取。 – Nishant