本篇文章为大家展示了使用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。