MATLAB: How to import and export an image sequence automatically using Video and Image Processing Blockset

Computer Vision Toolboxconvertexportimageimportreadvideowrite

I am using Video and Image Processing Blockset to process a sequence of images named 'foo_001.jpg', 'foo_002.jpg', etc. Can I import this sequence into Simulink as a video, which each image beomes a single video frame? Similarly, can I reverse this process and produce a series of numbered images from a video stream in Simulink?

Best Answer

If you are using Simulink version R2008b or later, please download the attached files, which demonstrate the process of creating a series of images from a video, and then producing a video from that series of images. Running the file vipFileVideoWorkflow.m demonstrates the use of the two models.
For previous product releases, if you wish to process your images as a single video stream, you must preload your image files into a structure supported by the From Workspace block in Simulink. The following code is an example of this process:
% Read three images
im1 = imread('foo_001.jpg');
im2 = imread('foo_002.jpg');
im3 = imread('foo_003.jpg);
% Insert the image data into a multidimensional matrix
imdata(:,:,1) = im1;
imdata(:,:,2) = im2;
imdata(:,:,3) = im3;
% Bring this data into a structure
foo.time=[0; 1; 2];
foo.signals.values= imdata;
foo.signals.dimensions = size(squeeze(imdata(:,:,1)));
In the Simulink From Workspace block, set the Data parameter to foo. The block imports the corresponding image data at simulation times 0, 1 and 2, respectively.
Related Question