前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在Python中使用OpenCV绘画和素描

在Python中使用OpenCV绘画和素描

作者头像
代码医生工作室
发布2020-07-24 10:34:14
1.9K0
发布2020-07-24 10:34:14
举报

来源 | Medium

编辑 | 代码医生团队

OpenCV是功能强大的计算机视觉库,具有强大的图像处理工具包。在本文中将利用它来创建绘图和绘画,其中大多数将使用内置功能!简短介绍一下,直接进入令人兴奋的部分。

目录

  • 要求
  • 油画效果
  • 水彩效果
  • 黑色和白色和彩色的铅笔素描
  • 点画艺术

要求

油画效果需要使用OpenCV Contrib模块,而其他模块可以使用OpenCV的标准发行版执行。除此之外,点画艺术还需要Sklearn和Scipy。

pip install opencv-contrib-python==4.3.0.36

pip install scikit-learn

pip install scipy

油画效果

它包括在内cv2.xphoto(),还具有其他一些很酷的功能,例如图像修复,白平衡,图像去噪等。

import cv2

img = cv2.imread('img.jpg')

res = cv2.xphoto.oilPainting(img, 7, 1)

原始图片

油画效果

水彩效果

像油画效果一样,水彩效果也可以用单行代码完成,但不包括导入和图像读取。

cv2.stylization()。

import cv2

img = cv2.imread('img.jpg')

res = cv2.stylization(img, sigma_s=60, sigma_r=0.6)

# sigma_s controls the size of the neighborhood. Range 1 - 200

# sigma_r controls the how dissimilar colors within the neighborhood will be averaged. A larger sigma_r results in large regions of constant color. Range 0 - 1

水彩效果

黑白和彩色铅笔素描

同样,只需一行代码,我们就可以得到灰度和彩色的出色草图。

import cv2

img = cv2.imread('img.jpg')

dst_gray, dst_color = cv2.pencilSketch(img, sigma_s=60, sigma_r=0.07, shade_factor=0.05)

# sigma_s and sigma_r are the same as in stylization.

# shade_factor is a simple scaling of the output image intensity. The higher the value, the brighter is the result. Range 0 - 0.1

黑白素描

彩色的素描

点画艺术

根据维基百科,点画艺术可以定义为:

点画法是一种绘画技术,在该技术中,将小而独特的颜色点应用到图案中以形成图像

要在Python中执行此操作,第一步是计算使用Kmeans的最常用颜色。使用的调色板为20,这意味着图像中将出现20种最常用的颜色来构成点。根据图像尺寸为点计算合适的半径尺寸。然后,遍历图像并找到最接近点的颜色,并以此绘制圆圈。

import scipy.spatial

import numpy as np

import random

import cv2

import math

from sklearn.cluster import KMeans

def compute_color_probabilities(pixels, palette):

    distances = scipy.spatial.distance.cdist(pixels, palette)

    maxima = np.amax(distances, axis=1)

distances = maxima[:, None] - distances

    summ = np.sum(distances, 1)

    distances /= summ[:, None]

    return distances

def get_color_from_prob(probabilities, palette):

    probs = np.argsort(probabilities)

    i = probs[-1]

    return palette[i]

def randomized_grid(h, w, scale):

    assert (scale > 0)

    r = scale//2

    grid = []

    for i in range(0, h, scale):

        for j in range(0, w, scale):

            y = random.randint(-r, r) + i

            x = random.randint(-r, r) + j

    grid.append((y % h, x % w))

    random.shuffle(grid)

    return grid

def get_color_palette(img, n=20):

    clt = KMeans(n_clusters=n)

    clt.fit(img.reshape(-1, 3))

return clt.cluster_centers_

def complement(colors):

    return 255 - colors

def create_pointillism_art(image_path, primary_colors):



    img = cv2.imread(image_path)

    radius_width = int(math.ceil(max(img.shape) / 1000))

    palette = get_color_palette(img, primary_colors)

    complements = complement(palette)

    palette = np.vstack((palette, complements))

    canvas = img.copy()

    grid = randomized_grid(img.shape[0], img.shape[1], scale=3)



    pixel_colors = np.array([img[x[0], x[1]] for x in grid])



    color_probabilities = compute_color_probabilities(pixel_colors, palette)

for i, (y, x) in enumerate(grid):

        color = get_color_from_prob(color_probabilities[i], palette)

        cv2.ellipse(canvas, (x, y), (radius_width, radius_width), 0, 0, 360, color, -1, cv2.LINE_AA)

    return canvas

res = create_pointillism_art('img.jpg', 20)

原始图片

结果

点画艺术的代码受此GitHub存储库的启发而进行了一些更改。

https://github.com/atriwal/Points_Art

因此发现使用OpenCV进行艺术创作很容易,尤其是使用内置功能时。如果要查看使用OpenCV进行图像编辑的操作,可以参考本文:

https://medium.com/dataseries/designing-image-filters-using-opencv-like-abode-photoshop-express-part-1-8765e3f4495b

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 相约机器人 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档