來源 | 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
推薦閱讀
使用深度學習和OpenCV的早期火災探測系統