图像处理之Canny边缘检测

http://blog.csdn.net/jia20003/article/details/41173767

图像处理之Canny 边缘检测

一:历史

Canny边缘检测算法是1986年有John F. Canny开发出来一种基于图像梯度计算的边缘

检测算法,同时Canny本人对计算图像边缘提取学科的发展也是做出了很多的贡献。尽

管至今已经许多年过去,但是该算法仍然是图像边缘检测方法经典算法之一。

二:Canny边缘检测算法

经典的Canny边缘检测算法通常都是从高斯模糊开始,到基于双阈值实现边缘连接结束

。但是在实际工程应用中,考虑到输入图像都是彩色图像,最终边缘连接之后的图像要

二值化输出显示,所以完整的Canny边缘检测算法实现步骤如下:

1.      彩色图像转换为灰度图像

2.      对图像进行高斯模糊

3.      计算图像梯度,根据梯度计算图像边缘幅值与角度

4.      非最大信号压制处理(边缘细化)

5.      双阈值边缘连接处理

6.      二值化图像输出结果

三:各步详解与代码实现

1.      彩色图像转灰度图像

根据彩色图像RGB转灰度公式:gray  =  R * 0.299 + G * 0.587 + B * 0.114

将彩色图像中每个RGB像素转为灰度值的代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <span style="font-size:18px;">int gray = (int) (0.299 * tr + 0.587 * tg + 0.114 * tb);</span>  

2.      对图像进行高斯模糊

图像高斯模糊时,首先要根据输入参数确定高斯方差与窗口大小,这里我设置默认方

差值窗口大小为16x16,根据这两个参数生成高斯卷积核算子的代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <span style="font-size:18px;">      float kernel[][] = new float[gaussianKernelWidth][gaussianKernelWidth];  
  2.         for(int x=0; x<gaussianKernelWidth; x++)  
  3.         {  
  4.             for(int y=0; y<gaussianKernelWidth; y++)  
  5.             {  
  6.                 kernel[x][y] = gaussian(x, y, gaussianKernelRadius);  
  7.             }  
  8.         }</span>  

获取了高斯卷积算子之后,我们就可以对图像高斯卷积模糊,关于高斯图像模糊更详

细的解释可以参见这里:http://blog.csdn.net/jia20003/article/details/7234741实现

图像高斯卷积模糊的代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <span style="font-size:18px;">// 高斯模糊 -灰度图像  
  2. int krr = (int)gaussianKernelRadius;  
  3. for (int row = 0; row < height; row++) {  
  4.     for (int col = 0; col < width; col++) {  
  5.         index = row * width + col;  
  6.         double weightSum = 0.0;  
  7.         double redSum = 0;  
  8.         for(int subRow=-krr; subRow<=krr; subRow++)  
  9.         {  
  10.             int nrow = row + subRow;  
  11.             if(nrow >= height || nrow < 0)  
  12.             {  
  13.                 nrow = 0;  
  14.             }  
  15.             for(int subCol=-krr; subCol<=krr; subCol++)  
  16.             {  
  17.                 int ncol = col + subCol;  
  18.                 if(ncol >= width || ncol <=0)  
  19.                 {  
  20.                     ncol = 0;  
  21.                 }  
  22.                 int index2 = nrow * width + ncol;  
  23.                 int tr1 = (inPixels[index2] >> 16) & 0xff;  
  24.                 redSum += tr1*kernel[subRow+krr][subCol+krr];  
  25.                 weightSum += kernel[subRow+krr][subCol+krr];  
  26.             }  
  27.         }  
  28.         int gray = (int)(redSum / weightSum);  
  29.         outPixels[index] = gray;  
  30.     }  
  31. }</span>  

3.      计算图像X方向与Y方向梯度,根据梯度计算图像边缘幅值与角度大小

高斯模糊的目的主要为了整体降低图像噪声,目的是为了更准确计算图像梯度及边缘

幅值。计算图像梯度可以选择算子有Robot算子、Sobel算子、Prewitt算子等。关于

图像梯度计算更多的解释可以看这里:

http://blog.csdn.net/jia20003/article/details/7664777。

