框架/背景减法

时间:2019-04-13 05:20:07

标签: python-3.x opencv4

我正在做一个基于对象检测的项目,因为我需要将差异化的帧存储在特定的文件夹中,所以在后台/帧减法的代码中遇到了麻烦

没有针对相同的问题发布我的代码的问题,但是使用stackoverflow戳了一下..am,所以请帮忙

import cv2
import os
import glob 

def extractFrames(pathIn, pathOut):
    os.mkdir(pathOut)

    cap = cv2.VideoCapture(pathIn)
    count = 0

    while (cap.isOpened()):

        # Capture frame-by-frame
        ret, current_frame = cap.read()

        current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
        if count > 1:
            previous_frame_gray = cv2.cvtcolor(previous_frame, cv2.COLOR_BGR2GRAY)

            frame_diff = cv2.absdiff(current_frame_gray,previous_frame_gray)        
            if ret == True:

                print('Read %d frame: ' % count, ret)
                cv2.imwrite(os.path.join(pathOut, "frame{:d}.jpg".format(count)), frame_diff)  # save frame as JPEG file

                count += 1
            else:
                break

        previous_frame = current_frame

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

def main():
    extractFrames(r"C:\Users\mathesn\Downloads\Wildlife.mp4", 'fd3')

if __name__=="__main__":
    main()

0 个答案:

没有答案