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像素轉為灰度值的代碼如下:
- <span style="font-size:18px;">int gray = (int) (0.299 * tr + 0.587 * tg + 0.114 * tb);</span>
2. 對圖像進行高斯模糊
圖像高斯模糊時,首先要根據輸入參數確定高斯方差與窗口大小,這里我設置默認方
差值窗口大小為16x16,根據這兩個參數生成高斯卷積核算子的代碼如下:
- <span style="font-size:18px;"> float kernel[][] = new float[gaussianKernelWidth][gaussianKernelWidth];
- for(int x=0; x<gaussianKernelWidth; x++)
- {
- for(int y=0; y<gaussianKernelWidth; y++)
- {
- kernel[x][y] = gaussian(x, y, gaussianKernelRadius);
- }
- }</span>
獲取了高斯卷積算子之后,我們就可以對圖像高斯卷積模糊,關於高斯圖像模糊更詳
細的解釋可以參見這里:http://blog.csdn.net/jia20003/article/details/7234741實現
圖像高斯卷積模糊的代碼如下:
- <span style="font-size:18px;">// 高斯模糊 -灰度圖像
- int krr = (int)gaussianKernelRadius;
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- index = row * width + col;
- double weightSum = 0.0;
- double redSum = 0;
- for(int subRow=-krr; subRow<=krr; subRow++)
- {
- int nrow = row + subRow;
- if(nrow >= height || nrow < 0)
- {
- nrow = 0;
- }
- for(int subCol=-krr; subCol<=krr; subCol++)
- {
- int ncol = col + subCol;
- if(ncol >= width || ncol <=0)
- {
- ncol = 0;
- }
- int index2 = nrow * width + ncol;
- int tr1 = (inPixels[index2] >> 16) & 0xff;
- redSum += tr1*kernel[subRow+krr][subCol+krr];
- weightSum += kernel[subRow+krr][subCol+krr];
- }
- }
- int gray = (int)(redSum / weightSum);
- outPixels[index] = gray;
- }
- }</span>
3. 計算圖像X方向與Y方向梯度,根據梯度計算圖像邊緣幅值與角度大小
高斯模糊的目的主要為了整體降低圖像噪聲,目的是為了更准確計算圖像梯度及邊緣
幅值。計算圖像梯度可以選擇算子有Robot算子、Sobel算子、Prewitt算子等。關於
圖像梯度計算更多的解釋可以看這里:
http://blog.csdn.net/jia20003/article/details/7664777。
這里采用更加簡單明了的2x2的算子,其數學表達如下:

- <span style="font-size:18px;">// 計算梯度-gradient, X放與Y方向
- data = new float[width * height];
- magnitudes = new float[width * height];
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- index = row * width + col;
- // 計算X方向梯度
- float xg = (getPixel(outPixels, width, height, col, row+1) -
- getPixel(outPixels, width, height, col, row) +
- getPixel(outPixels, width, height, col+1, row+1) -
- getPixel(outPixels, width, height, col+1, row))/2.0f;
- float yg = (getPixel(outPixels, width, height, col, row)-
- getPixel(outPixels, width, height, col+1, row) +
- getPixel(outPixels, width, height, col, row+1) -
- getPixel(outPixels, width, height, col+1, row+1))/2.0f;
- // 計算振幅與角度
- data[index] = hypot(xg, yg);
- if(xg == 0)
- {
- if(yg > 0)
- {
- magnitudes[index]=90;
- }
- if(yg < 0)
- {
- magnitudes[index]=-90;
- }
- }
- else if(yg == 0)
- {
- magnitudes[index]=0;
- }
- else
- {
- magnitudes[index] = (float)((Math.atan(yg/xg) * 180)/Math.PI);
- }
- // make it 0 ~ 180
- magnitudes[index] += 90;
- }
- }</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
分別表示上述四個離散角度的取值范圍。得到角度之后,比較中心像素角度上相鄰
兩個像素,如果中心像素小於其中任意一個,則舍棄該邊緣像素點,否則保留。一
個簡單的例子如下:

