Here is sample code in python using pymorph and mahotas. It should be pretty trivial to recreate with opencv. If possible, I would choose a different background, something further from the skin tone would simplify the initial threshold value.
import pymorph as m import mahotas def hsv_from_rgb(image): image = image/255.0 r, g, b = image[:,:,0], image[:,:,1], image[:,:,2] m, M = numpy.min(image[:,:,:3], 2), numpy.max(image[:,:,:3], 2) d = M - m

source share