2016-02-28 30 views
0

我正在写一段给定宽度,高度和西格玛值的java代码,它将返回一个二维高斯模板,然后我将能够与另一图像进行卷积。我的高斯2D模板没有返回正确的值

这里是高斯公式2D:

enter image description here

现在我simplyfying这是这样的。我们可以在2个部分,一个与Pi和E.

分这所以在我的代码,我有这样的:

piProduct = Math.pow(2 * Math.PI * Math.pow(sigma,2), -1); 

而且为E部分我在第2个部分又分为(基本和指数):

eulerNumberProductExponent = (Math.pow(x,2) + Math.pow(y,2))/(2 * Math.pow(sigma,2)); 

和底座(与指数):

eulerNumberProduct = Math.pow(Math.E, -1*eulerNumberProductExponent); 

现在我需要做的就是乘以丕p艺术与E部分:

coefficient = piProduct * eulerNumberProduct; 

下面是完整的代码:

public double[][] getGaussianTemplate(int width, int height, double sigma){ 
    double[][] gaussianTemplate = new double [height][width]; 
    double coefficient; 
    double piProduct; 
    double eulerNumberProductExponent; 
    double eulerNumberProduct; 

    piProduct = Math.pow(2 * Math.PI * Math.pow(sigma,2), -1); 
    for (int x = 0; x < width; x++) { 
     for (int y = 0; y < height; y++) { 
      eulerNumberProductExponent = (Math.pow(x,2) + Math.pow(y,2))/(2 * Math.pow(sigma,2)); 
      eulerNumberProduct = Math.pow(Math.E, -1*eulerNumberProductExponent); 
      coefficient = piProduct * eulerNumberProduct; 

      gaussianTemplate[y][x] = coefficient; 
      System.out.println("At x: "+x+" and y: "+y+" the coefficient is: "+coefficient); 
     } 
    } 

    printTemplate(gaussianTemplate,width,height); 

    return gaussianTemplate; 
} 

,现在这是我得到的差= 0.1:

| 15.915494 | 0.000000 | 0.000000 | 0.000000 | 0.000000 
| 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 
| 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 
| 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 
| 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 

这是什么printTemplate(gaussianTemplate,width,height);打印。

按照书上的,我用的0.1西格玛我应该得到这个以下为5x5的模板:

enter image description here

什么是错在这里的代码?

回答

1

通常过滤器由中心向外

for (int x = -width/2; x <= width/2; x++) { 
    for (int y = -height/2; y <= height/2; y++) { 
     eulerNumberProductExponent = (Math.pow(x,2) + Math.pow(y,2))/(2 * Math.pow(sigma,2)); 
     eulerNumberProduct = Math.pow(Math.E, -1*eulerNumberProductExponent); 
     coefficient = piProduct * eulerNumberProduct; 
//   gaussianTemplate[y][x] = coefficient; 
     System.out.println("At x: "+x+" and y: "+y+" the coefficient is: "+coefficient); 
    } 
} 

这会给你围绕中心对称的过滤器来计算。这本书说什么和计算什么是未知的。

+0

“通常过滤器是从中心计算出来的”,请您详细说明一下吗? – user15860

+0

如果检查此链接:http://mathworld.wolfram.com/GaussianFunction.html,点(8),你的意思是使用类似的东西?从-4开始到4? – user15860

+0

确实(0,0)位于中心,达到的最高值 – gpasch