- <span style="font-size:18px;">// 非最大信號壓制算法 3x3
- Arrays.fill(magnitudes, 0);
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- index = row * width + col;
- float angle = magnitudes[index];
- float m0 = data[index];
- magnitudes[index] = m0;
- if(angle >=0 && angle < 22.5) // angle 0
- {
- float m1 = getPixel(data, width, height, col-1, row);
- float m2 = getPixel(data, width, height, col+1, row);
- if(m0 < m1 || m0 < m2)
- {
- magnitudes[index] = 0;
- }
- }
- else if(angle >= 22.5 && angle < 67.5) // angle +45
- {
- float m1 = getPixel(data, width, height, col+1, row-1);
- float m2 = getPixel(data, width, height, col-1, row+1);
- if(m0 < m1 || m0 < m2)
- {
- magnitudes[index] = 0;
- }
- }
- else if(angle >= 67.5 && angle < 112.5) // angle 90
- {
- float m1 = getPixel(data, width, height, col, row+1);
- float m2 = getPixel(data, width, height, col, row-1);
- if(m0 < m1 || m0 < m2)
- {
- magnitudes[index] = 0;
- }
- }
- else if(angle >=112.5 && angle < 157.5) // angle 135 / -45
- {
- float m1 = getPixel(data, width, height, col-1, row-1);
- float m2 = getPixel(data, width, height, col+1, row+1);
- if(m0 < m1 || m0 < m2)
- {
- magnitudes[index] = 0;
- }
- }
- else if(angle >=157.5) // angle 0
- {
- float m1 = getPixel(data, width, height, col, row+1);
- float m2 = getPixel(data, width, height, col, row-1);
- if(m0 < m1 || m0 < m2)
- {
- magnitudes[index] = 0;
- }
- }
- }
- }</span>
1. 雙閾值邊緣連接
非最大信號壓制以后,輸出的幅值如果直接顯示結果可能會少量的非邊緣像素被包
含到結果中,所以要通過選取閾值進行取舍,傳統的基於一個閾值的方法如果選擇
的閾值較小起不到過濾非邊緣的作用,如果選擇的閾值過大容易丟失真正的圖像邊
緣,Canny提出基於雙閾值(Fuzzy threshold)方法很好的實現了邊緣選取,在實際
應用中雙閾值還有邊緣連接的作用。雙閾值選擇與邊緣連接方法通過假設兩個閾值
其中一個為高閾值TH另外一個為低閾值TL則有
a. 對於任意邊緣像素低於TL的則丟棄
b. 對於任意邊緣像素高於TH的則保留
c. 對於任意邊緣像素值在TL與TH之間的,如果能通過邊緣連接到一個像素大於
TH而且邊緣所有像素大於最小閾值TL的則保留,否則丟棄。代碼實現如下:
- <span style="font-size:18px;">Arrays.fill(data, 0);
- int offset = 0;
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- if(magnitudes[offset] >= highThreshold && data[offset] == 0)
- {
- edgeLink(col, row, offset, lowThreshold);
- }
- offset++;
- }
- }</span>
基於遞歸的邊緣尋找方法edgeLink的代碼如下:
- <span style="font-size:18px;">private void edgeLink(int x1, int y1, int index, float threshold) {
- int x0 = (x1 == 0) ? x1 : x1 - 1;
- int x2 = (x1 == width - 1) ? x1 : x1 + 1;
- int y0 = y1 == 0 ? y1 : y1 - 1;
- int y2 = y1 == height -1 ? y1 : y1 + 1;
- data[index] = magnitudes[index];
- for (int x = x0; x <= x2; x++) {
- for (int y = y0; y <= y2; y++) {
- int i2 = x + y * width;
- if ((y != y1 || x != x1)
- && data[i2] == 0
- && magnitudes[i2] >= threshold) {
- edgeLink(x, y, i2, threshold);
- return;
- }
- }
- }
- }</span>
6. 結果二值化顯示 - 不說啦,直接點,自己看吧,太簡單啦
- <span style="font-size:18px;">// 二值化顯示
- for(int i=0; i<inPixels.length; i++)
- {
- int gray = clamp((int)data[i]);
- outPixels[i] = gray > 0 ? -1 : 0xff000000;
- }</span>
最終運行結果:

