reset
Reset stream reader to first data item in ADTF DAT file selection
Syntax
Description
reset(
resets thestreamReader
)adtfStreamReader
object,StreamReader
to the first data item in the associated ADTF DAT file selection. Use this to read from the beginning of the file selection when you are reading data items sequentially with thereadNext
function.
Examples
Extract Video Stream From ADTF DAT File
This example shows how to extract and visualize a video stream, stored in an ADTF DAT file. It also shows how to write the video stream into a video file.
Download the sample video DAT file.
downloadURL ='https://ssd.mathworks.com/supportfiles/driving/data/sample_can_video_dat.zip'; dataFolder = fullfile(tempdir,'adtf-video', filesep); options = weboptions('Timeout', Inf); zipFileName = [dataFolder,'sample_can_video_dat.zip']; folderExists = exist(dataFolder,'dir');% Create a folder in a temporary directory to save the downloaded file.if~folderExists mkdir(dataFolder); disp('Downloading sample_can_video_dat.zip (9.74 MB)...') websave(zipFileName, downloadURL, options);% Extract contents of the downloaded file.disp('Extracting sample_can_video_dat.zip...') unzip(zipFileName, dataFolder);end
Create the ADTF File Reader object.
datFileName = fullfile(dataFolder,"sample_can_video.dat"); fileReader = adtfFileReader(datFileName)
fileReader = DataFileName: "C:\Users\latriwal\AppData\Local\Temp\adtf-video\sample_can_video.dat" DescriptionFileName: "" PluginDirectory: "" StreamCount: 2 StreamInfo: StreamIndex StreamName StreamType StartTime EndTime ItemCount ___________ __________ _______________ _________ __________ _________ 1 {'rawcan'} {'UNSUPPORTED'} 0 sec 14.805 sec 743 2 {'video' } {'adtf/image' } 0 sec 14.799 sec 149
From theStreamInfo
property, note that the index of the video stream is 2. Use theselect
function of theadtfFileReader
object, to select the video stream for reading. The returnedadtfStreamReader
object has all the information about the selection.
streamReader = select(fileReader,2)
streamReader = adtfStreamReader with properties: DataFileName: "C:\Users\latriwal\AppData\Local\Temp\adtf-video\sample_can_video.dat" DescriptionFileName: "" PluginDirectory: "" StreamIndex: 2 StartIndex: 1 EndIndex: 149 CurrentIndexOffset: 0 StartTime: [0×0 duration] EndTime: [0×0 duration] DataCount: 149
Note that the value ofCurrentIndexOffset
is 0. This signifies that the nextreadNext
call will return the first item.
Preview the first image frame from the stream.
firstFrame = readNext(streamReader); imshow(firstFrame.Data.Item)
Before creating a video, use thereset
function to start reading from the first frame. This resets the value ofCurrentIndexOffset
to 0.
reset(streamReader); fprintf("CurrentIndexOffset = %d\n",streamReader.CurrentIndexOffset)
CurrentIndexOffset = 0
创建一个VideoWriter
object that you can use to write image frames to a video file. Specify a frame rate of 1 frame per second.
videoWriterObj = VideoWriter("example_video.avi"); videoWriterObj.FrameRate = 1; open(videoWriterObj);
使用thestreamReader
object, iterate over the data items in the selection one-by-one. ThehasNext
function determines if there is an item left to read as we are incrementally reading the file.readNext
返回的数据项基本上是一个结构re containing the data and the associated timestamp. In every iteration, extract the image frame and write it to the video file.
whilestreamReader.hasNext() streamData = streamReader.readNext(); imageFrame = streamData.Data.Item; frame = im2frame(streamData.Data.Item, gray); writeVideo(videoWriterObj, frame);end
Alternatively, you can read all the image frames at once, using theread
function, and iterate over it later.
allData = read(streamReader)
allData =struct with fields:StreamIndex: 2 Data: [149×1 struct]
Close the connection with the video file.
close(videoWriterObj); closeall
Visualize the output fileexample_video.avi
usingVideo Viewer.
implay("example_video.avi")
Input Arguments
streamReader
—Stream reader
adtfStreamReader
object
Stream reader, specified as anadtfStreamReader
object.
Version History
See Also
adtfFileReader
|select
|adtfStreamReader
|read
|readNext
|hasNext
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)