views:

101

answers:

3

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?

+1  A: 

It's going to be sensitive to lighting - what happens if you view it under a red light!
I don't know if using YUV space is much easier than looking for similar RGB values.

edit - normally you would call something with no color gray (might help you with searches).
If you have a choice this is the worst color to search for because it will reflect any other lighting in the scene. So you will have to detect the overall lighting color and adjust for that.

So what you are looking for are pixels with simlar RGB values, or RGB values in the ratio of the average for objects in the scene.

Martin Beckett
I mean white=grey=no color, actually i'm trying to detect white plastic glasses. Here is a sample image:http://tinypic.com/r/14t1p1x/6
dnul
I'm not sure, you're saying that if i put a red light on top then the color of the glasses would become somehow reddish and instead of looking for white pixels i should look for similar pixels no matter the color?
dnul
if you have white objects in a scene and light them with a red light they are going to look red! In this case you are going to have to find the color of a 'white' object and search for that color
Martin Beckett
+1  A: 

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.

Lorenzo
+1  A: 

Another way would be turn your image into grayscale. White will have very high values (almost >225).

Yet another way would be to AND the R, G and B planes. Only the whites will remain in the resultant image. This way, you're not spending time calculating converting to the HSV colour space. You're just doing a simple AND operation at each pixel.

Utkarsh Sinha