Colour Recognition with Python

In this article, I’ll walk you through a colour recognition task with Python. This process is also known as colour detection. We are going to create a basic application that will help us detect colours in an image. The program will also return the RGB values ​​of the colours, which is useful.

Many graphic designers and web designers will understand how useful RGB values ​​can be. Building a colour recognition tool is a great project to get started with Computer Vision.

Also, Read – Creating a 3D Video with Python.

Colour Recognition with Python using OpenCV

If you’ve never heard of computer vision, now is the best time to find out more. Most areas of machine learning and artificial intelligence are closely related to computer vision. Before getting into the task of colour recognition with Python, I would suggest you to first go through this article, which is based on Computer Vision with Python.

Now, let’s import the libraries we need for this task of Colour recognition with Python. I will use only three libraries for this task. Pandas, NumPy, and OpenCV:

import numpy as np
import pandas as pd
import cv2Code language: JavaScript (javascript)

Now we can move on to our next step, where we will define the image we want to use to test our colour recognition task with Python. You can choose any image you want. The image that I am using for this task can be downloaded from here:

test_image = cv2.imread("color_image.jpg")Code language: JavaScript (javascript)

Colour Recognition

First, we need to train our model to identify colours. To do this, we need data that includes names of colours and values ​​corresponding to those colours. Since most colours can be set using red, green and blue. 

This is why we will be using the RGB format as the data points. I found a ready CSV file with around 1000 colour names and RGB values. You can download the data here:

index=["color", "color_name", "hex", "R", "G", "B"]
csv = pd.read_csv('colors.csv', names=index, header=None)Code language: JavaScript (javascript)

Now as our next step, we will define two functions. For our program to work properly, we need some global variables. You will know how global variables can be useful when working with functions:

clicked = False
r = g = b = xpos = ypos = 0Code language: PHP (php)

Colour Recognition Function

The function below will be called when you will double-click on an area of ​​the image. It will return the name of the colour and the RGB values ​​for that colour. This is where the magic happens:

def recognize_color(R,G,B):
    minimum = 10000
    for i in range(len(csv)):
        d = abs(R- int(csv.loc[i,"R"])) + abs(G- int(csv.loc[i,"G"]))+ abs(B- int(csv.loc[i,"B"]))
        if(d<=minimum):
            minimum = d
            cname = csv.loc[i,"color_name"]
    return cnameCode language: JavaScript (javascript)

Mouse Click Function

The function below is a helper function which is a part of our program which will help in the process of double click:

def mouse_click(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        global b,g,r,xpos,ypos, clicked
        clicked = True
        xpos = x
        ypos = y
        b,g,r = img[y,x]
        b = int(b)
        g = int(g)
        r = int(r)Code language: PHP (php)

Processing with Computer Vision

In this step, we’ll open the image in a new window using the OpenCV methods. And in this window, we will use the functions we defined earlier. The application is so simple that it returns the name of the colour and the colour values ​​when you double click on a certain area of ​​the image:

I hope you liked this article on how we can work on a task of colour recognition with Python. You can have a good idea of how you can implement Computer Vision in real-world tasks. Feel free to ask your valuable questions in the comments section below.

Also, Read – How Machine Learning can be used in Marketing?

Follow Us:

Thecleverprogrammer
Thecleverprogrammer
Articles: 76

Leave a Reply

Discover more from thecleverprogrammer

Subscribe now to keep reading and get access to the full archive.

Continue reading