"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

March 11, 2020

Day #332 - Dress Color Detection

After evaluating a few projects, This Gitproject was helpful  - link

Approach
1. Extract RGB composition for input images
2. Use pre-trained samples are available for White, Black, Red, Green, Blue, Orange, Yellow and Violet
3. KNN to compute nearest Color Match between 1 and 2

Input Image -

Detected color is: red

#https://stackoverflow.com/questions/37022787/color-detection-of-object-in-image
from sklearn.cluster import KMeans
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import cv2
img = cv2.imread(r'D:\PetProject\Multi_Label\Test\SkyblueShirt.jpg')
height, width, dim = img.shape
img = img[int(height/4):int(3*height/4), int(width/4):int(3*width/4), :]
height, width, dim = img.shape
img_vec = np.reshape(img, [height * width, dim] )
kmeans = KMeans(n_clusters=3)
kmeans.fit( img_vec )
unique_l, counts_l = np.unique(kmeans.labels_, return_counts=True)
sort_ix = np.argsort(counts_l)
sort_ix = sort_ix[::-1]
fig = plt.figure()
ax = fig.add_subplot(111)
x_from = 0.05
for cluster_center in kmeans.cluster_centers_[sort_ix]:
ax.add_patch(patches.Rectangle( (x_from, 0.05), 0.29, 0.9, alpha=None,
facecolor='#%02x%02x%02x' % (int(cluster_center[2]), int(cluster_center[1]), int(cluster_center[0]) ) ) )
x_from = x_from + 0.31
plt.show()
#https://stackoverflow.com/questions/9018016/how-to-compare-two-colors-for-similarity-difference
#Point1 has R1 G1 B1
#Point2 has R2 G2 B2
#Distance between colors is
#d=sqrt((r2-r1)^2+(g2-g1)^2+(b2-b1)^2)
#Percentage is
#p=d/sqrt((255)^2+(255)^2+(255)^2)
Happy Learning!!!

No comments: