Advertisement

Color Search Algorithm

Started by February 07, 2002 03:32 PM
0 comments, last by faet83 23 years ago
I''m trying to search for a specific color in an image. heres what i''m thinking. int img_x, img_y; int x, y; for( x=0; x < img_x; x++ ) { for( y=0; y < img_y; y++ ) { if( img->pixelcolor == Search_Color ) { // if image color is equal to predefined color // output only that color to a seperate image file // creating an image of only the search_color } } } is there a better way to do this? -= Off World Technologies, Inc. =-
-= Off World Technologies, Inc. =-
That''s it, basically.

Keep in mind that your colors actually have three components, though: rgb or hsv depending on the color space you use. Also, the colors will rarely exactly match up from pixel to pixel, so you probably want to check within a tolerance.

Something like this might work: ( abs(r - targetr) < 5) && (abs (g-targetg) < 5) && (abs (b - targetb) < 5)

I just picked 5 out of the air. In practice you''ll want to experiment with the tolerance there until you find something that works well for your application.

This topic is closed to new replies.

Advertisement