matlab 视频人物捕捉

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32095699/article/details/80773539
function HW1_4(filename)%视频读取
    Obj = VideoReader(filename);
    Obj.CurrentTime = 0;%视频的开始时间
    FrameCnt = 0;%视频帧数
    writerObj = VideoWriter('result.avi');%定义一个视频文件来存储结果


    figure(1) %视频播放
    while hasFrame(Obj)
       FrameCnt = FrameCnt+1;
       VideoFrame = readFrame(Obj);
       image(VideoFrame);
       lastFrame = VideoFrame;%提取最后一帧-->背景
       pause(1/Obj.FrameRate);
    end

    BackgroundImage = lastFrame;
    Threshold = 0.22;
    Obj.CurrentTime = 0;%视频的开始时间
    index =1;
    MYImg{FrameCnt}= [];

    figure(2);
    while hasFrame(Obj)

       VideoFrame = readFrame(Obj);


       Ig = rgb2gray(abs(im2double(VideoFrame)-im2double(BackgroundImage)));%去掉背景,提取人物
       %VideoFrame[0.255],im2double(VideoFrame)[0,1]
       Ib = imbinarize(Ig,Threshold);%二值化

       subplot(1,5,1),imshow(VideoFrame),title('origin');
       subplot(1,5,2),imshow(Ib),title('Ib');
       subplot(1,5,3),imshow(Ig),title('Ig');


       MYImg{index} = uint8(Ib).*rgb2gray(VideoFrame)+im2uint8(~Ib);%还原出原来图像
       %Ib-->logical;uint8(Ib)-->uint8

       imwrite(MYImg{index},strcat(int2str(index),'.jpg'));
       subplot(1,5,4),imshow(uint8(Ib).*rgb2gray(VideoFrame)),title('uint8(Ib).*rgb2gray(VideoFrame)');
       subplot(1,5,5),imshow(im2uint8(~Ib)),title('im2uint8(~Ib)');

       open(writerObj);
       writeVideo(writerObj,MYImg{index});%写入视频result.avi

       index = index+1;
       pause(1/Obj.FrameRate);

    end


end

原始视频截图:example.avi
这里写图片描述
提取后的视频截图:
这里写图片描述

运行过程截图:
这里写图片描述

没有更多推荐了,返回首页