Main Content

read

从ADTF DAT文件选择中读取所有数据项

    描述

    例子

    数据=读(StreamReaderreads all data from theADTFStreamReader目的StreamReader在所有流中,并以结构数组的形式返回它,数据

    例子

    全部收缩

    此示例显示了如何提取和可视化的视频流,该视频流存储在ADTF DAT文件中。它还显示了如何将视频流写入视频文件。

    Download the sample video DAT file.

    downloadurl ='https://ssd.mathworks.com/万博1manbetxsupportfiles/driving/data/sample_can_video_dat.zip';datafolder = fullfile(tempdir,'adtf-video',filesep);选项= WebOptions('暂停',Inf);zipfileName = [datafolder,'sample_can_video_dat.zip'];folderexists =存在(datafolder,'dir');% Create a folder in a temporary directory to save the downloaded file.如果〜folderexists mkdir(datafolter);disp('下载sample_can_video_dat.zip(9.74 mb)...')websave(zipfileName,downloadurl,options);%提取下载文件的内容。disp('提取sample_can_video_dat.zip ...')unzip(zipFileName, dataFolder);结尾

    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:“ plugindirectory:“ plugindirectory:”_________ 1 {'rawcan'} {'un万博1manbetxtupported'} 0 sec 14.805 sec 743 2 {'video'} {'adtf/image'} 0 sec 14.799 sec 149

    来自StreamInfoproperty, note that the index of the video stream is 2. Use the选择功能adtffilereader目的, to select the video stream for reading. The returnedADTFStreamReader目的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是0。这表示下一个readNext通话将返回第一项。

    Preview the first image frame from the stream.

    firstFrame = readNext(streamReader);imshow(firstframe.data.item)

    在创建视频之前,请使用重置function to start reading from the first frame. This resets the value ofCurrentIndexOffset到0。

    重置(streamReader); fprintf(“ currentIndexoffset =%d \ n”,streamReader.currentIndExoffset)
    CurrentIndexOffset = 0

    创建一个VideOwriter您可以用来将图像帧写入视频文件的对象。指定每秒1帧的帧速率。

    videoWriterObj = VideoWriter(“ example_video.avi”);VideOwriterObj.Framerate = 1;Open(VideOwriterObj);

    使用StreamReader对象,一对一的选择中的数据项迭代。这hasnextfunction determines if there is an item left to read as we are incrementally reading the file.readNextreturns the data item which is basically a structure 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);结尾

    另外,您可以使用readfunction, and iterate over it later.

    alldata = read(streamReader)
    allData =带有字段的结构:StreamIndex:2数据:[149×1结构]

    关the connection with the video file.

    关闭(VideOwriterObj);关all

    可视化输出文件example_video.avi使用视频查看器

    inmand(“ example_video.avi”

    输入参数

    全部收缩

    流阅读器, specified as anADTFStreamReader目的。

    输出参数

    全部收缩

    从所有流中选择中的所有数据项,返回为n-经过-1结构数组。n是选择中的流数。每个元素中的元素数据是具有这些字段的结构:

    • StreamIndex- 流的索引来自数据。

    • Data- 结构阵列包含流中的所有数据项及其在微秒中的时间戳。

    版本历史记录

    在R2022a中引入