# R: k-Means Clustering on Imaging

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**Analysis with Programming**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Enough with the theory we recently published, let’s take a break and have fun on the application of Statistics used in Data Mining and Machine Learning, the

We will utilize the following packages for input and output:

The image is represented by large array of pixels with dimension

Plot the clustered colours:

Possible clusters of pixels on different

I suggest you try it!

*k*-Means Clustering.We will apply this method to an image, wherein we group the pixels intok-means clusteringis a method of vector quantization, originally from signal processing, that is popular for cluster analysis in data mining.k-means clustering aims to partitionnobservations intokclusters in which each observation belongs to the cluster with the nearest mean, serving as a prototype of the cluster. (Wikipedia, Ref 1.)

*k*different clusters. Below is the image that we are going to use,Colorful Bird From Wall321 |

### Download and Read the Image

Let’s get started by downloading the image to our workspace, and tell R that our data is a JPEG file.### Cleaning the Data

Extract the necessary information from the image and organize this for our computation:The image is represented by large array of pixels with dimension

*rows*by*columns*by*channels*— red, green, and blue or RGB.### Plotting

Plot the original image using the following codes:### Clustering

Apply*k*-Means clustering on the image:Plot the clustered colours:

Possible clusters of pixels on different

*k*-Means:I suggest you try it!

### Reference

- K-means clustering.
*Wikipedia*. Retrieved September 11, 2014.

To

**leave a comment**for the author, please follow the link and comment on their blog:**Analysis with Programming**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.