效果如下:
代码如下:
import cv2
import numpy as np
def rgb_to_sketch(src_image, dst_image):
img_rgb = cv2.imread(src_image_name)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img_gray, ksize=(21, 21), sigmaX=0, sigmaY=0)
img_blend = cv2.divide(img_gray, img_blur, scale=255) # 除法运算,改变的是每个像素的颜色深度
cv2.imwrite(dst_image_name, img_blend)
img_gray2 = cv2.medianBlur(img_gray, 5)
img_edge = cv2.adaptiveThreshold(img_gray2, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, blockSize=3, C=2)
cv2.imwrite(dst_image_name2, img_edge)
if __name__ == '__main__':
src_image_name = 'photo.jpg'
dst_image_name = 'end1.jpg'
dst_image_name2 ='end2.jpg'
rgb_to_sketch(1, 2)