如何使用MATLAB從影像序列建立影片?


在MATLAB中,我們可以從影像序列建立影片。在本教程中,我們將探討從影像序列建立影片的步驟,並透過一個示例來理解這個概念。

從影像序列建立影片的過程

下面解釋了從影像序列建立影片的分步過程:

  • 步驟(1) - 首先,將我們要在影片中使用的所有影像收集到一個資料夾中。

  • 步驟(2) - 使用'imread'函式讀取所有影像,並將它們儲存在單元陣列中。

  • 步驟(3) - 使用'VideoWriter'函式建立一個影片物件,並定義影片的名稱、格式、幀率等。

  • 步驟(4) - 指定每張影像的秒數,即影片中每張影像的持續時間。

  • 步驟(5) - 開啟影片檔案,並使用'for'迴圈和'writeVideo'函式將影像寫入影片檔案。

  • 步驟(6) - 結束'for'迴圈並關閉影片檔案。

  • 步驟(7) - 使用'VideoReader'函式顯示建立的影片。

因此,透過遵循這7個步驟,我們可以輕鬆地在MATLAB中從影像序列建立影片。

MATLAB示例

現在,讓我們考慮一個MATLAB中的示例程式來演示從影像建立影片的過程。

% MATLAB Program to create a video from a sequence of images
% Read the images and store in a cell array
images = cell(4, 1);
images{1} = imread('https://tutorialspoint.tw/assets/questions/media/14304-1687425236.jpg');
images{2} = imread('https://tutorialspoint.tw/assets/questions/media/14304-1687425323.jpg');
images{3} = imread('https://tutorialspoint.tw/assets/questions/media/14304-1687425308.jpg');
images{4} = imread('https://tutorialspoint.tw/assets/questions/media/14304-1687425340.jpg');

% Resize all the images to a common size
Img_Size = [480, 640];    % Adjust as per your images
for i = 1:length(images)
   images{i} = imresize(images{i}, Img_Size);
end

% Create a video file with 30 fps
vdo = VideoWriter('video_file.avi', 'Motion JPEG AVI');
vdo.FrameRate = 30;

% Specify seconds per image
sec_per_img = 3;

% Open the created video file
open(vdo);

% Load the images to the video file
for i = 1:length(images)
   % Convert the image to a frame
   f = im2frame(images{i});
   % Write each frame multiple times to match seconds per image
   for j = 1:sec_per_img
      writeVideo(vdo, f);
   end
end

% Close the video file
close(vdo);

輸出

Replace the image URLs with your images. Run the code in your MATLAB environment, having the required toolboxes to write and play the video files.

結論

這就是關於在MATLAB中從影像序列建立影片的全部內容。在本教程中,我們解釋了從影像序列建立影片的分步過程,並透過示例程式演示了這些步驟的實現。

更新於:2023年9月6日

瀏覽量:103

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.