一張圖片上有上百種顏色,如何在一張圖上篩選出小於五種的基本色,或者在一張圖上進行顏色劃分歸類?
為了做品牌,我們需要從符合品牌調性的圖片中提取品牌色,但一張圖片的顏色有上百種 我們怎麼把這些顏色歸類劃分 提取呢?
如果是編程實現的話,可以對像素進行 K-Means 聚類,找出一幅圖像中最主要的 K 種顏色。
下面的代碼和結果來自:
How-To: OpenCV and Python K-Means Color Clustering
作者是:Adrian Rosebrock
使用 K-Means 對圖片進行顏色聚類的結果(K的值需要用戶指定,需要提取多少種顏色K就設為多少):
(圖片來自PyImageSearch)
(圖片來自PyImageSearch)(圖片來自PyImageSearch)Python 代碼(使用了 OpenCV 庫):# USAGE
# python color_kmeans.py --image images/jp.png --clusters 3
# Author: Adrian Rosebrock
# Website: www.pyimagesearch.com
# import the necessary packages
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import argparse
import utils
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
ap.add_argument("-c", "--clusters", required = True, type = int,
help = "# of clusters")
args = vars(ap.parse_args())
# load the image and convert it from BGR to RGB so that
# we can dispaly it with matplotlib
image = cv2.imread(args["image"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# show our image
plt.figure()
plt.axis("off")
plt.imshow(image)
# reshape the image to be a list of pixels
image = image.reshape((image.shape[0] * image.shape[1], 3))
# cluster the pixel intensities
clt = KMeans(n_clusters = args["clusters"])
clt.fit(image)
# build a histogram of clusters and then create a figure
# representing the number of pixels labeled to each color
hist = utils.centroid_histogram(clt)
bar = utils.plot_colors(hist, clt.cluster_centers_)
# show our color bart
plt.figure()
plt.axis("off")
plt.imshow(bar)
plt.show()
# import the necessary packages
import numpy as np
import cv2
def centroid_histogram(clt):
# grab the number of different clusters and create a histogram
# based on the number of pixels assigned to each cluster
numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
(hist, _) = np.histogram(clt.labels_, bins = numLabels)
# normalize the histogram, such that it sums to one
hist = hist.astype("float")
hist /= hist.sum()
# return the histogram
return hist
def plot_colors(hist, centroids):
# initialize the bar chart representing the relative frequency
# of each of the colors
bar = np.zeros((50, 300, 3), dtype = "uint8")
startX = 0
# loop over the percentage of each cluster and the color of
# each cluster
for (percent, color) in zip(hist, centroids):
# plot the relative percentage of each cluster
endX = startX + (percent * 300)
cv2.rectangle(bar, (int(startX), 0), (int(endX), 50),
color.astype("uint8").tolist(), -1)
startX = endX
# return the bar chart
return bar
在ps中用馬賽克濾鏡,擴大半徑減少圖片上顏色的數量,如果是對比比較明顯一點的圖可以半徑選擇大一點方便取色,但如果整個圖片主色比較明顯,就最好不要半徑太大使最後顏色過於接近了。分別試了一幅圖
Adobe Color CC 自動幫你提取基本色
不說專業的 品牌的色彩和行業關係很大
推薦閱讀:
※為何DL在NLP效果並不是特別好但是Stanford卻開設cs224d這門課?
※簡單解釋一下sparse autoencoder, sparse coding和restricted boltzmann machine的關係?
※有哪些比較好的機器學習,深度學習的網路資源可利用?
※如何理解soft target這一做法?
※神經網路的歷史?