1

所以我剛開始用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); 
+0

jpg文件的名稱是什麼?我在問,因爲你怎麼知道你讀取文件的順序是正確的幀順序?例如,如果在讀取文件列表並寫入視頻對象時,您的文件被命名爲frame1.jpg,frame2.jpg,...,frame10.jpg,frame11.jpg等,則圖像可能會被排序作爲frame1.jpg,frame10.jpg,frame11.jpg,...,frame19.jpg,frame2.jpg等等。所以這可能是爲什麼每第十個圖像不合適的原因。從'fullFileName = fullfile(myFolder,baseFileName)'中移除分號,然後重新運行腳本並查看文件順序。 – Geoff

+0

另外 - 您已經命名並指定了文件數的長度,如下所示:size = length(jpegFiles);'。 'size'是一個內置的MATLAB函數,所以如果你曾經(在代碼的後面的行中)嘗試使用'size'函數,給一個變量賦一個相同的名字將會成爲一個問題。請將其重命名爲'numOfFiles'。 – Geoff

+0

'dir'也存在問題,它按照操作系統給出的順序輸入,可能不會按名稱排序。我建議你檢查你的'jpegfiles'單元格,看看這些文件是否按正確的順序讀取。 – Nishant

回答

0

考慮到可能有太多的文件進行重命名(用零填充),這裏是一個快速的函數,將做它你:你只需要提供存儲圖像的目錄/文件夾,填充(如果少於100個文件,則填充可以是2;如果少於1000個文件,則填充可以是3;等等)模式。該代碼假定有在每個文件中的通用模式(如「框架」或「圖像」),該除去時,只剩下數:

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 

一個例子 - 如果所有文件都'的公共模式框架」,並有不到1000個文件,然後運行該功能

rename(pwd,3,'frame') 

分別命名爲frame1.jpgframe99.jpg現在被命名爲frame001.jpgframe099.jpg的所有文件。

希望這會有所幫助!

0

如果您有計算機視覺系統工具箱,則可以使用vision.VideoFileWriter對象。您只需將圖像按其方式一次一個地輸入其方法step(),並將它們作爲視頻幀寫入視頻文件。請注意,圖像必須全部具有相同的大小並且具有相同的數據類型。

+0

嗨; vision.VideoFileWriter能讓我使用圖像製作視頻嗎?似乎它使用現有的視頻。我很抱歉,我對這個完全陌生,很困惑。謝謝 –

+0

是的。我編輯了這個問題。 – Dima