四:完整的Canny算法源代碼
- package com.gloomyfish.filter.study;
- import java.awt.image.BufferedImage;
- import java.util.Arrays;
- public class CannyEdgeFilter extends AbstractBufferedImageOp {
- private float gaussianKernelRadius = 2f;
- private int gaussianKernelWidth = 16;
- private float lowThreshold;
- private float highThreshold;
- // image width, height
- private int width;
- private int height;
- private float[] data;
- private float[] magnitudes;
- public CannyEdgeFilter() {
- lowThreshold = 2.5f;
- highThreshold = 7.5f;
- gaussianKernelRadius = 2f;
- gaussianKernelWidth = 16;
- }
- public float getGaussianKernelRadius() {
- return gaussianKernelRadius;
- }
- public void setGaussianKernelRadius(float gaussianKernelRadius) {
- this.gaussianKernelRadius = gaussianKernelRadius;
- }
- public int getGaussianKernelWidth() {
- return gaussianKernelWidth;
- }
- public void setGaussianKernelWidth(int gaussianKernelWidth) {
- this.gaussianKernelWidth = gaussianKernelWidth;
- }
- public float getLowThreshold() {
- return lowThreshold;
- }
- public void setLowThreshold(float lowThreshold) {
- this.lowThreshold = lowThreshold;
- }
- public float getHighThreshold() {
- return highThreshold;
- }
- public void setHighThreshold(float highThreshold) {
- this.highThreshold = highThreshold;
- }
- @Override
- public BufferedImage filter(BufferedImage src, BufferedImage dest) {
- width = src.getWidth();
- height = src.getHeight();
- if (dest == null)
- dest = createCompatibleDestImage(src, null);
- // 圖像灰度化
- int[] inPixels = new int[width * height];
- int[] outPixels = new int[width * height];
- getRGB(src, 0, 0, width, height, inPixels);
- int index = 0;
- for (int row = 0; row < height; row++) {
- int ta = 0, tr = 0, tg = 0, tb = 0;
- for (int col = 0; col < width; col++) {
- index = row * width + col;
- ta = (inPixels[index] >> 24) & 0xff;
- tr = (inPixels[index] >> 16) & 0xff;
- tg = (inPixels[index] >> 8) & 0xff;
- tb = inPixels[index] & 0xff;
- int gray = (int) (0.299 * tr + 0.587 * tg + 0.114 * tb);
- inPixels[index] = (ta << 24) | (gray << 16) | (gray << 8)
- | gray;
- }
- }
- // 計算高斯卷積核
- float kernel[][] = new float[gaussianKernelWidth][gaussianKernelWidth];
- for(int x=0; x<gaussianKernelWidth; x++)
- {
- for(int y=0; y<gaussianKernelWidth; y++)
- {
- kernel[x][y] = gaussian(x, y, gaussianKernelRadius);
- }
- }
- // 高斯模糊 -灰度圖像
- int krr = (int)gaussianKernelRadius;
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- index = row * width + col;
- double weightSum = 0.0;
- double redSum = 0;
- for(int subRow=-krr; subRow<=krr; subRow++)
- {
- int nrow = row + subRow;
- if(nrow >= height || nrow < 0)
- {
- nrow = 0;
- }
- for(int subCol=-krr; subCol<=krr; subCol++)
- {
- int ncol = col + subCol;
- if(ncol >= width || ncol <=0)
- {
- ncol = 0;
- }
- int index2 = nrow * width + ncol;
- int tr1 = (inPixels[index2] >> 16) & 0xff;
- redSum += tr1*kernel[subRow+krr][subCol+krr];
- weightSum += kernel[subRow+krr][subCol+krr];
- }
- }
- int gray = (int)(redSum / weightSum);
- outPixels[index] = gray;
- }
- }
- // 計算梯度-gradient, X放與Y方向
- data = new float[width * height];
- magnitudes = new float[width * height];
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- index = row * width + col;
- // 計算X方向梯度
- float xg = (getPixel(outPixels, width, height, col, row+1) -
- getPixel(outPixels, width, height, col, row) +
- getPixel(outPixels, width, height, col+1, row+1) -
- getPixel(outPixels, width, height, col+1, row))/2.0f;
- float yg = (getPixel(outPixels, width, height, col, row)-
- getPixel(outPixels, width, height, col+1, row) +
- getPixel(outPixels, width, height, col, row+1) -
- getPixel(outPixels, width, height, col+1, row+1))/2.0f;
- // 計算振幅與角度
- data[index] = hypot(xg, yg);
- if(xg == 0)
- {
- if(yg > 0)
- {
- magnitudes[index]=90;
- }
- if(yg < 0)
- {
- magnitudes[index]=-90;
- }
- }
- else if(yg == 0)
- {
- magnitudes[index]=0;
- }
- else
- {
- magnitudes[index] = (float)((Math.atan(yg/xg) * 180)/Math.PI);
- }
- // make it 0 ~ 180
- magnitudes[index] += 90;
- }
- }
- // 非最大信號壓制算法 3x3
- Arrays.fill(magnitudes, 0);
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- index = row * width + col;
- float angle = magnitudes[index];
- float m0 = data[index];
- magnitudes[index] = m0;
- if(angle >=0 && angle < 22.5) // angle 0
- {
- float m1 = getPixel(data, width, height, col-1, row);
- float m2 = getPixel(data, width, height, col+1, row);
- if(m0 < m1 || m0 < m2)
- {
- magnitudes[index] = 0;
- }
- }
- else if(angle >= 22.5 && angle < 67.5) // angle +45
- {
- float m1 = getPixel(data, width, height, col+1, row-1);
- float m2 = getPixel(data, width, height, col-1, row+1);
- if(m0 < m1 || m0 < m2)
- {
- magnitudes[index] = 0;
- }
- }
- else if(angle >= 67.5 && angle < 112.5) // angle 90
- {
- float m1 = getPixel(data, width, height, col, row+1);
- float m2 = getPixel(data, width, height, col, row-1);
- if(m0 < m1 || m0 < m2)
- {
- magnitudes[index] = 0;
- }
- }
- else if(angle >=112.5 && angle < 157.5) // angle 135 / -45
- {
- float m1 = getPixel(data, width, height, col-1, row-1);
- float m2 = getPixel(data, width, height, col+1, row+1);
- if(m0 < m1 || m0 < m2)
- {
- magnitudes[index] = 0;
- }
- }
- else if(angle >=157.5) // angle 0
- {
- float m1 = getPixel(data, width, height, col, row+1);
- float m2 = getPixel(data, width, height, col, row-1);
- if(m0 < m1 || m0 < m2)
- {
- magnitudes[index] = 0;
- }
- }
- }
- }
- // 尋找最大與最小值
- float min = 255;
- float max = 0;
- for(int i=0; i<magnitudes.length; i++)
- {
- if(magnitudes[i] == 0) continue;
- min = Math.min(min, magnitudes[i]);
- max = Math.max(max, magnitudes[i]);
- }
- System.out.println("Image Max Gradient = " + max + " Mix Gradient = " + min);
- // 通常比值為 TL : TH = 1 : 3, 根據兩個閾值完成二值化邊緣連接
- // 邊緣連接-link edges
- Arrays.fill(data, 0);
- int offset = 0;
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- if(magnitudes[offset] >= highThreshold && data[offset] == 0)
- {
- edgeLink(col, row, offset, lowThreshold);
- }
- offset++;
- }
- }
- // 二值化顯示
- for(int i=0; i<inPixels.length; i++)
- {
- int gray = clamp((int)data[i]);
- outPixels[i] = gray > 0 ? -1 : 0xff000000;
- }
- setRGB(dest, 0, 0, width, height, outPixels );
- return dest;
- }
- public int clamp(int value) {
- return value > 255 ? 255 :
- (value < 0 ? 0 : value);
- }
- private void edgeLink(int x1, int y1, int index, float threshold) {
- int x0 = (x1 == 0) ? x1 : x1 - 1;
- int x2 = (x1 == width - 1) ? x1 : x1 + 1;
- int y0 = y1 == 0 ? y1 : y1 - 1;
- int y2 = y1 == height -1 ? y1 : y1 + 1;
- data[index] = magnitudes[index];
- for (int x = x0; x <= x2; x++) {
- for (int y = y0; y <= y2; y++) {
- int i2 = x + y * width;
- if ((y != y1 || x != x1)
- && data[i2] == 0
- && magnitudes[i2] >= threshold) {
- edgeLink(x, y, i2, threshold);
- return;
- }
- }
- }
- }
- private float getPixel(float[] input, int width, int height, int col,
- int row) {
- if(col < 0 || col >= width)
- col = 0;
- if(row < 0 || row >= height)
- row = 0;
- int index = row * width + col;
- return input[index];
- }
- private float hypot(float x, float y) {
- return (float) Math.hypot(x, y);
- }
- private int getPixel(int[] inPixels, int width, int height, int col,
- int row) {
- if(col < 0 || col >= width)
- col = 0;
- if(row < 0 || row >= height)
- row = 0;
- int index = row * width + col;
- return inPixels[index];
- }
- private float gaussian(float x, float y, float sigma) {
- float xDistance = x*x;
- float yDistance = y*y;
- float sigma22 = 2*sigma*sigma;
- float sigma22PI = (float)Math.PI * sigma22;
- return (float)Math.exp(-(xDistance + yDistance)/sigma22)/sigma22PI;
- }
- }
轉載請務必注明出自本博客-gloomyfish
