You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1 KiB

import cv2
import numpy as np
def sketch(image):
# 将图像转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 图像取反
gray_inverse = cv2.bitwise_not(gray)
# 高斯模糊
gray_inverse = cv2.GaussianBlur(gray_inverse, (15, 15), 50)
# 颜色减淡混合
sketch_image = cv2.divide(gray, 255 - gray_inverse, scale=256)
return sketch_image
def main():
# 打开摄像头
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("无法打开摄像头")
return
while True:
# 读取一帧图像
ret, frame = cap.read()
if not ret:
break
# 将图像转换为素描风格
sketch_frame = sketch(frame)
# 显示原始图像和素描图像
cv2.imshow('Sketch', sketch_frame)
# 按下 'q' 键退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头并关闭所有窗口
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()