Matlab 将视频分成图片

我们在对视频分析的时候免不了要将视频存成一帧帧的图片形式,以下是matlab将视频分成图片的实现代码。

目录结构

set1/类别/*.avi

直接上代码

video_source_path = 'G:\action_predict\TV human Interaction\videos\set1';%源地址
image_aim_path = 'G:\action_predict\TV human Interaction\images\set1';%目标地址
dirs=dir(video_source_path);
dirs_list={dirs.name};
for i=3:size(dirs_list,2)
    i
    class_name=dirs_list{i};
   
    dirs2=dir(fullfile(video_source_path,class_name,'*.avi'));
    dirs_list2={dirs2.name};
    
    for j=1:size(dirs_list2,2)
        video_name=dirs_list2{j};
         image_path=fullfile(image_aim_path,class_name,video_name(1:end-4));
         video_path=fullfile(video_source_path,class_name,video_name);
         

    if ~exist(image_path)
        mkdir(image_path);
    end
    videohandle=VideoReader(video_path);
 
    videoFrames=read(videohandle);
    [~,~,~,video_num]=size(videoFrames);
   
    for k=1:1:video_num

        mov(k).cdata = videoFrames(:,:,:,k);
        mov(k).colormap = [];
        frame_name=strcat(video_name(1:end-4),'_',num2str(k),'.jpg');
        frame_path=fullfile(image_path,frame_name);
        imwrite(mov(k).cdata, frame_path,'jpg');
    end
    end
end

猜你喜欢

转载自blog.csdn.net/captainhailong/article/details/80064991