%% Create Custom Image Source
% Specify the path to a folder containing a sequence of road images.

imageFolder = fullfile(toolboxdir('driving'),'drivingdata','roadSequence');

% Store the images in an image datastore. The Ground Truth Labeler app and groundTruthMultisignal object do not natively support image datastores, so it is considered a custom image data source.

imds = imageDatastore(imageFolder);

% Write a reader function, readerFcn, to read images from the datastore. The first input argument to the reader function, sourceName, is not used. The second input argument, currentTimestamp, is converted from a duration scalar to a 1-based index. This format is compatible with reading images from the datastore.

readerFcn = @(~,idx)readimage(imds,seconds(idx));

% Create a custom image source. Load the source name, reader function, and first five timestamps of the datastore into the CustomImageSource object.

sourceName = imageFolder;
sourceParams = struct();
sourceParams.FunctionHandle = readerFcn;
sourceParams.Timestamps = seconds(1:5);
customImgSource = vision.labeler.loading.CustomImageSource;
loadSource(customImgSource,sourceName,sourceParams)

% Read the first frame in the sequence. Display the frame.

signalName = customImgSource.SignalName;
I = readFrame(customImgSource,signalName,1);
figure
imshow(I)

% Copyright 2019 The MathWorks, Inc.