A method of compressing pictures---machine learning's K-means

Source: Internet
Author: User

Background Description:

RGB encoding: For an image that directly uses 24bit for each pixel, each pixel uses an 8-bit unsigned integer (0-255) to denote red or green or blue.

Compression Purpose:

The 128x128 size picture is represented by the original 24bit-compressed into->16bit to represent the image of each pixel.

Compression method:

For each pixel, use K-means to select 16bits to represent the original 24bits. Of course, the original 24bits is expressed by calculating the cluster of 16bits size per pixel space.

Implementation steps:

1. Read the original 128x128-sized picture into a 3-D matrix A. For example, a (50, 33, 3) indicates that the original image is RowNum = Columnnum = the b dimension in RGB is the corresponding

Color value (0-255). So we get an X = (M X 3) matrix of Piexl colors (where m = 128 * 128 = 16384).

2. Run the K-means algorithm, take k= 16, so that each pixel cluster to a cluster K (the choice about the initial K-point of K-means is randomly selected in X as the centroid of K points). Use K, the location of the centroid point, to represent the original pixel value. Here k=16, we can use four bits to represent the possibility of 16 classifications. Then you need 4bits to record the compressed classification of the current PIEXL. (Of course, there will be a table K (16 colors). Why not 4bits to map, 2 binary conversion is too cumbersome! The direct mapping here), RGB (24bits)).

Compression effect:

Pre-Compression size: 128*128*24 = 393216 bits;

Compressed size: 128*128*4 + 16*24 = 65920 bits;

Compression factor is close to 6!

The code is in Gitlab machine learning ex7.

Core ideas:

K-means image compression, compression is not a color. The original 24-bit color, now also used 24-bit color. But the coding changed, turning the original 24bits color into a 16bits color. And this process is the use of K-means clustering. Then the distinction between colors becomes less obvious, resulting in reduced resolution.

Has fun, good luck!

And this are my picture Kebe compressed:

A method of compressing pictures---machine learning's K-means

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.