日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python+opencv移動偵測(幀差法)

系統 2485 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條評論
主站蜘蛛池模板: 大埔区| 昌都县| 香格里拉县| 梨树县| 平乡县| 雷山县| 甘南县| 江口县| 姚安县| 乌兰县| 揭东县| 陇西县| 桐庐县| 封丘县| 诸城市| 华容县| 澄城县| 彝良县| 黄大仙区| 长兴县| 平阴县| 蒙城县| 平度市| 华容县| 南京市| 蒲城县| 阳原县| 本溪市| 四会市| 娄底市| 万年县| 惠安县| 雅安市| 宁海县| 鄢陵县| 兰坪| 临清市| 青浦区| 西吉县| 枣庄市| 含山县|