簡體   English   中英

幀到視頻轉換Matlab

[英]frame to video conversion 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);

鑒於可能有太多文件需要重命名(用零填充),這里有個快速功能可以幫您完成:您只需要提供存儲圖像的目錄/文件夾,填充(如果少於100個文件) ,則padding可以為2;如果文件數少於1000,則padding可以為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

例如-如果所有文件都具有“ frame”的通用模式,並且文件數少於1000,則以以下方式運行此功能:

rename(pwd,3,'frame')

現在,所有名為frame1.jpgframe99.jpg都被命名為frame001.jpgframe099.jpg

希望這可以幫助!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM