Kmeans Image Compression

Source: Internet
Author: User
From sklearn. datasets import load_sample_imageimport matplotlib. pyplot as pltfrom sklearn. cluster import kmeansimport numpy as npchina = load_sample_image ("china.jpg") PLT. imshow (China) PLT. show () image = China [: 3,: 3] # reduce the resolution PLT. imshow (image) PLT. show () x = image. reshape (-256,256,256) # recreate array n_colors = 64 # () model = kmeans (n_colors) #64 class clustering center labels = model. fit_predict (x) # color classification of each vertex, 0-63colors = model. cluster_centers _ #64 cluster center value new_image = colors [labels] # color filling new_image = new_image.reshape (image. shape) # restore the original array PLT. imshow (new_image.astype (NP. uint8) # convert to the Data Type and draw the PLT. show ()
# View image size import sysprint (SYS. getsizeof (China) # original image print (SYS. getsizeof (new_image) # New Image
819968128
# Save the original and new images as files to observe the file size.

# Understanding Bayesian theorem: # M Bucket: 7 Red 3 yellow # N Bucket: 1 red 9 yellow # now: a red ball is taken out # question: what is the probability that the red ball is M and N buckets?

Kmeans Image Compression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.