我正在尝试制作一个能使任何图像看起来像卡通漫画的功能.
这是我到目前为止的代码:
import numpy
import cv2
__author__ = "Michael Beyeler"
__license__ = "GNU GPL 3.0 or later"
class Cartoonizer:
def __init__(self):
self.numDownSamples = 1
self.numBilateralFilters = 7
def render(self, img_rgb):
# downsample image using Gaussian pyramid
img_color = img_rgb
for _ in range(self.numDownSamples):
img_color = cv2.pyrDown(img_color)
# repeatedly apply small bilateral filter instead of applying
# one large filter
for _ in range(self.numBilateralFilters):
img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
# upsample image to original size
for _ in range(self.numDownSamples):
img_color = cv2.pyrUp(img_color)
# convert to grayscale and apply bilateral blur
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
for _ in range(self.numBilateralFilters):
img_gray_blur = cv2.bilateralFilter(img_gray, 9, 9, 7)
# detect and enhance edges
img_edge = cv2.adaptiveThreshold(img_gray_blur, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 9, 5)
# convert back to color so that it can be bit-ANDed with color image
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
#Ensure that img_color and img_edge are the same size, otherwise bitwise_and will not work
height = min(len(img_color), len(img_edge))
width = min(len(img_color[0]), len(img_edge[0]))
img_color = img_color[0:height, 0:width]
img_edge = img_edge[0:height, 0:width]
return cv2.bitwise_and(img_color, img_edge)
我原来是这样的:
这是我的脚本输出的内容:
这就是我需要的:
到目前为止我注意到的是:
>我模糊图像的代码有太多的颜色,我需要从浅色到暗色的平滑过渡.
>当我的代码产生大量噪音(“孤独的”黑点)并分割线条时,目标图像有平滑的边缘,即线条.
我尝试过更改一些参数,添加了几个随机过滤器,但我真的不知道接下来要做什么.
任何帮助是极大的赞赏.