I'm trying to detect white objects in a video. The first step is to filter the image so that it leaves only white-color pixels. My first approach was using HSV color space and then checking for high level of VAL channel. Here is the code:
//convert image to hsv
cvCvtColor( src, hsv, CV_BGR2HSV );
cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );
for(int x=0;x<srcSize.width;x++){
for(int y=0;y<srcSize.height;y++){
uchar * hue=&((uchar*) (h_plane->imageData+h_plane->widthStep*y))[x];
uchar * sat=&((uchar*) (s_plane->imageData+s_plane->widthStep*y))[x];
uchar * val=&((uchar*) (v_plane->imageData+v_plane->widthStep*y))[x];
if((*val>170))
*hue=255;
else
*hue=0;
}
}
leaving the result in the hue channel. Unfortunately, this approach is very sensitive to lighting. I'm sure there is a better way. Any suggestions?
Human eyes and brains work together to translate light into color. Light receptors that are present in our eyes transmit the signal to the brain. Our brain then recognizes the color.
4) White has an HSV value of 0-255, 0-255, 255.
To detect colors in images, the first thing you need to do is define the upper and lower limits for your pixel values. Once you have defined your upper and lower limits, you then make a call to the cv2. inRange method which returns a mask, specifying which pixels fall into your specified upper and lower range.
Why are you using HSV? It would make more sense to convert to HSL and use the luminance channel. Of course you will not get only white, but every color with high luminance over a threshold. After all, you can't rely on pure white unless your source image has been overexposed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With