简要描述;简介
我对你的问题很感兴趣,所以我尝试了你建议的网站代码,你发布的代码,我自己用谷歌搜索了一些尝试。甚至与我的同龄人讨论过,我的教授使用 C# 教授介绍性图像处理/计算机视觉,这是我几年前学习的。
讨论反馈
可悲的是,他们的反应都一样,就像我最初的想法一样,不可能直接转换/转换为帖子中的第二张图片,发布的第二张图片很可能是艺术图形照片。好吧,也许您深入挖掘也许实际上有一个模块或库可以像第二张图片一样实际转换/转换它 100%。
示例代码测试
所以,我开始尝试你发布的网站的内容,在那里剪了一点,调整了一些,但总的来说,离第二张卡通图片还差得很远。
- “使用 OpenCV 将图像转换为卡通”的代码和结果
import cv2
from matplotlib import pyplot as plt
# Reading image
img = cv2.imread("img.png")
plt.imshow(img)
# Converting to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
# Detecting edges of the input image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 9)
edges = cv2.adaptiveThreshold(
gray, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 9, 9
)
# Cartoonifying the image
color = cv2.bilateralFilter(img, 9, 250, 250)
cartoon = cv2.bitwise_and(color, color, mask=edges)
plt.imshow(cartoon)
plt.savefig("cartoonify.png")
plt.show()

- 继续前进,然后我在帖子中尝试了您的代码,它实际上产生了一些差异,并且运行速度并不慢或没有进行任何更改。我运行了你的代码,它确实做了一些改变,代码几乎保持不变,只是在最后添加了保存图像的方法,
cv2.imwrite()
.
import cv2
import matplotlib.pyplot as plt
window_name = "image"
img = cv2.imread("img.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
edges = cv2.adaptiveThreshold(
gray, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
9, 9
)
color = cv2.bilateralFilter(img, 9, 250, 250)
cartoon = cv2.bitwise_and(color, color, mask=edges)
cv2.imshow(window_name, cartoon)
cv2.waitKey(0)
cv2.imwrite("cartoon_op.png", cartoon)
cv2.waitKey(0)
cv2.destroyAllWindows()

- 第三,我在github上搜索,找到了这段代码,但是为此我使用了我的 stackoverlfow 个人资料图片,这是一张头像,我认为白色背景可能会产生更明显的差异,但与之前的示例相比,它没有,它非常接近。
import cv2
import numpy as np
from tkinter.filedialog import *
photo = askopenfilename()
img = cv2.imread(photo)
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
grey = cv2.medianBlur(grey, 5)
edges = cv2.adaptiveThreshold(grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
#cartoonize
color = cv2.bilateralFilter(img, 9, 250, 250)
cartoon = cv2.bitwise_and(color, color, mask = edges)
cv2.imshow("Image", img)
cv2.imshow("Cartoon", cartoon)
#save
cv2.imwrite("cartoon-git.png", cartoon)
cv2.waitKey(0)
cv2.destroyAllWindows()

- 就在答案快写完之前,我发现这个例子给出了Dev - How to cartoonize an image with Python上最接近卡通化图片示例的结果,这个例子使用了 Elon Musk 的照片来演示,虽然它是最接近卡通的,但它的大小不知何故变得非常小。
import numpy as np
import cv2
file_name = "elon.jpg"
def resize_image(image):
scale_ratio = 0.3
width = int(image.shape[1] * scale_ratio)
height = int(image.shape[0] * scale_ratio)
new_dimensions = (width, height)
resized = cv2.resize(
image, new_dimensions,
interpolation=cv2.INTER_AREA
)
return resized
def find_countours(image):
contoured_image = image
gray = cv2.cvtColor(contoured_image, cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(gray, 30, 100)
contours, hierarchy = cv2.findContours(
edged, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE
)
cv2.drawContours(
contoured_image, contours,
contourIdx=-1, color=1,
thickness=1
)
cv2.imshow("Image after contouring", contoured_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
return contoured_image
def color_quantization(image, k=4):
z = image.reshape((-1, 3))
z = np.float32(z)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,
10000, 0.0001)
compactness, label, center = cv2.kmeans(z, k, None, criteria,
1, cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((image.shape))
return res2
if __name__ == '__main__':
image = cv2.imread(file_name)
resized_image = resize_image(image)
coloured = color_quantization(resized_image)
contoured = find_countours(coloured)
final_image = contoured
save_q = input("Save the image? [y]/[n]: ")
if save_q == "y":
cv2.imwrite("cartoonized_" + file_name, final_image)
print("Image saved!")
原始埃隆.jpg

卡通化的 Elon.jpg

包起来
我希望这个听起来没有明确答案的冗长答案会有所帮助,这正是我发现感兴趣并决定分享发现它的过程。