这里采用更加简单明了的2x2的算子,其数学表达如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <span style="font-size:18px;">// 计算梯度-gradient, X放与Y方向  
  2. data = new float[width * height];  
  3. magnitudes = new float[width * height];  
  4. for (int row = 0; row < height; row++) {  
  5.     for (int col = 0; col < width; col++) {  
  6.         index = row * width + col;  
  7.         // 计算X方向梯度  
  8.         float xg = (getPixel(outPixels, width, height, col, row+1) -   
  9.                 getPixel(outPixels, width, height, col, row) +   
  10.                 getPixel(outPixels, width, height, col+1, row+1) -  
  11.                 getPixel(outPixels, width, height, col+1, row))/2.0f;  
  12.         float yg = (getPixel(outPixels, width, height, col, row)-  
  13.                 getPixel(outPixels, width, height, col+1, row) +  
  14.                 getPixel(outPixels, width, height, col, row+1) -  
  15.                 getPixel(outPixels, width, height, col+1, row+1))/2.0f;  
  16.         // 计算振幅与角度  
  17.         data[index] = hypot(xg, yg);  
  18.         if(xg == 0)  
  19.         {  
  20.             if(yg > 0)  
  21.             {  
  22.                 magnitudes[index]=90;                         
  23.             }  
  24.             if(yg < 0)  
  25.             {  
  26.                 magnitudes[index]=-90;  
  27.             }  
  28.         }  
  29.         else if(yg == 0)  
  30.         {  
  31.             magnitudes[index]=0;  
  32.         }  
  33.         else  
  34.         {  
  35.             magnitudes[index] = (float)((Math.atan(yg/xg) * 180)/Math.PI);                    
  36.         }  
  37.         // make it 0 ~ 180  
  38.         magnitudes[index] += 90;  
  39.     }  
  40. }</span>  

在获取了图像每个像素的边缘幅值与角度之后

4.      非最大信号压制

信号压制本来是数字信号处理中经常用的,这里的非最大信号压制主要目的是实现边

缘细化,通过该步处理边缘像素进一步减少。非最大信号压制主要思想是假设3x3的

像素区域,中心像素P(x,y) 根据上一步中计算得到边缘角度值angle,可以将角度分

为四个离散值0、45、90、135分类依据如下:

其中黄色区域取值范围为0~22.5 与157.5~180

绿色区域取值范围为22.5 ~ 67.5

蓝色区域取值范围为67.5~112.5

红色区域取值范围为112.5~157.5

分别表示上述四个离散角度的取值范围。得到角度之后,比较中心像素角度上相邻

两个像素,如果中心像素小于其中任意一个,则舍弃该边缘像素点,否则保留。一

个简单的例子如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <span style="font-size:18px;">// 非最大信号压制算法 3x3  
  2. Arrays.fill(magnitudes, 0);  
  3. for (int row = 0; row < height; row++) {  
  4.     for (int col = 0; col < width; col++) {  
  5.         index = row * width + col;  
  6.         float angle = magnitudes[index];  
  7.         float m0 = data[index];  
  8.         magnitudes[index] = m0;  
  9.         if(angle >=0 && angle < 22.5) // angle 0  
  10.         {  
  11.             float m1 = getPixel(data, width, height, col-1, row);  
  12.             float m2 = getPixel(data, width, height, col+1, row);  
  13.             if(m0 < m1 || m0 < m2)  
  14.             {  
  15.                 magnitudes[index] = 0;  
  16.             }  
  17.         }  
  18.         else if(angle >= 22.5 && angle < 67.5) // angle +45  
  19.         {  
  20.             float m1 = getPixel(data, width, height, col+1, row-1);  
  21.             float m2 = getPixel(data, width, height, col-1, row+1);  
  22.             if(m0 < m1 || m0 < m2)  
  23.             {  
  24.                 magnitudes[index] = 0;  
  25.             }  
  26.         }  
  27.         else if(angle >= 67.5 && angle < 112.5) // angle 90  
  28.         {  
  29.             float m1 = getPixel(data, width, height, col, row+1);  
  30.             float m2 = getPixel(data, width, height, col, row-1);  
  31.             if(m0 < m1 || m0 < m2)  
  32.             {  
  33.                 magnitudes[index] = 0;  
  34.             }  
  35.         }  
  36.         else if(angle >=112.5 && angle < 157.5) // angle 135 / -45  
  37.         {  
  38.             float m1 = getPixel(data, width, height, col-1, row-1);  
  39.             float m2 = getPixel(data, width, height, col+1, row+1);  
  40.             if(m0 < m1 || m0 < m2)  
  41.             {  
  42.                 magnitudes[index] = 0;  
  43.             }  
  44.         }  
  45.         else if(angle >=157.5) // angle 0  
  46.         {  
  47.             float m1 = getPixel(data, width, height, col, row+1);  
  48.             float m2 = getPixel(data, width, height, col, row-1);  
  49.             if(m0 < m1 || m0 < m2)  
  50.             {  
  51.                 magnitudes[index] = 0;  
  52.             }  
  53.         }  
  54.     }  
  55. }</span>  

