Python中的Blob检测 - 视频无法打开

时间:2017-12-25 10:02:23

标签: python opencv

import numpy as np
import cv2
cap = cv2.VideoCapture('car.avi')
size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.cv.CV_FOURCC(*'AVID')
video = cv2.VideoWriter('6.avi', fourcc, 25, size)
while(1):
    ret, frame = cap.read()
    if not ret: 
        break
    frame = cv2.convertScaleAbs(frame)
    params = cv2.SimpleBlobDetector_Params()
    params.blobColor = 0
    params.filterByColor = True
    params.minArea = 0
    params.filterByArea = False
    params.minThreshold = 120;
    params.maxThreshold = 255;
    detector = cv2.SimpleBlobDetector(params)
    keypoints = detector.detect(frame)
    im_with_keypoints = cv2.drawKeypoints(frame, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    if ret == True:
            video.write(im_with_keypoints)
            cv2.imshow('frame', im_with_keypoints)
    else:
        cap.release()
            video.release()
        break
    k = cv2.waitKey(10) & 0xff
    if k == 27:
        break

1 个答案:

答案 0 :(得分:0)

我修改了你拥有的代码,现在它可以运行了。我使用了python3.5。

import numpy as np
import cv2
cap = cv2.VideoCapture(r'E:/test.mp4')
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
video = cv2.VideoWriter(r'E:/6.avi', fourcc, 25, size)
while(1):
    ret, frame = cap.read()
    if not ret:
        break
    frame = cv2.convertScaleAbs(frame)
    params = cv2.SimpleBlobDetector_Params()
    params.blobColor = 0
    params.filterByColor = True
    params.minArea = 0
    params.filterByArea = False
    params.minThreshold = 120;
    params.maxThreshold = 255;
    ver = (cv2.__version__).split('.')
    if int(ver[0]) < 3:
        detector = cv2.SimpleBlobDetector(params)
    else:
        detector = cv2.SimpleBlobDetector_create(params)
    keypoints = detector.detect(frame)
    im_with_keypoints = cv2.drawKeypoints(frame, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    if ret == True:
        video.write(im_with_keypoints)
        cv2.imshow('frame', im_with_keypoints)
    else:
        cap.release()
        break
    k = cv2.waitKey(10) & 0xff
    if k == 27:
        break