import matplotlib.image as img
import matplotlib.pyplot as plt
from scipy.cluster.vq
import whiten
from scipy.cluster.vq
import kmeans
import pandas as pd
iron_image =
img.imread( 'iron.jpg' ) //created a
random img
r = []
g = []
b = []
for row in
iron_image:
for
temp_r, temp_g, temp_b, temp in
row:
r.append(temp_r)
g.append(temp_g)
b.append(temp_b)
iron_df =
pd.DataFrame({ 'red' : r,
'green'
: g,
'blue'
: b})
iron_df[ 'scaled_color_red' ]
=
whiten(iron_df[ 'red' ])
iron_df[ 'scaled_color_blue' ]
=
whiten(iron_df[ 'blue' ])
iron_df[ 'scaled_color_green' ]
=
whiten(iron_df[ 'green' ])
cluster_centers, _ =
kmeans(iron_df[[ 'scaled_color_red' ,
'scaled_color_blue' ,
'scaled_color_green' ]],
3 )
dominant_colors = []
red_std, green_std, blue_std =
iron_df[[ 'red' ,
'green' ,
'blue' ]].std()
for cluster_center in
cluster_centers:
red_scaled,
green_scaled, blue_scaled =
cluster_center
dominant_colors.append((
red_scaled
* red_std /
255 ,
green_scaled
* green_std /
255 ,
blue_scaled
* blue_std /
255
))
plt.imshow([dominant_colors])
plt.show()
|