1.      双阈值边缘连接

非最大信号压制以后,输出的幅值如果直接显示结果可能会少量的非边缘像素被包

含到结果中,所以要通过选取阈值进行取舍,传统的基于一个阈值的方法如果选择

的阈值较小起不到过滤非边缘的作用,如果选择的阈值过大容易丢失真正的图像边

缘,Canny提出基于双阈值(Fuzzy threshold)方法很好的实现了边缘选取,在实际

应用中双阈值还有边缘连接的作用。双阈值选择与边缘连接方法通过假设两个阈值

其中一个为高阈值TH另外一个为低阈值TL则有

a.      对于任意边缘像素低于TL的则丢弃

b.      对于任意边缘像素高于TH的则保留

c.      对于任意边缘像素值在TL与TH之间的,如果能通过边缘连接到一个像素大于

TH而且边缘所有像素大于最小阈值TL的则保留,否则丢弃。代码实现如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <span style="font-size:18px;">Arrays.fill(data, 0);  
  2. int offset = 0;  
  3. for (int row = 0; row < height; row++) {  
  4.     for (int col = 0; col < width; col++) {  
  5.         if(magnitudes[offset] >= highThreshold && data[offset] == 0)  
  6.         {  
  7.             edgeLink(col, row, offset, lowThreshold);  
  8.         }  
  9.         offset++;  
  10.     }  
  11. }</span>  

基于递归的边缘寻找方法edgeLink的代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <span style="font-size:18px;">private void edgeLink(int x1, int y1, int index, float threshold) {  
  2.     int x0 = (x1 == 0) ? x1 : x1 - 1;  
  3.     int x2 = (x1 == width - 1) ? x1 : x1 + 1;  
  4.     int y0 = y1 == 0 ? y1 : y1 - 1;  
  5.     int y2 = y1 == height -1 ? y1 : y1 + 1;  
  6.       
  7.     data[index] = magnitudes[index];  
  8.     for (int x = x0; x <= x2; x++) {  
  9.         for (int y = y0; y <= y2; y++) {  
  10.             int i2 = x + y * width;  
  11.             if ((y != y1 || x != x1)  
  12.                 && data[i2] == 0   
  13.                 && magnitudes[i2] >= threshold) {  
  14.                 edgeLink(x, y, i2, threshold);  
  15.                 return;  
  16.             }  
  17.         }  
  18.     }  
  19. }</span>  

6.      结果二值化显示 - 不说啦,直接点,自己看吧,太简单啦

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <span style="font-size:18px;">// 二值化显示  
  2. for(int i=0; i<inPixels.length; i++)  
  3. {  
  4.     int gray = clamp((int)data[i]);  
  5.     outPixels[i] = gray > 0 ? -1 : 0xff000000;       
  6. }</span>  

最终运行结果:


