圖像處理之霍夫變換(直線檢測(cè)算法)
本文關(guān)鍵詞:霍夫變換,由筆耕文化傳播整理發(fā)布。
圖像處理之霍夫變換(直線檢測(cè)算法)
霍夫變換是圖像變換中的經(jīng)典手段之一,主要用來從圖像中分離出具有某種相同特征的幾何
形狀(如,直線,圓等)。霍夫變換尋找直線與圓的方法相比與其它方法可以更好的減少噪
聲干擾。經(jīng)典的霍夫變換常用來檢測(cè)直線,圓,,橢圓等。
霍夫變換算法思想:
以直線檢測(cè)為例,每個(gè)像素坐標(biāo)點(diǎn)經(jīng)過變換都變成都直線特質(zhì)有貢獻(xiàn)的統(tǒng)一度量,一個(gè)簡(jiǎn)單
的例子如下:一條直線在圖像中是一系列離散點(diǎn)的集合,通過一個(gè)直線的離散極坐標(biāo)公式,
可以表達(dá)出直線的離散點(diǎn)幾何等式如下:
X *cos(theta) + y * sin(theta) = r 其中角度theta指r與X軸之間的夾角,r為到直線幾何垂
直距離。任何在直線上點(diǎn),x, y都可以表達(dá),其中 r, theta是常量。該公式圖形表示如下:
然而在實(shí)現(xiàn)的圖像處理領(lǐng)域,圖像的像素坐標(biāo)P(x, y)是已知的,而r, theta則是我們要尋找
的變量。如果我們能繪制每個(gè)(r, theta)值根據(jù)像素點(diǎn)坐標(biāo)P(x, y)值的話,那么就從圖像笛卡
爾坐標(biāo)系統(tǒng)轉(zhuǎn)換到極坐標(biāo)霍夫空間系統(tǒng),這種從點(diǎn)到曲線的變換稱為直線的霍夫變換。變換
通過量化霍夫參數(shù)空間為有限個(gè)值間隔等分或者累加格子。當(dāng)霍夫變換算法開始,每個(gè)像素
坐標(biāo)點(diǎn)P(x, y)被轉(zhuǎn)換到(r, theta)的曲線點(diǎn)上面,累加到對(duì)應(yīng)的格子數(shù)據(jù)點(diǎn),當(dāng)一個(gè)波峰出現(xiàn)
時(shí)候,說明有直線存在。同樣的原理,我們可以用來檢測(cè)圓,只是對(duì)于圓的參數(shù)方程變?yōu)槿?/p>
下等式:
(x –a ) ^2 + (y-b) ^ 2 = r^2其中(a, b)為圓的中心點(diǎn)坐標(biāo),r圓的半徑。這樣霍夫的參數(shù)空間就
變成一個(gè)三維參數(shù)空間。給定圓半徑轉(zhuǎn)為二維霍夫參數(shù)空間,變換相對(duì)簡(jiǎn)單,也比較常用。
編程思路解析:
1. 讀取一幅帶處理二值圖像,最好背景為黑色。
2. 取得源像素?cái)?shù)據(jù)
3. 根據(jù)直線的霍夫變換公式完成霍夫變換,預(yù)覽霍夫空間結(jié)果
5. 越界處理,顯示霍夫變換處理以后的圖像
關(guān)鍵代碼解析:
直線的變換角度為[0 ~ PI]之間,設(shè)置等份為500為PI/500,同時(shí)根據(jù)參數(shù)直線參數(shù)方程的取值
范圍為[-r, r]有如下霍夫參數(shù)定義:
// prepare for hough transform int centerX = width / 2; int centerY = height / 2; double hough_interval = PI_VALUE/(double)hough_space; int max = Math.max(width, height); int max_length = (int)(Math.sqrt(2.0D) * max); hough_1d = new int[2 * hough_space * max_length];
// start hough transform now.... int[][] image_2d = convert1Dto2D(inPixels); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int p = image_2d[row][col] & 0xff; if(p == 0) continue; // which means background color // since we does not know the theta angle and r value, // we have to calculate all hough space for each pixel point // then we got the max possible theta and r pair. // r = x * cos(theta) + y * sin(theta) for(int cell=0; cell < hough_space; cell++ ) { max = (int)((col - centerX) * Math.cos(cell * hough_interval) + (row - centerY) * Math.sin(cell * hough_interval)); max += max_length; // start from zero, not (-max_length) if (max < 0 || (max >= 2 * max_length)) {// make sure r did not out of scope[0, 2*max_lenght] continue; } hough_2d[cell][max] +=1; } } }尋找最大霍夫值計(jì)算霍夫閾值的代碼如下:
// find the max hough value int max_hough = 0; for(int i=0; i<hough_space; i++) { for(int j=0; j<2*max_length; j++) { hough_1d[(i + j * hough_space)] = hough_2d[i][j]; if(hough_2d[i][j] > max_hough) { max_hough = hough_2d[i][j]; } } } System.out.println("MAX HOUGH VALUE = " + max_hough); // transfer back to image pixels space from hough parameter space int hough_threshold = (int)(threshold * max_hough);從霍夫空間反變換回像素?cái)?shù)據(jù)空間代碼如下:
// transfer back to image pixels space from hough parameter space int hough_threshold = (int)(threshold * max_hough); for(int row = 0; row < hough_space; row++) { for(int col = 0; col < 2*max_length; col++) { if(hough_2d[row][col] < hough_threshold) // discard it continue; int hough_value = hough_2d[row][col]; boolean isLine = true; for(int i=-1; i<2; i++) { for(int j=-1; j<2; j++) { if(i != 0 || j != 0) { int yf = row + i; int xf = col + j; if(xf < 0) continue; if(xf < 2*max_length) { if (yf < 0) { yf += hough_space; } if (yf >= hough_space) { yf -= hough_space; } if(hough_2d[yf][xf] <= hough_value) { continue; } isLine = false; break; } } } } if(!isLine) continue; // transform back to pixel data now... double dy = Math.sin(row * hough_interval); double dx = Math.cos(row * hough_interval); if ((row <= hough_space / 4) || (row >= 3 * hough_space / 4)) { for (int subrow = 0; subrow < height; ++subrow) { int subcol = (int)((col - max_length - ((subrow - centerY) * dy)) / dx) + centerX; if ((subcol < width) && (subcol >= 0)) { image_2d[subrow][subcol] = -16776961; } } } else { for (int subcol = 0; subcol < width; ++subcol) { int subrow = (int)((col - max_length - ((subcol - centerX) * dx)) / dy) + centerY; if ((subrow < height) && (subrow >= 0)) { image_2d[subrow][subcol] = -16776961; } } } } }霍夫變換源圖如下:霍夫變換以后,在霍夫空間顯示如下:(白色表示已經(jīng)找到直線信號(hào))
最終反變換回到像素空間效果如下:
一個(gè)更好的運(yùn)行監(jiān)測(cè)直線的結(jié)果(輸入為二值圖像):
完整的霍夫變換源代碼如下:
package com.gloomyfish.image.transform; import java.awt.image.BufferedImage; import com.process.blur.study.AbstractBufferedImageOp; public class HoughLineFilter extends AbstractBufferedImageOp { public final static double PI_VALUE = Math.PI; private int hough_space = 500; private int[] hough_1d; private int[][] hough_2d; private int width; private int height; private float threshold; private float scale; private float offset; public HoughLineFilter() { // default hough transform parameters // scale = 1.0f; // offset = 0.0f; threshold = 0.5f; scale = 1.0f; offset = 0.0f; } public void setHoughSpace(int space) { this.hough_space = space; } public float getThreshold() { return threshold; } public void setThreshold(float threshold) { this.threshold = threshold; } public float getScale() { return scale; } public void setScale(float scale) { this.scale = scale; } public float getOffset() { return offset; } public void setOffset(float offset) { this.offset = offset; } @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 ); houghTransform(inPixels, outPixels); setRGB( dest, 0, 0, width, height, outPixels ); return dest; } private void houghTransform(int[] inPixels, int[] outPixels) { // prepare for hough transform int centerX = width / 2; int centerY = height / 2; double hough_interval = PI_VALUE/(double)hough_space; int max = Math.max(width, height); int max_length = (int)(Math.sqrt(2.0D) * max); hough_1d = new int[2 * hough_space * max_length]; // define temp hough 2D array and initialize the hough 2D hough_2d = new int[hough_space][2*max_length]; for(int i=0; i<hough_space; i++) { for(int j=0; j<2*max_length; j++) { hough_2d[i][j] = 0; } } // start hough transform now.... int[][] image_2d = convert1Dto2D(inPixels); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int p = image_2d[row][col] & 0xff; if(p == 0) continue; // which means background color // since we does not know the theta angle and r value, // we have to calculate all hough space for each pixel point // then we got the max possible theta and r pair. // r = x * cos(theta) + y * sin(theta) for(int cell=0; cell < hough_space; cell++ ) { max = (int)((col - centerX) * Math.cos(cell * hough_interval) + (row - centerY) * Math.sin(cell * hough_interval)); max += max_length; // start from zero, not (-max_length) if (max < 0 || (max >= 2 * max_length)) {// make sure r did not out of scope[0, 2*max_lenght] continue; } hough_2d[cell][max] +=1; } } } // find the max hough value int max_hough = 0; for(int i=0; i<hough_space; i++) { for(int j=0; j<2*max_length; j++) { hough_1d[(i + j * hough_space)] = hough_2d[i][j]; if(hough_2d[i][j] > max_hough) { max_hough = hough_2d[i][j]; } } } System.out.println("MAX HOUGH VALUE = " + max_hough); // transfer back to image pixels space from hough parameter space int hough_threshold = (int)(threshold * max_hough); for(int row = 0; row < hough_space; row++) { for(int col = 0; col < 2*max_length; col++) { if(hough_2d[row][col] < hough_threshold) // discard it continue; int hough_value = hough_2d[row][col]; boolean isLine = true; for(int i=-1; i<2; i++) { for(int j=-1; j<2; j++) { if(i != 0 || j != 0) { int yf = row + i; int xf = col + j; if(xf < 0) continue; if(xf < 2*max_length) { if (yf < 0) { yf += hough_space; } if (yf >= hough_space) { yf -= hough_space; } if(hough_2d[yf][xf] <= hough_value) { continue; } isLine = false; break; } } } } if(!isLine) continue; // transform back to pixel data now... double dy = Math.sin(row * hough_interval); double dx = Math.cos(row * hough_interval); if ((row <= hough_space / 4) || (row >= 3 * hough_space / 4)) { for (int subrow = 0; subrow < height; ++subrow) { int subcol = (int)((col - max_length - ((subrow - centerY) * dy)) / dx) + centerX; if ((subcol < width) && (subcol >= 0)) { image_2d[subrow][subcol] = -16776961; } } } else { for (int subcol = 0; subcol < width; ++subcol) { int subrow = (int)((col - max_length - ((subcol - centerX) * dx)) / dy) + centerY; if ((subrow < height) && (subrow >= 0)) { image_2d[subrow][subcol] = -16776961; } } } } } // convert to hough 1D and return result for (int i = 0; i < this.hough_1d.length; i++) { int value = clamp((int)(scale * this.hough_1d[i] + offset)); // scale always equals 1 this.hough_1d[i] = (0xFF000000 | value + (value << 16) + (value << 8)); } // convert to image 1D and return for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { outPixels[(col + row * width)] = image_2d[row][col]; } } } public BufferedImage getHoughImage() { BufferedImage houghImage = new BufferedImage(hough_2d[0].length, hough_space, BufferedImage.TYPE_4BYTE_ABGR); setRGB(houghImage, 0, 0, hough_2d[0].length, hough_space, hough_1d); return houghImage; } public static int clamp(int value) { if (value < 0) value = 0; else if (value > 255) { value = 255; } return value; } private int[][] convert1Dto2D(int[] pixels) { int[][] image_2d = new int[height][width]; int index = 0; for(int row = 0; row < height; row++) { for(int col = 0; col < width; col++) { index = row * width + col; image_2d[row][col] = pixels[index]; } } return image_2d; } } 轉(zhuǎn)載文章請(qǐng)務(wù)必注明出自本博客!
本文關(guān)鍵詞:霍夫變換,由筆耕文化傳播整理發(fā)布。
本文編號(hào):67053
本文鏈接:http://sikaile.net/wenshubaike/shangbiaozhuanli/67053.html