Domanda

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

È stato utile?

Soluzione

Filter weights should sum to 1.0. Yours do not.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top