您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用OpenCV怎么對人臉圖像進行卡通化,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
import cv2 import numpy as np
在這個轉換中,我們將找到圖像的邊緣,并使用雙邊濾波器和位操作符制作一個卡通化的圖像。
# Reading the Image image = cv2.imread("image1.jpg") # Finding the Edges of Image gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.medianBlur(gray, 7) edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 10) # Making a Cartoon of the image color = cv2.bilateralFilter(image, 12, 250, 250) cartoon = cv2.bitwise_and(color, color, mask=edges) #Visualize the cartoon image cv2.imshow("Cartoon", cartoon) cv2.waitKey(0) # "0" is Used to close the image window cv2.destroyAllWindows()
在第二次變換中,我們嘗試用一個邊緣保持濾波器來模糊圖像,并在邊緣上加入一個閾值。在這里我們使用的是高斯模糊。
#convert to gray scale grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #apply gaussian blur grayImage = cv2.GaussianBlur(grayImage, (3, 3), 0) #detect edges edgeImage = cv2.Laplacian(grayImage, -1, ksize=5) edgeImage = 255 - edgeImage #threshold image ret, edgeImage = cv2.threshold(edgeImage, 150, 255, cv2.THRESH_BINARY) #blur images heavily using edgePreservingFilter edgePreservingImage = cv2.edgePreservingFilter(image, flags=2, sigma_s=50, sigma_r=0.4) #create output matrix output =np.zeros(grayImage.shape) #combine cartoon image and edges image output = cv2.bitwise_and(edgePreservingImage, edgePreservingImage, mask=edgeImage) #Visualize the cartoon image cv2.imshow("Cartoon", output) cv2.waitKey(0) # "0" is Used to close the image window cv2.destroyAllWindows()
在這一變換過程中,我們將運用風格化的手法,創造出形象的卡通效果。
cartoon_image = cv2.stylization(image, sigma_s=150, sigma_r=0.25) cv2.imshow('cartoon', cartoon_image) cv2.waitKey(0) cv2.destroyAllWindows()
在這個變換中,我們將分別創建一個彩色和黑白的鉛筆素描草圖形象。
cartoon_image1, cartoon_image2 = cv2.pencilSketch(image, sigma_s=60, sigma_r=0.5, shade_factor=0.02) cv2.imshow('pencil', cartoon_image1) cv2.waitKey() cv2.destroyAllWindows()
cv2.imshow('pencil', cartoon_image2) cv2.waitKey() cv2.destroyAllWindows()
上述內容就是使用OpenCV怎么對人臉圖像進行卡通化,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。