3

EDIT: My description of a box filter is very wrong (all weights should be the same in a box filter), but the answer provided does fix the problem in the picture. Namely the error of not making sure the sum of the weights was equal to 1.

I'm taking a computer graphics class and I am having some issues getting a smoothing box filter to work. For my attempts I'm using a 3x3 mask and convolving it with a source image. The formula given in my book gives the weights as 1/(2r+1) for discrete and 1/2r for continuous, where r is the radius from the center pixel. So what I'm doing is assigning each value of the 3x3 mask, as

b a b
a 1 a
b a b

where a is 1/3 and b is 1/( ( 2 * sqrt(2) ) +1)

and then convolving it with source image.

The particular library I'm using is CImg which can be found here: http://cimg.sourceforge.net/ and I might as well include my source code and the results.

#include "CImg.h"
#include <cmath>

using namespace cimg_library;
int main() 
{
     CImg<unsigned char> image("zhbackground.bmp"), image2("zhbackground.bmp");
     double a = 1.0/3.0;
     double b = 1.0/((2.0*sqrt(2.0))+1.0);
     CImg<> mask = CImg<>(3,3).fill(b,a,b,a,1,a,b,a,b);
     image2.convolve(mask);
     CImgDisplay main_disp(image,"original"), main_disp2(image2, "second");
     while(1)
     {
        main_disp.wait();main_disp2.wait();
     }
}

original second

Royi
  • 4,640
  • 6
  • 46
  • 64
Tim
  • 301
  • 1
  • 5
  • 8

1 Answers1

3

Filter weights should sum to 1.0. Yours do not.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Felt stupid when I read this, felt stupider when it took me about ten minutes to figure out what I should do to fix it. Obviously just summing the weights and dividing the weights by that sum. – Tim Sep 17 '12 at 01:49
  • @Tim Yeah sorry, I could have been a little more helpful there. And no need to beat yourself up, I think it's an often overlooked point. P.S. If there were any weights that were `<0.0` or `>1.0` then you'd need to add clamping, too. – Mark Ransom Sep 17 '12 at 02:12