python+opencv移动侦测(帧差法)

系统 2071 0

1.帧差法原理

移动侦测即是根据视频每帧或者几帧之间像素的差异,对差异值设置阈值,筛选大于阈值的像素点,做掩模图即可选出视频中存在变化的桢。 帧差法 较为简单的视频中 物体移动侦测 ,帧差法分为: 单帧差 两桢差 、和 三桢差 。随着帧数的增加是防止检测结果的重影。

2.算法思路

文章以截取视频为例进行单帧差法移动侦测

python+opencv移动侦测(帧差法)_第1张图片

3.python 实现代码

            
              
                def
              
              
                threh
              
              
                (
              
              video
              
                ,
              
              save_video
              
                ,
              
              thres1
              
                ,
              
              area_threh
              
                )
              
              
                :
              
              
    cam 
              
                =
              
               cv2
              
                .
              
              VideoCapture
              
                (
              
              video
              
                )
              
              
                #打开一个视频
              
              
    input_fps 
              
                =
              
               cam
              
                .
              
              get
              
                (
              
              cv2
              
                .
              
              CAP_PROP_FPS
              
                )
              
              
    ret_val
              
                ,
              
               input_image 
              
                =
              
               cam
              
                .
              
              read
              
                (
              
              
                )
              
              
    index
              
                =
              
              
                [
              
              
                ]
              
              
    images
              
                =
              
              
                [
              
              
                ]
              
              
    images
              
                .
              
              append
              
                (
              
              input_image
              
                )
              
              
    video_length 
              
                =
              
              
                int
              
              
                (
              
              cam
              
                .
              
              get
              
                (
              
              cv2
              
                .
              
              CAP_PROP_FRAME_COUNT
              
                )
              
              
                )
              
              
    input_image
              
                =
              
              cv2
              
                .
              
              resize
              
                (
              
              input_image
              
                ,
              
              
                (
              
              
                512
              
              
                ,
              
              
                512
              
              
                )
              
              
                )
              
              
    ending_frame 
              
                =
              
               video_length
    fourcc 
              
                =
              
               cv2
              
                .
              
              VideoWriter_fourcc
              
                (
              
              
                *
              
              
                'XVID'
              
              
                )
              
              
    out 
              
                =
              
               cv2
              
                .
              
              VideoWriter
              
                (
              
              save_video
              
                ,
              
              fourcc
              
                ,
              
               input_fps
              
                ,
              
              
                (
              
              
                512
              
              
                ,
              
              
                512
              
              
                )
              
              
                )
              
              
    gray_lwpCV 
              
                =
              
               cv2
              
                .
              
              cvtColor
              
                (
              
              input_image
              
                ,
              
               cv2
              
                .
              
              COLOR_BGR2GRAY
              
                )
              
              
    gray_lwpCV 
              
                =
              
               cv2
              
                .
              
              GaussianBlur
              
                (
              
              gray_lwpCV
              
                ,
              
              
                (
              
              
                21
              
              
                ,
              
              
                21
              
              
                )
              
              
                ,
              
              
                0
              
              
                )
              
              
    background
              
                =
              
              gray_lwpCV


              
                # es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 4))
              
              

    i 
              
                =
              
              
                0
              
              
                # default is 0
              
              
    outt
              
                =
              
              
                [
              
              
                ]
              
              
                while
              
              
                (
              
              cam
              
                .
              
              isOpened
              
                (
              
              
                )
              
              
                )
              
              
                and
              
               ret_val 
              
                ==
              
              
                True
              
              
                and
              
               i 
              
                <
              
              
                2999
              
              
                :
              
              
                ## if i % 2==1:
              
              
        ret_val
              
                ,
              
               input_image 
              
                =
              
               cam
              
                .
              
              read
              
                (
              
              
                )
              
              
        input_image
              
                =
              
              cv2
              
                .
              
              resize
              
                (
              
              input_image
              
                ,
              
              
                (
              
              
                512
              
              
                ,
              
              
                512
              
              
                )
              
              
                )
              
              
        gray_lwpCV 
              
                =
              
               cv2
              
                .
              
              cvtColor
              
                (
              
              input_image
              
                ,
              
               cv2
              
                .
              
              COLOR_BGR2GRAY
              
                )
              
              
        gray_lwpCV 
              
                =
              
               cv2
              
                .
              
              GaussianBlur
              
                (
              
              gray_lwpCV
              
                ,
              
              
                (
              
              
                21
              
              
                ,
              
              
                21
              
              
                )
              
              
                ,
              
              
                0
              
              
                )
              
              
        diff 
              
                =
              
               cv2
              
                .
              
              absdiff
              
                (
              
              background
              
                ,
              
               gray_lwpCV
              
                )
              
              
        outt
              
                .
              
              append
              
                (
              
              diff
              
                )
              
              
                #跟着图像变换背景
              
              
        tem_diff
              
                =
              
              diff
              
                .
              
              flatten
              
                (
              
              
                )
              
              
        tem_ds
              
                =
              
              pd
              
                .
              
              Series
              
                (
              
              tem_diff
              
                )
              
              
        tem_per
              
                =
              
              
                1
              
              
                -
              
              
                len
              
              
                (
              
              tem_ds
              
                [
              
              tem_ds
              
                ==
              
              
                0
              
              
                ]
              
              
                )
              
              
                /
              
              
                len
              
              
                (
              
              tem_ds
              
                )
              
              
                if
              
              
                (
              
              tem_per 
              
                <
              
              
                0.2
              
              
                )
              
              
                |
              
              
                (
              
              tem_per
              
                >
              
              
                0.75
              
              
                )
              
              
                :
              
              
            background
              
                =
              
              gray_lwpCV
        
              
                else
              
              
                :
              
              
            diff 
              
                =
              
               cv2
              
                .
              
              threshold
              
                (
              
              diff
              
                ,
              
               thres1
              
                ,
              
              
                255
              
              
                ,
              
               cv2
              
                .
              
              THRESH_BINARY
              
                )
              
              
                [
              
              
                1
              
              
                ]
              
              
            ret
              
                ,
              
              thresh 
              
                =
              
               cv2
              
                .
              
              threshold
              
                (
              
              diff
              
                .
              
              copy
              
                (
              
              
                )
              
              
                ,
              
              
                150
              
              
                ,
              
              
                255
              
              
                ,
              
              
                0
              
              
                )
              
              
            contours
              
                ,
              
               hierarchy 
              
                =
              
               cv2
              
                .
              
              findContours
              
                (
              
              thresh
              
                ,
              
              cv2
              
                .
              
              RETR_EXTERNAL
              
                ,
              
              cv2
              
                .
              
              CHAIN_APPROX_SIMPLE
              
                )
              
              
                #     contours, hierarchy = cv2.findContours(diff.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
              
              
                for
              
               c 
              
                in
              
               contours
              
                :
              
              
                if
              
              
                (
              
              cv2
              
                .
              
              contourArea
              
                (
              
              c
              
                )
              
              
                <
              
               area_threh
              
                )
              
              
                |
              
              
                (
              
              cv2
              
                .
              
              contourArea
              
                (
              
              c
              
                )
              
              
                >
              
              
                int
              
              
                (
              
              
                512
              
              
                *
              
              
                512
              
              
                *
              
              
                0.3
              
              
                )
              
              
                )
              
              
                :
              
              
                # 对于矩形区域,只显示大于给定阈值的轮廓(去除微小的变化等噪点)
              
              
                continue
              
              
                (
              
              x
              
                ,
              
               y
              
                ,
              
               w
              
                ,
              
               h
              
                )
              
              
                =
              
               cv2
              
                .
              
              boundingRect
              
                (
              
              c
              
                )
              
              
                # 该函数计算矩形的边界框
              
              
                cv2
              
                .
              
              rectangle
              
                (
              
              input_image
              
                ,
              
              
                (
              
              x
              
                ,
              
               y
              
                )
              
              
                ,
              
              
                (
              
              x
              
                +
              
              w
              
                ,
              
               y
              
                +
              
              h
              
                )
              
              
                ,
              
              
                (
              
              
                0
              
              
                ,
              
              
                255
              
              
                ,
              
              
                0
              
              
                )
              
              
                ,
              
              
                2
              
              
                )
              
               
                index
              
                .
              
              append
              
                (
              
              i
              
                )
              
              
                #     cv2.imshow('contours', input_image)
              
              
                #     cv2.imshow('dis', diff)
              
              
        out
              
                .
              
              write
              
                (
              
              input_image
              
                )
              
              
        images
              
                .
              
              append
              
                (
              
              input_image
              
                )
              
              
        i 
              
                =
              
               i
              
                +
              
              
                1
              
              
    out
              
                .
              
              release
              
                (
              
              
                )
              
              
    cam
              
                .
              
              release
              
                (
              
              
                )
              
              
                return
              
               outt
              
                ,
              
              index
              
                ,
              
              images```

              
                ##调取函数
              
              
outt
              
                =
              
              threh
              
                (
              
              
                'new_video.mp4'
              
              
                ,
              
              
                'test6.mp4'
              
              
                ,
              
              
                25
              
              
                ,
              
              
                3000
              
              
                )
              
            
          

更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论