0
$\begingroup$

I am a newbie to Data science.I have learned about clustering algorithms(especially K-means algorithm)and I would like to implement this algorithm(using Euclidian distance metric) to segment image on basis of RGB values of the image so that I could find clusters of most similar colours in an image .but I didn't have any relevant dataset with me if anyone has or know where can I find the dataset ? please provide me any direct link .?

$\endgroup$

1 Answer 1

0
$\begingroup$

It seems that you are referring to quantizing the colors in an image using K-Means for vector quantization. So you would, for example, pick an image and then say that you want to reduce the number of colors to five in this image. You are asking for data, which in this case couldn't be simpler: It can be literally any image.

As an example, here is how you would do it in Mathematica:

Pick the famous Mandrill image (although, again, the image doesn't have to be famous!):

mandrill = ExampleData[{"TestImage", "Mandrill"}]

Mandrill

Find the RGB vectors and run K-Means on them:

data = ImageData[mandrill];
clusters = FindClusters[Flatten[data, 1], 5, Method -> "KMeans"];

Replace the RGB vectors with the centroid in each cluster and create the new image:

rules = Dispatch@Flatten[MapThread[
     Function[{cluster, mean}, # -> mean & /@ cluster],
     {clusters, Mean /@ clusters}
     ], 1];

Image[data /. rules]

Color-quantized mandrill

This new image uses only five different colors because we asked for five clusters when running K-means.

The key operation here for you is

data = ImageData[mandrill]

which takes an image and returns a grid of RGB vectors corresponding to the colors of the pixels in the image. You need to find a function like that in your programming language of choice, and then you can use whatever image you like.

$\endgroup$
3
  • $\begingroup$ but I would like to know one more thing here .can I draw an elbow curve for this data so that I can choose the optimal number of clusters for this data instead of predefining the number of clusters to be 5 $\endgroup$
    – læran91
    Commented Dec 1, 2017 at 10:52
  • $\begingroup$ @læran91 Hopefully this will be answered in response to your other question. $\endgroup$
    – C. E.
    Commented Dec 1, 2017 at 11:14
  • $\begingroup$ hmm.yeah.but Thank you @C.E for you help :) $\endgroup$
    – læran91
    Commented Dec 1, 2017 at 11:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.