用python画卡通图_需要用Python和OpenCV制作一张卡通漫画版的图片

我正在尝试制作一个能使任何图像看起来像卡通漫画的功能.

这是我到目前为止的代码:

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)

我原来是这样的:

Zq1f4.jpg

这是我的脚本输出的内容:

BWNv8.jpg

这就是我需要的:

BVTSM.jpg

到目前为止我注意到的是:

>我模糊图像的代码有太多的颜色,我需要从浅色到暗色的平滑过渡.

>当我的代码产生大量噪音(“孤独的”黑点)并分割线条时,目标图像有平滑的边缘,即线条.

我尝试过更改一些参数,添加了几个随机过滤器,但我真的不知道接下来要做什么.

任何帮助是极大的赞赏.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值