# Detecting colour under different lighting conditions

G'day, I'm trying to use simplecv to read a gauge needle, which I have seen some others do, and am having success with the following:

img = Image('gauge.jpg')
img = img.colorDistance(color=(172,89,115)).stretch(0,50).binarize()
blobs = img.findBlobs()
print "Found angle: %d" % blobs[0].angle()


Example image:

I have taped a large piece of coloured paper to the needle as the original is just black and very fine, so I could easily use a different colour/shape/whatever. The problem I'm having is that this gauge is in a place where I can't control the lighting very well. So the RGB colour that I'm using here that works may not work well under slightly different lighting conditions.

Does anyone have any suggestions on how I can improve the detection? Is there something I can do besides using the specific RGB color in colorDistance() ?

edit retag close merge delete

Sort by » oldest newest most voted

I'm also struggling with a similar problem.So far i figured out to put a piece of colored paper in front of the camera, get the color of the pixel and use it as a reference for brightness/color calibration.This is my code:

from SimpleCV import *
cam = Camera()
bright = 1
while True:
img = cam.getImage() #grab a frame
img = img*bright                # multiply the image by the brightness variable

pixel=img[0,0]                  # reference color
r,g,b=pixel
if r < 128 :                    # change the brightness var until its 128
bright=bright + 0.1         # 0.1 - the speed of change/cycle
elif r > 128 :
bright=bright - 0.1
img.show()


In your case i guess that img.binarize could also work. Hope this helps.I'm new to simplecv and python so sorry if this isn't an elegant solution.

more