四:完整的Canny算法源代码

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package com.gloomyfish.filter.study;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.util.Arrays;  
  5.   
  6. public class CannyEdgeFilter extends AbstractBufferedImageOp {  
  7.     private float gaussianKernelRadius = 2f;  
  8.     private int gaussianKernelWidth = 16;  
  9.     private float lowThreshold;  
  10.     private float highThreshold;  
  11.     // image width, height  
  12.     private int width;  
  13.     private int height;  
  14.     private float[] data;  
  15.     private float[] magnitudes;  
  16.   
  17.     public CannyEdgeFilter() {  
  18.         lowThreshold = 2.5f;  
  19.         highThreshold = 7.5f;  
  20.         gaussianKernelRadius = 2f;  
  21.         gaussianKernelWidth = 16;  
  22.     }  
  23.   
  24.     public float getGaussianKernelRadius() {  
  25.         return gaussianKernelRadius;  
  26.     }  
  27.   
  28.     public void setGaussianKernelRadius(float gaussianKernelRadius) {  
  29.         this.gaussianKernelRadius = gaussianKernelRadius;  
  30.     }  
  31.   
  32.     public int getGaussianKernelWidth() {  
  33.         return gaussianKernelWidth;  
  34.     }  
  35.   
  36.     public void setGaussianKernelWidth(int gaussianKernelWidth) {  
  37.         this.gaussianKernelWidth = gaussianKernelWidth;  
  38.     }  
  39.   
  40.     public float getLowThreshold() {  
  41.         return lowThreshold;  
  42.     }  
  43.   
  44.     public void setLowThreshold(float lowThreshold) {  
  45.         this.lowThreshold = lowThreshold;  
  46.     }  
  47.   
  48.     public float getHighThreshold() {  
  49.         return highThreshold;  
  50.     }  
  51.   
  52.     public void setHighThreshold(float highThreshold) {  
  53.         this.highThreshold = highThreshold;  
  54.     }  
  55.   
  56.     @Override  
  57.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  58.         width = src.getWidth();  
  59.         height = src.getHeight();  
  60.         if (dest == null)  
  61.             dest = createCompatibleDestImage(src, null);  
  62.         // 图像灰度化  
  63.         int[] inPixels = new int[width * height];  
  64.         int[] outPixels = new int[width * height];  
  65.         getRGB(src, 0, 0, width, height, inPixels);  
  66.         int index = 0;  
  67.         for (int row = 0; row < height; row++) {  
  68.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  69.             for (int col = 0; col < width; col++) {  
  70.                 index = row * width + col;  
  71.                 ta = (inPixels[index] >> 24) & 0xff;  
  72.                 tr = (inPixels[index] >> 16) & 0xff;  
  73.                 tg = (inPixels[index] >> 8) & 0xff;  
  74.                 tb = inPixels[index] & 0xff;  
  75.                 int gray = (int) (0.299 * tr + 0.587 * tg + 0.114 * tb);  
  76.                 inPixels[index] = (ta << 24) | (gray << 16) | (gray << 8)  
  77.                         | gray;  
  78.             }  
  79.         }  
  80.           
  81.         // 计算高斯卷积核  
  82.         float kernel[][] = new float[gaussianKernelWidth][gaussianKernelWidth];  
  83.         for(int x=0; x<gaussianKernelWidth; x++)  
  84.         {  
  85.             for(int y=0; y<gaussianKernelWidth; y++)  
  86.             {  
  87.                 kernel[x][y] = gaussian(x, y, gaussianKernelRadius);  
  88.             }  
  89.         }  
  90.         // 高斯模糊 -灰度图像  
  91.         int krr = (int)gaussianKernelRadius;  
  92.         for (int row = 0; row < height; row++) {  
  93.             for (int col = 0; col < width; col++) {  
  94.                 index = row * width + col;  
  95.                 double weightSum = 0.0;  
  96.                 double redSum = 0;  
  97.                 for(int subRow=-krr; subRow<=krr; subRow++)  
  98.                 {  
  99.                     int nrow = row + subRow;  
  100.                     if(nrow >= height || nrow < 0)  
  101.                     {  
  102.                         nrow = 0;  
  103.                     }  
  104.                     for(int subCol=-krr; subCol<=krr; subCol++)  
  105.                     {  
  106.                         int ncol = col + subCol;  
  107.                         if(ncol >= width || ncol <=0)  
  108.                         {  
  109.                             ncol = 0;  
  110.                         }  
  111.                         int index2 = nrow * width + ncol;  
  112.                         int tr1 = (inPixels[index2] >> 16) & 0xff;  
  113.                         redSum += tr1*kernel[subRow+krr][subCol+krr];  
  114.                         weightSum += kernel[subRow+krr][subCol+krr];  
  115.                     }  
  116.                 }  
  117.                 int gray = (int)(redSum / weightSum);  
  118.                 outPixels[index] = gray;  
  119.             }  
  120.         }  
  121.           
  122.         // 计算梯度-gradient, X放与Y方向  
  123.         data = new float[width * height];  
  124.         magnitudes = new float[width * height];  
  125.         for (int row = 0; row < height; row++) {  
  126.             for (int col = 0; col < width; col++) {  
  127.                 index = row * width + col;  
  128.                 // 计算X方向梯度  
  129.                 float xg = (getPixel(outPixels, width, height, col, row+1) -   
  130.                         getPixel(outPixels, width, height, col, row) +   
  131.                         getPixel(outPixels, width, height, col+1, row+1) -  
  132.                         getPixel(outPixels, width, height, col+1, row))/2.0f;  
  133.                 float yg = (getPixel(outPixels, width, height, col, row)-  
  134.                         getPixel(outPixels, width, height, col+1, row) +  
  135.                         getPixel(outPixels, width, height, col, row+1) -  
  136.                         getPixel(outPixels, width, height, col+1, row+1))/2.0f;  
  137.                 // 计算振幅与角度  
  138.                 data[index] = hypot(xg, yg);  
  139.                 if(xg == 0)  
  140.                 {  
  141.                     if(yg > 0)  
  142.                     {  
  143.                         magnitudes[index]=90;                         
  144.                     }  
  145.                     if(yg < 0)  
  146.                     {  
  147.                         magnitudes[index]=-90;  
  148.                     }  
  149.                 }  
  150.                 else if(yg == 0)  
  151.                 {  
  152.                     magnitudes[index]=0;  
  153.                 }  
  154.                 else  
  155.                 {  
  156.                     magnitudes[index] = (float)((Math.atan(yg/xg) * 180)/Math.PI);                    
  157.                 }  
  158.                 // make it 0 ~ 180  
  159.                 magnitudes[index] += 90;  
  160.             }  
  161.         }  
  162.           
  163.         // 非最大信号压制算法 3x3  
  164.         Arrays.fill(magnitudes, 0);  
  165.         for (int row = 0; row < height; row++) {  
  166.             for (int col = 0; col < width; col++) {  
  167.                 index = row * width + col;  
  168.                 float angle = magnitudes[index];  
  169.                 float m0 = data[index];  
  170.                 magnitudes[index] = m0;  
  171.                 if(angle >=0 && angle < 22.5) // angle 0  
  172.                 {  
  173.                     float m1 = getPixel(data, width, height, col-1, row);  
  174.                     float m2 = getPixel(data, width, height, col+1, row);  
  175.                     if(m0 < m1 || m0 < m2)  
  176.                     {  
  177.                         magnitudes[index] = 0;  
  178.                     }  
  179.                 }  
  180.                 else if(angle >= 22.5 && angle < 67.5) // angle +45  
  181.                 {  
  182.                     float m1 = getPixel(data, width, height, col+1, row-1);  
  183.                     float m2 = getPixel(data, width, height, col-1, row+1);  
  184.                     if(m0 < m1 || m0 < m2)  
  185.                     {  
  186.                         magnitudes[index] = 0;  
  187.                     }  
  188.                 }  
  189.                 else if(angle >= 67.5 && angle < 112.5) // angle 90  
  190.                 {  
  191.                     float m1 = getPixel(data, width, height, col, row+1);  
  192.                     float m2 = getPixel(data, width, height, col, row-1);  
  193.                     if(m0 < m1 || m0 < m2)  
  194.                     {  
  195.                         magnitudes[index] = 0;  
  196.                     }  
  197.                 }  
  198.                 else if(angle >=112.5 && angle < 157.5) // angle 135 / -45  
  199.                 {  
  200.                     float m1 = getPixel(data, width, height, col-1, row-1);  
  201.                     float m2 = getPixel(data, width, height, col+1, row+1);  
  202.                     if(m0 < m1 || m0 < m2)  
  203.                     {  
  204.                         magnitudes[index] = 0;  
  205.                     }  
  206.                 }  
  207.                 else if(angle >=157.5) // angle 0  
  208.                 {  
  209.                     float m1 = getPixel(data, width, height, col, row+1);  
  210.                     float m2 = getPixel(data, width, height, col, row-1);  
  211.                     if(m0 < m1 || m0 < m2)  
  212.                     {  
  213.                         magnitudes[index] = 0;  
  214.                     }  
  215.                 }  
  216.             }  
  217.         }  
  218.         // 寻找最大与最小值  
  219.         float min = 255;  
  220.         float max = 0;  
  221.         for(int i=0; i<magnitudes.length; i++)  
  222.         {  
  223.             if(magnitudes[i] == 0) continue;  
  224.             min = Math.min(min, magnitudes[i]);  
  225.             max = Math.max(max, magnitudes[i]);  
  226.         }  
  227.         System.out.println("Image Max Gradient = " + max + " Mix Gradient = " + min);  
  228.   
  229.         // 通常比值为 TL : TH = 1 : 3, 根据两个阈值完成二值化边缘连接  
  230.         // 边缘连接-link edges  
  231.         Arrays.fill(data, 0);  
  232.         int offset = 0;  
  233.         for (int row = 0; row < height; row++) {  
  234.             for (int col = 0; col < width; col++) {  
  235.                 if(magnitudes[offset] >= highThreshold && data[offset] == 0)  
  236.                 {  
  237.                     edgeLink(col, row, offset, lowThreshold);  
  238.                 }  
  239.                 offset++;  
  240.             }  
  241.         }  
  242.           
  243.         // 二值化显示  
  244.         for(int i=0; i<inPixels.length; i++)  
  245.         {  
  246.             int gray = clamp((int)data[i]);  
  247.             outPixels[i] = gray > 0 ? -1 : 0xff000000;       
  248.         }  
  249.         setRGB(dest, 0, 0, width, height, outPixels );  
  250.         return dest;  
  251.     }  
  252.       
  253.     public int clamp(int value) {  
  254.         return value > 255 ? 255 :  
  255.             (value < 0 ? 0 : value);  
  256.     }  
  257.       
  258.     private void edgeLink(int x1, int y1, int index, float threshold) {  
  259.         int x0 = (x1 == 0) ? x1 : x1 - 1;  
  260.         int x2 = (x1 == width - 1) ? x1 : x1 + 1;  
  261.         int y0 = y1 == 0 ? y1 : y1 - 1;  
  262.         int y2 = y1 == height -1 ? y1 : y1 + 1;  
  263.           
  264.         data[index] = magnitudes[index];  
  265.         for (int x = x0; x <= x2; x++) {  
  266.             for (int y = y0; y <= y2; y++) {  
  267.                 int i2 = x + y * width;  
  268.                 if ((y != y1 || x != x1)  
  269.                     && data[i2] == 0   
  270.                     && magnitudes[i2] >= threshold) {  
  271.                     edgeLink(x, y, i2, threshold);  
  272.                     return;  
  273.                 }  
  274.             }  
  275.         }  
  276.     }  
  277.       
  278.     private float getPixel(float[] input, int width, int height, int col,  
  279.             int row) {  
  280.         if(col < 0 || col >= width)  
  281.             col = 0;  
  282.         if(row < 0 || row >= height)  
  283.             row = 0;  
  284.         int index = row * width + col;  
  285.         return input[index];  
  286.     }  
  287.       
  288.     private float hypot(float x, float y) {  
  289.         return (float) Math.hypot(x, y);  
  290.     }  
  291.       
  292.     private int getPixel(int[] inPixels, int width, int height, int col,  
  293.             int row) {  
  294.         if(col < 0 || col >= width)  
  295.             col = 0;  
  296.         if(row < 0 || row >= height)  
  297.             row = 0;  
  298.         int index = row * width + col;  
  299.         return inPixels[index];  
  300.     }  
  301.       
  302.     private float gaussian(float x, float y, float sigma) {  
  303.         float xDistance = x*x;  
  304.         float yDistance = y*y;  
  305.         float sigma22 = 2*sigma*sigma;  
  306.         float sigma22PI = (float)Math.PI * sigma22;  
  307.         return (float)Math.exp(-(xDistance + yDistance)/sigma22)/sigma22PI;  
  308.     }  
  309.   
  310. }  

转载请务必注明出自本博客-gloomyfish

原文地址:https://www.cnblogs.com/zkwarrior/p/4954382.html