# comparing colors

This topic is 2514 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok I got this idea for a little shader but one hurdle... i want to compare colors (rgb no alpha), and if they are close enough alter them as I see fit. Now I could do this with say a threshold and check every channel to see if it's close enough to the other channel they are the same. Though I also what to confine the resulting colors to either red, green or blue.... I'm mostly curious if there is an easier/ more elegant way to do this then checking every chennel for every pixel in a 5x5 radius for every pixel on the screen?

This is my idea so far.. psuedo...
 float threshold = 0.25f; if ( absolute(r1 - r2) <= threshold ) red channel close enough... // do this for each channel, if all pass then they colors are close enough. // then check each channel to see which is the predominat one, and output that color only. 

Now this is going to be doing an area comparison, so it isn't just going to check 4 pixels per pixel, but probably ~8-16 pixels to compare all surrounding pixels. Hopefully that made sense, I can try to explain more if needed... and thanks.

##### Share on other sites
a common way to compare colors is by taking the MSE (mean squared error http://en.wikipedia.org/wiki/Mean_squared_error )
 float3 delta = r1.rgb-r2.rgb; delta*=weight.rgb;//if you want to weight individual channels if(dot(delta,delta)<treshold) ... 
if you want to find particular areas, you might want to take a look at summed area tables ( http://en.wikipedia.org/wiki/Summed_area_table ).
Another way ist to create a variance map in lower resolution, you basically scale the image down to lets say 1/4th the resolution, saving per pixel the average of 4x4 pixel and the biggest delta, this way you can easily figure out if some 4x4 area falls into the space of the color you seek.

##### Share on other sites
You might find it makes more sense to base your tolerances in HSV instead of RGB too.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633767
• Total Posts
3013735
×