[ 挑战杯] Python openCV 处理 图片,视频

  1. 读取摄像头数据保存图片
import cv2
import numpy as np
import sys
import os

videoN = sys.argv[1]
os.makedirs('cap{}'.format(videoN))
cap = cv2.VideoCapture(1)
sz = (640,480)
fps=30
# fourcc = cv2.cv.FOURCC(*'MJPG')
#fourcc = cv2.cv.FOURCC(*'XVID')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
videoWrite = cv2.VideoWriter('output{}.avi'.format(videoN),fourcc,fps,sz)

# videoWrite=cv2.VideoWriter('output.mp4',fourcc,fps,sz)
# videoWrite.open('./output.avi',-1,fps,sz,True)
i=0
while(1):
    ret,frame = cap.read()
    videoWrite.write(frame)
    cv2.imshow("capture",frame)
    cv2.imwrite("./cap{}/cap{}.jpg".format(videoN,str(i)),frame)
    i=i+1
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
print i    
videoWrite.release()
cap.release()
cv2.destroyAllWindows()
  1. 读取图片转化为.avi
import cv2
import numpy as np
import os 
DIR = './output' 
N = len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))]) 
print (N)
# N = 
fourcc = cv2.VideoWriter_fourcc(*'XVID')
sz = (640,480)
fps=20
videoWrite=cv2.VideoWriter('input.avi',fourcc,fps,sz)
for i in range(0,N-1):
    cap = cv2.imread('./output/{}.png'.format(str(i)))
    #fourcc= cv2.cv.FOURCC(*'XVID') 
    videoWrite.write(cap)
    #cv2.imshow("capture",cap)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
videoWrite.release()
cv2.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值