Color Detection using Python OpenCV
The jewel-toned bouquets of wildflowers, emerald-hued blades of grass, snuffed bushes, aquamarine waves, and brown hills of earth that make up our wonderfully painted earth are visible with just a quick look around us.
Colors abound; the list does not stop with red, orange, yellow, green, blue, indigo, and violet. A hidden universe of color is coming at us from all directions that the human brain can barely understand.
So, this topic is all about using Python OpenCV to detect colors in an image. In this topic, we’ll look at how to use OpenCV to see color in a picture by clicking on it.
The following is the link to the CSV file for our dataset: colors.csv
So, let’s get this tutorial started….
First, make sure you have all of the required Python libraries loaded on your system, and then import all of them as shown below.
#import neccessarry modules import numpy as np import pandas as pd import cv2
We’ll start by reading the image and importing the CSV file.
#Reading image with opencv img = cv2.imread('image.jpg') index=['color','color_name','hex','R','G','B'] #read the CSV file with pandas csv = pd.read_csv('colors.csv', names=index, header=None) clicked=False r=g=b=xpos=ypos=0
Now we’ll build a function called ‘click,’ using the event name and mouse pointer coordinates as function parameters. We’ll calculate the RGB values of the pixel by double-clicking it.
#calculate the rgb values of the pixel which we double click def click(event,x,y,flags,param): if event == cv2.EVENT_LBUTTONDBLCLK: global g,b,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)
We’ll next create the ‘color_namer’ function. We calculate a distance(d) that informs us how close we are to the color and choose the one with the least distance.
#return us the color name from RGB values #d = abs(Red – ithRedColor) + (Green – ithGreenColor) + (Blue – ithBlueColor) def color_namer(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 color=csv.loc[i,'color_name'] return color
We’ll set a mouse callback event on a window after constructing one in which the input image will appear.
#Set a mouse callback event on a window cv2.namedWindow('image') cv2.setMouseCallback('image', click)
The cv2.imshow() function is used to draw the picture on the window. When the user double-clicks the window, we use the cv2.rectangle and cv2.putText() functions to draw a rectangle and extract the color name.
while(True): cv2.imshow('image', img) if (clicked): #To display text, draw a rectangle cv2.rectangle(img, (20, 20), (750, 60), (b, g, r), -1) #generating a display text string (color name and RGB values) text = recognize_color(r,g,b) + ' R=' + str(r) + ' G=' + str(g) + ' B=' + str(b) # cv2.putText(img,text,start,font(0-7),fontScale,color,thickness,lineType ) cv2.putText(img,text,(50,50),2,0.8,(255,255,255),2,cv2.LINE_AA) #Text will be displayed in black for really light colours if (r+g+b>=600): cv2.putText(img,text,(50,50),2,0.8,(0,0,0),2,cv2.LINE_AA) clicked=False # when 'esc' is pressed, the loop is ended if cv2.waitKey(20) & 0xFF==27: break cv2.destroyAllWindows()
Hurray, it’s all finished! Let’s have a look at the results.
So now we know how to use OpenCV to detect colors. I hope you all had a good time following along with this tutorial.
Leave a Reply