Created
September 17, 2018 16:51
-
-
Save ImadDabbura/30bf36136221b1ef3ea99586a825c486 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Read the image | |
| img = imread('images/my_image.jpg') | |
| img_size = img.shape | |
| # Reshape it to be 2-dimension | |
| X = img.reshape(img_size[0] * img_size[1], img_size[2]) | |
| # Run the Kmeans algorithm | |
| km = KMeans(n_clusters=30) | |
| km.fit(X) | |
| # Use the centroids to compress the image | |
| X_compressed = km.cluster_centers_[km.labels_] | |
| X_compressed = np.clip(X_compressed.astype('uint8'), 0, 255) | |
| # Reshape X_recovered to have the same dimension as the original image 128 * 128 * 3 | |
| X_compressed = X_compressed.reshape(img_size[0], img_size[1], img_size[2]) | |
| # Plot the original and the compressed image next to each other | |
| fig, ax = plt.subplots(1, 2, figsize = (12, 8)) | |
| ax[0].imshow(img) | |
| ax[0].set_title('Original Image') | |
| ax[1].imshow(X_compressed) | |
| ax[1].set_title('Compressed Image with 30 colors') | |
| for ax in fig.axes: | |
| ax.axis('off') | |
| plt.tight_layout(); |
Author
It is an attribute for "km" object. You can look at Sklearn's doc for more details.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where did that cluster_centers_ came from in line 13.