我试图在 Ubuntu 16.04 中工作的 opencv-python 中保存处理过的视频。我能够可视化处理后的视频。但是,当我尝试保存视频时,它给了我以下错误。有什么可能的建议吗?
from collections import deque
import cv2
import numpy as np
import matplotlib.pyplot as plt
import imutils
import argparse
black_lower = (0,0,0)
black_upper = (100, 110, 138)
kernel_erode = np.ones((1,1),np.uint8)
kernel_dialate = np.array((2,2),np.uint8)
center = None
ap = argparse.ArgumentParser()
ap.add_argument("-b", "--buffer", type=int, default=2048,help="max buffer size")
args = vars(ap.parse_args())
pts = deque(maxlen = args["buffer"])
video = cv2.VideoCapture('/home/users/Desktop/opencv_files/fun_video.mp4')
while (video.isOpened()):
num, frame = video.read()
frame1 = imutils.resize(frame,width=600)
frame2 = frame1[210:500,11:455]
mask = cv2.inRange(frame2,black_lower,black_upper)
mask1 = cv2.erode(mask,kernel_erode, iterations=1)
mask2 = cv2.dilate(mask1,kernel_dialate,iterations=6)
contours=cv2.findContours(mask2.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(contours) > 0:
c = max(contours,key = cv2.contourArea)
((x,y),radius)=cv2.minEnclosingCircle(c) #(x,y) are the coordinates by which the ants are moving
print (x,y)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
if radius > 6:
cv2.circle(frame2,(int(x),int(y)),int(radius),(0,0,255),1)
cv2.circle(frame2,center,5,(0,0,255),1)
pts.append(center)
for i in xrange(1,len(pts)):
if pts[i-1] is None or pts[i] is None:
continue
thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 0.50)
cv2.line(frame2, pts[i - 1], pts[i], (0, 0, 255), thickness)
cv2.imshow('frame2', frame2)
#cv2.imshow('mask', mask2)
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter('/home/users/Desktop/output7.avi', fourcc, 20.0, (160,160))
#out = cv2.VideoWriter('file.avi', -1, 20.0, (640, 480))
if cv2.waitKey(50) and 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()
错误:
Traceback (most recent call last):
File "/home/users/PycharmProjects/cv_test/rest.py", line 19, in <module>
frame1 = imutils.resize(frame,width=600)
File "/home/users/.local/lib/python2.7/site-packages/imutils /convenience.py", line 69, in resize
(h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'