View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
K means clustering for Image Compression

3.0 | 1 rating Rate this file 10 Downloads (last 30 days) File Size: 4.99 KB File ID: #36376 Version: 1.5

K means clustering for Image Compression



25 Apr 2012 (Updated )

K-means clustering is a popular vector quantization method for data compression.

| Watch this File

File Information

k-means clustering is used for Image Compression. In this matlab program, the feature vectors are simply the N X N non-overlapping blocks of pixels in the image. Like a scalar quantizer, a vector quantizer has a quantization levels called codevectors and the set of K such codevectors is called codebook of size K.
K-means clustering is an iterative process in which the codevectors are refined every stage by computing the centroid of the input vectors which belong to the respective cluster.

Required Products Image Processing Toolbox
MATLAB Compiler
MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
26 Apr 2014 amir

amir (view profile)

31 Mar 2014 Mayuri

Mayuri (view profile)

I want to compress a gray-scale image using vector quantization. I have not understood how you have generated the codebook. Will you please explain me the steps for generation of the same in your code

Comment only
14 Mar 2013 Edwin

Edwin (view profile)

Hello, thanks for your sharing.

However, when I tried to test this code, i got some errors pposted input image is a 181*217 png image...would you please help me to solve this problem?

Thanks for your help in advanced!

Attempted to access I1(1,2.41421); index must be a positive integer or logical.

Error in ==> K_MEANS_PREPROCESSING at 18
s((L^0.5*size(I1,2))*(i-1)+L*(j-1)+(L^0.5)*(k-1)+l)=I1(sqrt(L)*(i-1)+k, sqrt(L)*(j-1)+l);

Error in ==> Image_kmeans_main at 72

Comment only
08 Jul 2012 Vinay Kumar Tadepalli

Hey Nidhi, Thanks for all the comments. I have updated the file again and this time i have tested for different images and it is working fine. You need to supply input parameters such as L & K for the program. First try with L=4 and K=16. This will execute in approx. 50 sec. Then you can try for other combinations.

Comment only
07 Jul 2012 Nidhi

Nidhi (view profile)

Thanks for your support. This new code doesnot give the error I got earlier, but the program is busy in its operation and finally it says "elapsed time". The program works for about 21 min approx.
Hope you will help me in resolving this issue.



Comment only
07 Jul 2012 Neha

Neha (view profile)

resolved with the above query.. m new to MATLAB.. i need k mean clustering technique to apply on my images for my project having deadline 15th july.. plzz help me for classifying my 255*255 grayscale satellite images into 3 clusters..

will be very grateful to u..
looking for possitive feedback in anticipation...

Comment only
06 Jul 2012 Nidhi

Nidhi (view profile)

In fact I tried using the original code with a 512*512 image but it too gets hanged or matlab shows status as busy and then nothing happens.



Comment only
02 Jul 2012 Vinay Kumar Tadepalli

Hey, Thanks for the feedback. Actually, when i wrote the code, i have used 512 X 512 image and didnt generalize it for all dimensions. I am pasting a new code here which should work for all dimensions of images.
clear all;
I1=imread('C:\Users\as\Documents\MATLAB\Test Images\1.gif');
%% Creating L-dimension vectors from the Image.
% For an Image, L is an integer power of 2.
for i=1:size(I1,1)/sqrt(L)
for j=1:size(I1,2)/sqrt(L)
for k=1:sqrt(L)
for l=1:sqrt(L)
s((L^0.5*size(I1,2))*(i-1)+L*(j-1)+(L^0.5)*(k-1)+l)=I1(sqrt(L)*(i-1)+k, sqrt(L)*(j-1)+l);
K=256; % K is the Codebook Size.
% Reconstructing the Image using the Codebook vectors (Cluster Centres).
for i=1:length(ClusterNum)
for i=1:size(I1,1)/sqrt(L)
for j=1:size(I1,2)/sqrt(L)
for k=1:sqrt(L)
for l=1:sqrt(L)
I_re(sqrt(L)*(i-1)+k, sqrt(L)*(j-1)+l)=s_re((L^0.5*size(I1,2))*(i-1)+L*(j-1)+(L^0.5)*(k-1)+l);
clear s s_re;
figure, imshow(uint8(I_re));

Comment only
01 Jul 2012 Nidhi

Nidhi (view profile)

When I use a .jpg file it says :
"??? Attempted to access I1(1,247); index out of bounds because size(I1)=[185,246].

Error in ==> image_kmeans at 17
sqrt(L)*(j-1)+l); "

I tried changing the size of the image but still it gives the same error. Only the coordinate or pixel value of out of bound changes.
Can you pl. tell me the size of image which I should use.

Comment only
02 Jul 2012 1.1

The file I uploaded previously will fail to work for Images of different dimensions, since it was not generalized for all the dimensions. This updated file contains the instructions for working on different dimensions of Images.

09 Jul 2012 1.2

This file solves the issues with different dimensions of the images and also prints the output in a user-friendly manner.

15 Mar 2013 1.3

I have revamped the code completely to make it more efficient and hence run faster. The bugs in the previous version are fixed and hopefully its bug-free now. See ya!! :)

15 Mar 2013 1.4

I have revamped the code completely to make it more efficient(run Faster). I have rectified all the previous bugs. Feel free to contact me at my email address. See ya!!

22 Apr 2013 1.5

A small bug is fixed.

Contact us