今天刷朋友圈,突然发现朋友把他的头像变成了动漫卡通形象,和他真实头像也差距不大,就是蛮像的,然后我也很感兴趣,觉得很有意思。于是我也想找一下相关的方法。
首先肯定是万能的淘宝,搜索头像动漫化,有一堆商家提供了相关服务,但是我不想花钱怎么办?突然想到我是程序员嘛,那我可以找一下有没有方法能不能用编程的方式把头像动漫化?
一开始google简单的搜索了一下,果然找到了相关的方法,介绍的是使用opencv来对图像进行卡通转换的方法。
首先简单介绍一下opencv
OpenCV是用于计算机视觉和机器学习的开源python库。它主要针对实时计算机视觉和图像处理。它用于对图像执行不同的操作,然后使用不同的技术对其进行转换。
简单来说,你只需要知道opencv很牛逼,大概是图像处理中最牛逼的库了吧
如果说编程可以实现我们的目的,那应该就是用这个库做到的了
不废话了,直接上代码,下面使用python调用opencv来实现
import cv2
from scipy import stats
import numpy as np
from collections import defaultdict
def update_c(C, hist):
while True:
groups = defaultdict(list)
for i in range(len(hist)):
if (hist[i] == 0):
continue
d = np.abs(C - i)
index = np.argmin(d)
groups[index].append(i)
new_C = np.array(C)
for i, indice in groups.items():
if (np.sum(hist[indice]) == 0):
continue
new_C[i] = int(np.sum(indice * hist[indice]) / np.sum(hist[indice]))
if (np.sum(new_C - C) == 0):
break
C = new_C
return C, groups
# Calculates K Means clustering
def K_histogram(hist):
alpha = 0.001
N = 80
C = np.array([128])
while True:
C, groups = update_c(C, hist)
new_C = set()
for i, indice in groups.items():
if (len(indice) < N):
new_C.add(C[i])
continue
z, pval = stats.normaltest(hist[indice])
if (pval < alpha):
left = 0 if i == 0 else C[i - 1]
right = len(hist) - 1 if i == len(C) - 1 else C[i + 1]
delta = right - left
if (delta >= 3):
c1 = (C[i] + left) / 2
c2 = (C[i] + right) / 2
new_C.add(c1)
new_C.add(c2)
else:
new_C.add(C[i])
else:
new_C.add(C[i])
if (len(new_C) == len(C)):
break
else:
C = np.array(sorted(new_C))
return C
# The main controlling function
def caart(img):
kernel = np.ones((2, 2), np.uint8)
output = np.array(img)
x, y, c = output.shape
for i in range(c):
output[:, :, i] = cv2.bilateralFilter(output[:, :, i], 5, 150, 150)
edge = cv2.Canny(output, 100, 200)
output = cv2.cvtColor(output, cv2.COLOR_RGB2HSV)
hists = []
hist, _ = np.histogram(output[:, :, 0], bins=np.arange(180 + 1))
hists.append(hist)
hist, _ = np.histogram(output[:, :, 1], bins=np.arange(256 + 1))
hists.append(hist)
hist, _ = np.histogram(output[:, :, 2], bins=np.arange(256 + 1))
hists.append(hist)
C = []
for h in hists:
C.append(K_histogram(h))
# print("centroids: {0}".format(C))
output = output.reshape((-1, c))
for i in range(c):
channel = output[:, i]
index = np.argmin(np.abs(channel[:, np.newaxis] - C[i]), axis=1)
output[:, i] = C[i][index]
output = output.reshape((x, y, c))
output = cv2.cvtColor(output, cv2.COLOR_HSV2RGB)
contours, _ = cv2.findContours(edge, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
cv2.drawContours(output, contours, -1, 0, thickness=1)
# cartoon = cv2.bitwise_and(output, output, mask=contours)
for i in range(3):
output[:, :, i] = cv2.erode(output[:, :, i], kernel, iterations=1)
# Laplacian = cv2.Laplacian(output,cv2.CV_8U, ksize=11)
# output=output-Laplacian
return output
output = caart(cv2.imread("src.jpg"))
cv2.imwrite("cartoon.jpg", output)
网上找一张明星的图片试试效果,效果如下,感觉这。。。还行?
image.png
image.png
我想更进一步找找还有没有更好的方案,又在github找到了一个使用ai技术的动漫化方案,这个感觉更高大上一些,果断试试。
项目地址在这里:https://github.com/minivision-ai/photo2cartoon
这里就不贴代码了,这里使用里面的小程序展示一下效果,有兴趣的读者可以浏览相关代码
image.png
这个效果是挺好的,更加彻底一些,但是跟opencv实现的方案相比,它仅仅可以动漫化具体的人物头像那一部分,而不像opencv那样可以对整张图做处理。
当然孰优孰劣,就留给读者自己考虑了。