OpenCV LogoA while back a friend of mine asked me to help him build an app that used computer vision to detect things like traffic lights & road signs. The idea was to use his Raspberry Pi and Pi Cam as a dashcam in his car. He wanted it to identify traffic lights that were red and play some sort of sound letting him know the light was red and that he needed to stop. Once the light turned green, it would play another sound letting him know that he could proceed. He also wanted to have the app do several other things, all of which were based off of color. Normally I would recommend using something such as a Haar classifier or dedicated algorithm for detecting such objects. But, I also wanted to see for myself whether it would be possible to detect objects based entirely on their color. The results were actually very good and the speed of the app outperformed some of the best algorithms available.

In order for me to know the RGB values of the objects he wanted to detect, I wrote a simple tool that uses Python and OpenCV to filter out all colors except the colors I am interested in, leaving me with the RGB values I needed to plug into the app. Now, I know that there are plenty of tools out there that can be used to identify RGB values of objects in images. But, being the geek that I am, I opted to write my own tool. Plus, this was just another opportunity for me to do some work with computer vision. Since I enjoyed playing around with this tool and found it helpful for my friend’s dashcam project, I thought I would share the app in case anyone else might also find it useful / fun.

Aside from Python, the only thing you need is the OpenCV library compiled for Python. If you don’t have it, don’t worry. I’ve included it in the ZIP linked to at the end of this post. You will also need some images to play with. Since one of the objects my friend wanted to detect was a red traffic light, I have also included 3 images in the ZIP that contain scenes from intersections. Here is the image I used as a demo when writing this post.



You can swap out my images with whatever images you want by simply replacing the image_filename at the beginning of the code with the path to your image of choice. Once you’ve done that, run the Python app. The app will launch a window that contains slider bars at the top and your image at the bottom. Simply adjust the sliders at the top of the app and you will see colors get filtered from your image. Here are a couple of screenshots of my demo while adjusting the sliders.





If you keep adjusting the sliders, you will eventually end up with the RGB values for only the color you are looking for as shown in this final result from filtering out everything except the red traffic lights.


As you can see in the source code below, I have already hardcoded the best values for each of the test images included in the ZIP. When you run the app, the sliders will default to these values (assuming you have uncommented the values for the image you are using). Feel free to modify these values (or the rest of the code) to fit your needs. Enjoy!

import as cv

image_filename = 'images/traffic_light2.jpg'

#mR_val = 254
#mG_val = 105
#mB_val = 0
#MAR_val = 256
#MAG_val = 138
#MAB_val = 17

mR_val = 172
mG_val = 136
mB_val = 150
MAR_val = 256
MAG_val = 256
MAB_val = 256

#mR_val = 144
#mG_val = 100
#mB_val = 134
#MAR_val = 256
#MAG_val = 256
#MAB_val = 205

def set_RGB(pos):
    mR_val = cv.GetTrackbarPos('minR', winName)
    mG_val = cv.GetTrackbarPos('minG', winName)
    mB_val = cv.GetTrackbarPos('minB', winName)

    MAR_val = cv.GetTrackbarPos('maxR', winName)
    MAG_val = cv.GetTrackbarPos('maxG', winName)
    MAB_val = cv.GetTrackbarPos('maxB', winName)
    min_color = cv.CV_RGB(mR_val, mG_val, mB_val)
    max_color = cv.CV_RGB(MAR_val, MAG_val, MAB_val)
    cv.InRangeS(hsv, min_color, max_color, mask)
    cv.ShowImage(winName, mask)

# setup window
winName = 'Color Identifier'
cv.NamedWindow(winName, cv.CV_WINDOW_AUTOSIZE)

min_color = cv.CV_RGB(mR_val, mG_val, mB_val)
max_color = cv.CV_RGB(MAR_val, MAG_val, MAB_val)

cv.CreateTrackbar('minR', winName, mR_val, 256, set_RGB)
cv.CreateTrackbar('minG', winName, mG_val, 256, set_RGB)
cv.CreateTrackbar('minB', winName, mB_val, 256, set_RGB)

cv.CreateTrackbar('maxR', winName, MAR_val, 256, set_RGB)
cv.CreateTrackbar('maxG', winName, MAG_val, 256, set_RGB)
cv.CreateTrackbar('maxB', winName, MAB_val, 256, set_RGB)

img = cv.LoadImage(image_filename)
hsv = cv.CreateImage(cv.GetSize(img), 8, 3)
cv.CvtColor(img, hsv, cv.CV_BGR2HSV)
mask = cv.CreateImage(cv.GetSize(img), 8, 1)

cv.InRangeS(hsv, min_color, max_color, mask)

# show image
cv.ShowImage(winName, mask)

Download: Python source code, OpenCV library, and test images. (5.5MB ZIP)

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.

Related Posts

Leave a Reply