Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

Smoothing filter

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

[edit]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 this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!