# Smoothing filter

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

## Recommended Posts

I need some help on how to smooth 8 values like a blur effect but they all have to add up to 255... So lets say I have 255 and 100 for the first two and all the rest are 0 I would like to smooth the difference between 255 and 100 so the jump isn't so drastic? Any ideas. Thanks

##### Share on other sites
I'm confused as to what you're asking for. Are you thinking of some form of interpolation?
What do you mean by "add to 255"? Do you want the sum of the 8 elements to be 255?

##### Share on other sites
element = element*max/sum;

a = 200
b = 100
c = 0

sum = 300

a = 200*255/300 = 170
b = 100*255/300 = 85
c = 0*255/300 = 0

##### Share on other sites
Are these just 8 arbitrary values, or do they have a relation to each other spatially (sorta like a 3 by 3 grid of pixels, though that's 9 values). If I get what you're saying, though, you merely wish to average these all together. So for example, with (255, 100, 0, 0, 0, 0, 0, 0), you'd wind up with approximately (44, 44, 44, 44, 44, 44, 44, 44). But you don't want to entirely average them. I'm guessing this jump from 255->44 is the drastic jump you were talking about.

I'd do it this way: First of all, figure out the overall average, in this case 44.375. Then, adjust each value towards this average by a certain percentage. Like so:
void PartialAverage(std::vector<float>& Values, float Fraction){  float Sum = 0.0f;  int i;  for (i = 0; i < Values.size(); ++i)    Sum += Values;  float Average = Sum / (float)Values.size();  for (i = 0; i < Values.size(); ++i)    Values -= (Values - Average) * Fraction;}

Let's say you chose a fractional value of 0.25. Using the example set above, you would wind up with approximately (202, 87, 11, 11, 11, 11, 11, 11). The 202 came from (255 - (255 - 44) * 0.25), the 87 from (100 - (100 - 44) * 0.25), and the 11s from (0 - (0 - 44) * 0.25). You can change your fractional value depending on how quickly you want to values to approach the average. 0.0 will change them not at all, while 1.0 will change them to their average in one single step.

Hopefully that's more or less what you were looking for.

If you're working strictly with integers, then rounding is going to cause some evil problems in the process, as you won't wind up with a set that adds up to the exactly the same. This could be fixed, though, with a bit more work. I think I know the general idea, but I'll wait until I find out if this overall concept is indeed what you needed.[/edit]

##### Share on other sites
Yeah I need to average out the variables, e.g. if red = 255 and blue = 100 I want to be able to set some kind of factor to smooth out the transition from red to blue so I guess some kind of linear interpolation? Thanks

##### Share on other sites
Hmmm not sure that is working either. What I am after is a Gaussian Blur like in Photoshop...

##### Share on other sites
I have been suggested to use a "Box filter"? Would anyone have a tutorial or code for that filter algorithm? Thanks

##### Share on other sites
i remember readiong one in the articles section here at gamedev... but i dont recall the title.