This can be applied in eshop websites/apps: filtering products by colors. I wonder if the images are labelled by humans or machines.
Input: An image
Output: Colors
Keyword: k-means clustering
Step 1: Convert an image into its component colors in the form of a matrix.
from matplotlib import image as img
image = img.imread('source_of_image')
r = [] g = [] b = []
for line in image:
for pixel in line:
temp_r, temp_g, temp_b = pixel
r.append(temp_r) g.append(temp_g) b.append(temp_b)
image = img.imread('source_of_image')
r = [] g = [] b = []
for line in image:
for pixel in line:
temp_r, temp_g, temp_b = pixel
r.append(temp_r) g.append(temp_g) b.append(temp_b)
Step 2: Create a Pandas data frame to easily manage the variables.
import pandas as pd
df = pd.DataFrame({'red': r, 'blue': b, 'green': g})
df = pd.DataFrame({'red': r, 'blue': b, 'green': g})
Step 3: Standardize the color (rgb) columns for performing k-means clustering.
from scipy.cluster.vq import whiten
df['scaled_red'] = whiten(df['red'])
df['scaled_blue'] = whiten(df['blue'])
df['scaled_green'] = whiten(df['green'])
df['scaled_red'] = whiten(df['red'])
df['scaled_blue'] = whiten(df['blue'])
df['scaled_green'] = whiten(df['green'])
Step 4: Perform k-means clustering on the matrix to find the dominant colors.
from scipy.cluster.vq import kmeans
cluster_centers, distortion = kmeans(df[['scaled_red', 'scaled_green', 'scaled_blue']], number_of_clusters)
cluster_centers, distortion = kmeans(df[['scaled_red', 'scaled_green', 'scaled_blue']], number_of_clusters)
Step 5: Multiply the standardized color values with their standard deviations to get the original color values
colors = []r_std, g_std, b_std = df[['red', 'green', 'blue']].std()
No comments:
Post a Comment