如何让我们进入到素描世界呢,很简单,只要拍个小视频,用代码将一帧帧图片变为二值图片即可。比如拍摄的视频如下:
生成的视频如下:
直接上代码:
import cv2
import numpy as np
import random
#读取和保存视频路径和各个参数设置
video_path = './视频2.mp4'
video_save_path = './视频3.mp4'
video_fps = 30.0
capture = cv2.VideoCapture(video_path)
# 初始化参数
fps = 30
size = (1280, 720)
videowriter = cv2.VideoWriter(video_save_path,cv2.VideoWriter_fourcc(*'XVID'), fps, size)
while(True):
ref,frame=capture.read()
if ref == False:
break
#灰度化
gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#二值图
ret, thresh = cv2.threshold(gray_img, 127, 255, cv2.THRESH_BINARY)
#变回三通道才能输出成视频
gray3_img = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB)
videowriter.write(gray3_img)
videowriter.release()