Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to detect white color? [closed]

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?

like image 249
dnul Avatar asked Jun 17 '10 20:06

dnul


People also ask

How can you identify colors?

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.

What is the HSV value of white?

4) White has an HSV value of 0-255, 0-255, 255.

How do I find the color in an image?

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.


1 Answers

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.

like image 80
lornova Avatar answered Oct 03 '22 04:10

lornova