以下文章来源于深度学习与计算机视觉 ,作者磐怼怼
深度学习与计算机视觉碰撞出了新的火花,本公众号将坚持分享原创计算机视觉技术相关文章。主要分为实战教程、视觉领域最新咨询、国内外最新论文翻译三类。欢迎志同道合的朋友关注。
磐创AI分享
来源 | 深度学习与计算机视觉
作者 | 磐怼怼
import cv2
import numpy as np
# required if you use Google Colab
from google.colab.patches import cv2_imshow
from google.colab import files
1. 加载图像
def read_file(filename):
img = cv2.imread(filename)
cv2_imshow(img)
return img
uploaded = files.upload()
filename = next(iter(uploaded))
img = read_file(filename)
2. 创建边缘蒙版
def edge_mask(img, line_size, blur_value):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_blur = cv2.medianBlur(gray, blur_value)
edges = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, line_size, blur_value)
return edges
line_size = 7
blur_value = 7
edges = edge_mask(img, line_size, blur_value)
cv2_imshow(edges)
3. 减少调色板
def color_quantization(img, k):
# Transform the image
data = np.float32(img).reshape((-1, 3))
# Determine criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 0.001)
# Implementing K-Means
ret, label, center = cv2.kmeans(data, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
result = center[label.flatten()]
result = result.reshape(img.shape)
return result
total_color = 9
img = color_quantization(img, total_color)
blurred = cv2.bilateralFilter(img, d=7,
sigmaColor=200,sigmaSpace=200)
4. 结合边缘蒙版和彩色图像
cartoon = cv2.bitwise_and(blurred, blurred, mask=edges)
扫码看好书,满100减50超值优惠活动等你哦
看到这里,说明你喜欢这篇文章,请点击「在看」或顺手「转发」「点赞」。
欢迎微信搜索「panchuangxx」,添加小编磐小小仙微信,每日朋友圈更新一篇高质量推文(无广告),为您提供更多精彩内容。
▼ ▼ 扫描二维码添加小编 ▼ ▼