Codementor Events

AI-COLOR-SEPARATOR

Published Aug 09, 2020
AI-COLOR-SEPARATOR

In here I will be writing out some code to detect top colours in an image using ML and display the stats.

So color extraction from images. Sounds great ha!

A bit of a background
Recently while working with a color picker extension for chrome an idea came into my mind that wouldn't it be great if I could just upload an image into a software or website and that software or website pulls out all different colors being used in that image and shows me some stats about an image lets say a pie chart.

So, I began to go deep into it and realised that in a normal color picking extension in chrome or normal tools we gotta select some part of the image and that tool basically pulls out the color in that part of the image. It means it doesn't consider how the colors are distributed through out the image.

A bit more deeper into it and some searches over the web brought me to the idea that our very own AI and ML based algorithms can be helpful here. An algorithm such as "KMeans" clustering is a perfect fit for this kind of situation where we don't have any kind of idea for how the colors are going to be distributed within that image.

If you are not familiar with KMeans clustering algorithm have a look at this link KMeans Clustering

So, I had a background now on how to solve this kinda problem. Now it was time to CODE!!!
So, I started off with :-

  • Anaconda (I wrote the code in a Jupyter notebook)
  • Installed OpenCV using Anaconda itself (it was super easy. Believe me)
  • Other libraries and packages which I used includes:- SKLearn, Matplotlib, Numpy, Collections, skimage (can be used, although I haven't) and os module.
  • Jumped write into the code
    and Results were just wow !!!
    You can have a look at my repository for which the link is AI-COLOR-DETECT
    or just follow along I will paste the code snippets here with explanation:-

STEPS FOLLOWED :-

  • Load required libraries and packages
  • Load in the image we will work on (Yours can be any I chose beautiful butterflies)
  • Color identification
  • Checking out our creation

STEP - 1

In here we will load all our required libraries and packages from python which include KMeans from sklearn.clusters, matplotlib.pyplot, numpy, cv2, from collections import Counter, os (if you wanna access an image in some other directory)

Screen Shot 2020-08-09 at 11.29.45 PM.png

STEP - 2

In here we will load our image (on which we will work) just to have a look at it! By default image is loaded as a numpy array in BGR channel by OpenCV rather than RGB so color of the image is a bit different from original.

Screen Shot 2020-08-09 at 11.31.01 PM.png
Screen Shot 2020-08-09 at 11.32.53 PM.png

As I told ya before, image color is a bit different from original one so let's convert it into the original looks shall we? We will use cv2.cvtColor which means convert to color format. It takes in two arguments, image to be converted and channels from/to we want to convert the image. In our case BGR to RGB.

Screen Shot 2020-08-09 at 11.35.52 PM.png

STEP - 3

In here we will define functions so as to make our code modular. First function is for converting RGB codes to Hex codes so that its easy for us to plot those colors, second function is for reading in the image via OpenCV and ofcourse, convert it to RGB channel from BGR and final function is where all the magic lies. Our KMeans clustering algorithm will do its magic and separate out colors in form of predictions.

  • FUNCTION 1: Converting RGB codes to Hex codes
    Screen Shot 2020-08-09 at 11.43.09 PM.png

  • FUNCTION 2: Read an image into RGB space
    Screen Shot 2020-08-09 at 11.44.24 PM.png

  • FUNCTION 3: KMeans clustering and Pie Chart
    Screen Shot 2020-08-09 at 11.45.59 PM.png

Let's understand what this function does step by step.

  • Step 1: Reshape the image into a 2 by 2 numpy array because that's the shape of array KMeans algorithm expects as input.
 modified_image = image.reshape(image.shape[0]*image.shape[1], 3)
  • Step 2: Fetch color clusters using KMeans algorithm based on count of colors required to be fetched from the image. Here color_count is one of our argument to the function, we can give any whole numeric value to it.
clf = KMeans(n_clusters = color_count)
  • Step 3: Fit the model on our modified image so as to get some color value predictions based on clusters.
predictions = clf.fit_predict(modified_image)
  • Step 4: Create a counter for predictions, basically it means how many times a cluster has been spotted. For a definition of Collections.counter have a look at this link Collections.counter
counts = Counter(predictions)
  • Step 5: Fetch cluster centers i.e fetch color codes or predictions for clusters (in easy way)
center_colors = clf.cluster_centers_
  • Step 6: Fetch RGB Code values and convert them to Hex codes. It might look confusing at first but when you compare it with code above and values fetched you will understand that we are using counter keys which are actually the clusters (like 5 for example refers to cluster 5) and using it on cluster centers that we fetched above which are basically RGB codes for clusters and then converting them to Hex codes and storing it in list hex_colors and storing RGB colors in other list called rgb_colors.

  • Step 7: Plot pie chart if our argument display chart is True.

if (display_chart):
        plt.figure(figsize = (8, 6))
        plt.pie(counts.values(), labels = hex_colors, colors = hex_colors)
  • Step 8: Return values from function. I returned all variables you can return any values you want to check the values for.
return counts, hex_colors,center_colors, predictions

STEP - 4

Woah! at last we are here. The moment of truth. Let's test our creation.

  • Read in the image with our function read_image().
loaded_image = read_image('butterfly.jpeg')
  • Set arguments for our big fetch_colors() function.
color_count = 10
display_chart  = True
  • Run the algorithm
colors_fetched_counts, colors_fetched, center_colors, predictions = fetch_colors(loaded_image, color_count, display_chart)

SUPERB!!!! Our own color extractor is ready and that too with stats
Screen Shot 2020-08-10 at 12.15.46 AM.png

Image on which I tested the code
butterfly.jpeg

Let's print out the colors fetched:

print(colors_fetched)
>>['#efe2ca',
 '#23201c',
 '#5898d4',
 '#f8f5f1',
 '#909b79',
 '#954b16',
 '#f6dd86',
 '#efdba7',
 '#e49d11',
 '#365979']

Phew!!! So, we are all done, but wait.....
After having a look into it what do you feel could be the use cases of it? Well comment down and share your ideas.
In the meanwhile I will share what I feel for example:-

  • Such an algorithm can be used if we want to create a fun website where you could just upload an image and have a look into its color distribution.
  • We can use it in application where we have 1000s of images and we wanna look for an image with specific color in it, let's say Blue may be.
  • We can create an image separator feature for our devices which will differentiate images based on colors and collect them in separate places so that if we wanna look for all images containing blue we can have it separated out.
  • etc.

I came across this blog post and it was super helpful to me, you may feel that codes are aligned in my blog and this one but I felt that it requires some more explanations for what's happening so I thought of writing one on my own.

Well, that's it for this blog. I hope you enjoyed it and if so comment down and let me know and also like this blog post if you think it was helpful and don't forget to share your ideas on what we can do with this kind of algorithm.

Discover and read more posts from Dharvi
get started
post commentsBe the first to share your opinion
Robert Morgan
8 months ago

Hello Dharvi,

I see that you have an interest in color separation.

I need help to build embedded software that extracts colors from textile designs. Each separated color would be shown with a RGB reference. I also need to be able to change each color in a design using individual RGB references.

I can show you example images of everything that II require.

I’ve just joined code mentor. It would be good to chat with you.

Robert Morgan

Show more replies