zoukankan      html  css  js  c++  java
  • matlab读写视频VideoReader/VideoWriter

    前言

    视频处理分析的过程中,需要用到将视频一帧帧地读取、写入,本文就涉及此问题。

    系统环境

    1.系统:win7_64

    2.matlab版本:matlab2015a

    测试代码

    代码一(读视频):

    %To read video frames.
    clc
    clear 
    close all
    
    fileName = 'E:fatigue_detectiondatasetsegVideosP11_5.avi';  
    obj = VideoReader(fileName); 
    numFrames = obj.NumberOfFrames;                       
    for i = 1 : numFrames      
        frame = read(obj,i);                                 
        imshow(frame);                                        
        imwrite(frame,strcat(num2str(i),'.jpg'),'jpg');  
    end

     代码二(读视频):

    fileName = 'E:fatigue_detectiondatasetsegVideosP11_5.avi';  
    
    xyloObj = VideoReader(fileName);
    
    vidWidth = xyloObj.Width;
    vidHeight = xyloObj.Height;
    % mov = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),'colormap',[]);
    
    while hasFrame(xyloObj)
        frame = readFrame(xyloObj);
        imshow(frame);
    end

    代码三(写视频):

    写视频步骤:

    创建视频文件VideoWriter - > 打开视频文件open - > 获取视频帧并写入视频文件writeVideo -> 关闭视频文件close.

    fileName = 'E:fatigue_detectiondatasetsegVideosP11_5.avi';  
    
    %method2
    xyloObj = VideoReader(fileName);
    vidWidth = xyloObj.Width;
    vidHeight = xyloObj.Height;
    fps = xyloObj.FrameRate;
    
    out = VideoWriter('out.avi');
    out.FrameRate = fps;
    open(out);
    while hasFrame(xyloObj)
        frame = readFrame(xyloObj);
        writeVideo(out, frame);
    end
    close(out);

    代码可参考matlab的help文档.

    注意:

    1.不同版本之间可能会存在一些代码问题,可参考help文档进行修正.

    2.写入视频文件之前要先打开文件,写入完毕之后要关闭文件.

  • 相关阅读:
    ABP理论学习之异常处理
    ABP理论学习之导航(Navigation)
    ABP理论学习之验证DTO
    C#程序实现窗体的最大化/最小化
    残缺棋盘的覆盖问题
    23:区间内的真素数
    最大质因子序列
    02:二分法求函数的零点
    01:查找最接近的元素
    最大连续和问题【四种不同的算法】
  • 原文地址:https://www.cnblogs.com/happyamyhope/p/7770622.html
Copyright © 2011-2022 走看看