如何使脸部的颜色和质地均匀

时间:2018-06-21 15:33:36

标签: python opencv image-processing

我正在尝试使用opencv漫画化一张脸,这是原始图像 enter image description here

当前I'm doing

  1. 缩小图像,应用双滤镜并将其放大回原始图像
  2. 然后将原始图像的RGB转换为灰度并遵循 中位数模糊以减少干扰
  3. 应用自适应阈值创建边缘遮罩
  4. 将步骤1中获得的图像与边缘遮罩组合在一起 位图 这是输出 enter image description here

然后使用OpenCV应用non-photorealistic rendering。这是最终的输出enter image description here 我想生成颜色均匀的脸部(也消除光线反射),同时又不影响眼睛,嘴巴。如何通过调整当前代码或opencv(python)中的另一种可能的方法来实现这一目标

1 个答案:

答案 0 :(得分:1)

基于:https://www.pyimagesearch.com/2014/07/07/color-quantization-opencv-using-k-means-clustering/

以下代码可满足您的需求:

import cv2
import numpy as np
from sklearn.cluster import MiniBatchKMeans

n = 32

# read image and convert to gray
img = cv2.imread('./obama.jpg',cv2.IMREAD_COLOR)
img = cv2.resize(img, (0,0), fx=.2, fy=.2)
img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
(h, w) = img.shape[:2]

img =np.reshape(img, (img.shape[0]* img.shape[1], 3))
clt = MiniBatchKMeans(n_clusters=n)
labels = clt.fit_predict(img)
quant = clt.cluster_centers_.astype("uint8")[labels]

quant = np.reshape(quant, (h,w,3))
img = np.reshape(img, (h,w,3))

quant = cv2.cvtColor(quant, cv2.COLOR_LAB2BGR)
img = cv2.cvtColor(img, cv2.COLOR_LAB2BGR)
double = np.hstack([img, quant])
while True:
  cv2.imshow('img', double)
  k = cv2.waitKey(30) & 0xff
  if k == 27:
      break

您可以使用本教程将颜色量化仅应用于包含面部的盒子。

https://realpython.com/face-recognition-with-python/

相关问题