蔡徐坤教你用OpenCV实现素描效果

跟着知乎的文章做的,但最后的视频保存部分没有运行成功,试了好久都没成功,有知道错误的麻烦指点一下,谢谢!

蔡徐坤教你用OpenCV实现素描效果 - Maker毕的文章 - 知乎

import cv2 as cv

# 打开视频
capture = cv.VideoCapture("D:/opencv/cxk.flv")
# 保存视频
fourcc = cv.VideoWriter_fourcc(*'DIVX')
out = cv.VideoWriter("D:/opencv/output.avi", fourcc, 25, (640, 480))
while True:
    ret, frame = capture.read()
    if frame is None:
        break

    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)  # 转为灰度图
    blurred = cv.GaussianBlur(gray, (5, 5), 0)  # 高斯模糊
    threshold = cv.adaptiveThreshold(blurred, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)  # 局部阈值化
    threshold_blurred = cv.GaussianBlur(threshold, (5, 5), 0)  # 再次高斯模糊
    ret, threshold_again = cv.threshold(threshold_blurred, 200, 255, cv.THRESH_BINARY)  # 全局阈值化
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # 获取运算核
    open = cv.bitwise_not(cv.morphologyEx(cv.bitwise_not(threshold_again), cv.MORPH_OPEN, kernel))  # 图像开运算
    open_blurred = cv.GaussianBlur(open, (3, 3), 0)
    # cv.imshow("open_blurred", open_blurred)
    out.write(open_blurred)

    if cv.waitKey(25) == 27:
        break

capture.release()
out.release()
cv.destroyAllWindows()

你可能感兴趣的:(opencv)