2022-12-23|閱讀時間 ‧ 約 5 分鐘

OpenCV 圖轉動漫

#安裝 OpenCV 相關套件 pip install opencv-python pip install opencv-contrib-python pip install matplotlib
import cv2
import matplotlib.pyplot as plt

def show_with_matplotlib(color_img, title, pos):
    img_RGB = color_img[:, :, ::-1]

    ax = plt.subplot(2, 4, pos)
    plt.imshow(img_RGB)
    plt.title(title, fontsize=10)
    plt.axis('off')

def sketch_image(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_gray = cv2.medianBlur(img_gray, 5)
    edges = cv2.Laplacian(img_gray, cv2.CV_8U, ksize=5)
    ret, threshold = cv2.threshold(edges, 145, 255, cv2.THRESH_BINARY_INV)
    return threshold

# 自訂轉換函式
def cartonize_image(img, gray_mode=False):
    # 處理邊線
    threshold = sketch_image(img)
    # 雙邊濾波器為使影像平滑化的非線性濾波器
    filtered = cv2.bilateralFilter(img, 10, 250, 250)
    # CG
    cartoonized = cv2.bitwise_and(filtered, filtered, mask=threshold)

    if gray_mode:
        return cv2.cvtColor(cartoonized, cv2.COLOR_BGR2GRAY)
    return cartoonized

plt.figure(figsize=(14, 6))
plt.suptitle("OpenCV ToCG", fontsize=14, fontweight='bold')

image = cv2.imread('test123.jpg')

custom_sketch_image = sketch_image(image)
custom_cartonized_image = cartonize_image(image)
custom_cartonized_image_gray = cartonize_image(image, True)

# OpenCV to CG
sketch_gray, sketch_color = cv2.pencilSketch(image, sigma_s=20, sigma_r=0.1, shade_factor=0.1)
stylizated_image = cv2.stylization(image, sigma_s=60, sigma_r=0.07)

show_with_matplotlib(image, "image", 1)
show_with_matplotlib(cv2.cvtColor(custom_sketch_image, cv2.COLOR_GRAY2BGR), 'custom sketch', 2)
show_with_matplotlib(cv2.cvtColor(sketch_gray, cv2.COLOR_GRAY2BGR), 'sketch gray cv2.pencilSketch()', 3)
show_with_matplotlib(sketch_color, 'sketch color cv2.pencilSketch()', 4)
show_with_matplotlib(stylizated_image, 'cv2.stylization()', 5)
show_with_matplotlib(custom_cartonized_image, 'custom CG', 6)
show_with_matplotlib(cv2.cvtColor(custom_cartonized_image_gray, cv2.COLOR_GRAY2BGR), 'custom gray', 7)

plt.show()

分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.