CommencerCommencer gratuitement

Display dominant colors

We have loaded the following image using the imread() function of the image class of matplotlib.

To display the dominant colors, convert the colors of the cluster centers to their raw values and then converted them to the range of 0-1, using the following formula: converted_pixel = standardized_pixel * pixel_std / 255

The RGB values are stored in a DataFrame, batman_df. The scaled RGB values are stored in columns, scaled_red, scaled_blue and scaled_green. The cluster centers are stored in the variable cluster_centers, which were generated using the kmeans() function with three clusters.

Cet exercice fait partie du cours

Cluster Analysis in Python

Afficher le cours

Instructions

  • Get standard deviations of each color from the DataFrame and store it in r_std, g_std, b_std.
  • For each cluster center, convert the standardized RGB values to scaled values in the range of 0-1.
  • Display the colors of the cluster centers.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Get standard deviations of each color
____, ____, ____ = batman_df[['red', 'green', 'blue']].___()

for cluster_center in cluster_centers:
    scaled_r, scaled_g, scaled_b = cluster_center
    # Convert each standardized value to scaled value
    colors.append((
        scaled_r * ____ / ____,
        scaled_g * ____ / ____,
        scaled_b * ____ / ____
    ))

# Display colors of cluster centers
plt.____(____)
plt.show()
Modifier et exécuter le code