圖像處理之Canny 邊緣檢測(cè) 一:歷史 Canny邊緣檢測(cè)算法是1986年有John F. Canny開(kāi)發(fā)出來(lái)一種基于圖像梯度計(jì)算的邊緣 檢測(cè)算法,同時(shí)Canny本人對(duì)計(jì)算圖像邊緣提取學(xué)科的發(fā)展也是做出了很多的貢獻(xiàn)。盡 管至今已經(jīng)許多年過(guò)去,但是該算法仍然是圖像邊緣檢測(cè)方法經(jīng)典算法之一。 二:Canny邊緣檢測(cè)算法 經(jīng)典的Canny邊緣檢測(cè)算法通常都是從高斯模糊開(kāi)始,到基于雙閾值實(shí)現(xiàn)邊緣連接結(jié)束 。但是在實(shí)際工程應(yīng)用中,考慮到輸入圖像都是彩色圖像,最終邊緣連接之后的圖像要 二值化輸出顯示,所以完整的Canny邊緣檢測(cè)算法實(shí)現(xiàn)步驟如下: 1. 彩色圖像轉(zhuǎn)換為灰度圖像 2. 對(duì)圖像進(jìn)行高斯模糊 3. 計(jì)算圖像梯度,根據(jù)梯度計(jì)算圖像邊緣幅值與角度 4. 非最大信號(hào)壓制處理(邊緣細(xì)化) 5. 雙閾值邊緣連接處理 6. 二值化圖像輸出結(jié)果 三:各步詳解與代碼實(shí)現(xiàn) 1. 彩色圖像轉(zhuǎn)灰度圖像 根據(jù)彩色圖像RGB轉(zhuǎn)灰度公式:gray = R * 0.299 + G * 0.587 + B * 0.114 將彩色圖像中每個(gè)RGB像素轉(zhuǎn)為灰度值的代碼如下: - <span style="font-size:18px;">int gray = (int) (0.299 * tr + 0.587 * tg + 0.114 * tb);</span>
2. 對(duì)圖像進(jìn)行高斯模糊 圖像高斯模糊時(shí),首先要根據(jù)輸入?yún)?shù)確定高斯方差與窗口大小,這里我設(shè)置默認(rèn)方 差值窗口大小為16x16,根據(jù)這兩個(gè)參數(shù)生成高斯卷積核算子的代碼如下: - <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>
獲取了高斯卷積算子之后,我們就可以對(duì)圖像高斯卷積模糊,關(guān)于高斯圖像模糊更詳 細(xì)的解釋可以參見(jiàn)這里:http://blog.csdn.net/jia20003/article/details/7234741實(shí)現(xiàn) 圖像高斯卷積模糊的代碼如下: - <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. 計(jì)算圖像X方向與Y方向梯度,根據(jù)梯度計(jì)算圖像邊緣幅值與角度大小 高斯模糊的目的主要為了整體降低圖像噪聲,目的是為了更準(zhǔn)確計(jì)算圖像梯度及邊緣 幅值。計(jì)算圖像梯度可以選擇算子有Robot算子、Sobel算子、Prewitt算子等。關(guān)于 圖像梯度計(jì)算更多的解釋可以看這里: http://blog.csdn.net/jia20003/article/details/7664777。 這里采用更加簡(jiǎn)單明了的2x2的算子,其數(shù)學(xué)表達(dá)如下:
- <span style="font-size:18px;">// 計(jì)算梯度-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;
- // 計(jì)算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;
- // 計(jì)算振幅與角度
- 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>
在獲取了圖像每個(gè)像素的邊緣幅值與角度之后 4. 非最大信號(hào)壓制 信號(hào)壓制本來(lái)是數(shù)字信號(hào)處理中經(jīng)常用的,這里的非最大信號(hào)壓制主要目的是實(shí)現(xiàn)邊 緣細(xì)化,通過(guò)該步處理邊緣像素進(jìn)一步減少。非最大信號(hào)壓制主要思想是假設(shè)3x3的 像素區(qū)域,中心像素P(x,y) 根據(jù)上一步中計(jì)算得到邊緣角度值angle,可以將角度分 為四個(gè)離散值0、45、90、135分類(lèi)依據(jù)如下:
其中黃色區(qū)域取值范圍為0~22.5 與157.5~180 綠色區(qū)域取值范圍為22.5 ~ 67.5 藍(lán)色區(qū)域取值范圍為67.5~112.5 紅色區(qū)域取值范圍為112.5~157.5 分別表示上述四個(gè)離散角度的取值范圍。得到角度之后,比較中心像素角度上相鄰 兩個(gè)像素,如果中心像素小于其中任意一個(gè),則舍棄該邊緣像素點(diǎn),否則保留。一 個(gè)簡(jiǎn)單的例子如下:
- <span style="font-size:18px;">// 非最大信號(hào)壓制算法 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) // 跟零度是一致的,感謝一位網(wǎng)友發(fā)現(xiàn)了這個(gè)問(wèn)題
- {
- 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;
- }
- }
- }
- }</span>
1. 雙閾值邊緣連接 非最大信號(hào)壓制以后,輸出的幅值如果直接顯示結(jié)果可能會(huì)少量的非邊緣像素被包 含到結(jié)果中,所以要通過(guò)選取閾值進(jìn)行取舍,傳統(tǒng)的基于一個(gè)閾值的方法如果選擇 的閾值較小起不到過(guò)濾非邊緣的作用,如果選擇的閾值過(guò)大容易丟失真正的圖像邊 緣,Canny提出基于雙閾值(Fuzzy threshold)方法很好的實(shí)現(xiàn)了邊緣選取,在實(shí)際 應(yīng)用中雙閾值還有邊緣連接的作用。雙閾值選擇與邊緣連接方法通過(guò)假設(shè)兩個(gè)閾值 其中一個(gè)為高閾值TH另外一個(gè)為低閾值TL則有 a. 對(duì)于任意邊緣像素低于TL的則丟棄 b. 對(duì)于任意邊緣像素高于TH的則保留 c. 對(duì)于任意邊緣像素值在TL與TH之間的,如果能通過(guò)邊緣連接到一個(gè)像素大于 TH而且邊緣所有像素大于最小閾值TL的則保留,否則丟棄。代碼實(shí)現(xiàn)如下: - <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. 結(jié)果二值化顯示 - 不說(shuō)啦,直接點(diǎn),自己看吧,太簡(jiǎn)單啦 - <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>
最終運(yùn)行結(jié)果: 四:完整的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;
- }
- }
-
- // 計(jì)算高斯卷積核
- 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;
- }
- }
-
- // 計(jì)算梯度-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;
- // 計(jì)算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;
- // 計(jì)算振幅與角度
- 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;
- }
- }
-
- // 非最大信號(hào)壓制算法 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, 根據(jù)兩個(gè)閾值完成二值化邊緣連接
- // 邊緣連接-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;
- }
-
- }
轉(zhuǎn)載請(qǐng)務(wù)必注明出自本博客-gloomyfish
|