图像处理之Harris角度检测算法
Harris角度检测是通过数学计算在图像上发现角度特征的一种算法,而且其具有旋转不
变性的特质。OpenCV中的Shi-Tomasi角度检测就是基于Harris角度检测改进算法。
基本原理:
角度是一幅图像上最明显与重要的特征,对于一阶导数而言,角度在各个方向的变化是
最大的,而边缘区域在只是某一方向有明显变化。一个直观的图示如下:
数学原理:
基本数学公式如下:
其中W(x, y)表示移动窗口,I(x, y)表示像素灰度值强度,范围为0~255。根据泰勒级数
计算一阶到N阶的偏导数,最终得到一个Harris矩阵公式:
根据Harris的矩阵计算矩阵特征值,然后计算Harris角度响应值:
其中K为系数值,通常取值范围为0.04 ~ 0.06之间。
算法详细步骤
第一步:计算图像X方向与Y方向的一阶高斯偏导数Ix与Iy
第二步:根据第一步结果得到Ix^2 , Iy^2与Ix*Iy值
第三步:高斯模糊第二步三个值得到Sxx, Syy, Sxy
第四部:定义每个像素的Harris矩阵,计算出矩阵的两个特质值
第五步:计算出每个像素的R值
第六步:使用3X3或者5X5的窗口,实现非最大值压制
第七步:根据角度检测结果计算,最提取到的关键点以绿色标记,显示在原图上。
程序关键代码解读:
第一步计算一阶高斯偏导数的Ix与Iy值代码如下:
filter.setDirectionType(GaussianDerivativeFilter.X_DIRECTION); BufferedImage xImage = filter.filter(grayImage, null); getRGB( xImage, 0, 0, width, height, inPixels ); extractPixelData(inPixels, GaussianDerivativeFilter.X_DIRECTION, height, width); filter.setDirectionType(GaussianDerivativeFilter.Y_DIRECTION); BufferedImage yImage = filter.filter(grayImage, null); getRGB( yImage, 0, 0, width, height, inPixels ); extractPixelData(inPixels, GaussianDerivativeFilter.Y_DIRECTION, height, width);
关于如何计算高斯一阶与二阶偏导数请看这里:
http://blog.csdn.net/jia20003/article/details/16369143
http://blog.csdn.net/jia20003/article/details/7664777
第三步:分别对第二步计算出来的三个值,单独进行高斯
模糊计算,代码如下:
private void calculateGaussianBlur(int width, int height) { int index = 0; int radius = (int)window_radius; double[][] gw = get2DKernalData(radius, sigma); double sumxx = 0, sumyy = 0, sumxy = 0; for(int row=0; row<height; row++) { for(int col=0; col<width; col++) { for(int subrow =-radius; subrow<=radius; subrow++) { for(int subcol=-radius; subcol<=radius; subcol++) { int nrow = row + subrow; int ncol = col + subcol; if(nrow >= height || nrow < 0) { nrow = 0; } if(ncol >= width || ncol < 0) { ncol = 0; } int index2 = nrow * width + ncol; HarrisMatrix whm = harrisMatrixList.get(index2); sumxx += (gw[subrow + radius][subcol + radius] * whm.getXGradient()); sumyy += (gw[subrow + radius][subcol + radius] * whm.getYGradient()); sumxy += (gw[subrow + radius][subcol + radius] * whm.getIxIy()); } } index = row * width + col; HarrisMatrix hm = harrisMatrixList.get(index); hm.setXGradient(sumxx); hm.setYGradient(sumyy); hm.setIxIy(sumxy); // clean up for next loop sumxx = 0; sumyy = 0; sumxy = 0; } } }
第六步:非最大信号压制(non-max value suppression)
这个在边源检测中是为了得到一个像素宽的边缘,在这里则
是为了得到准确的一个角点像素,去掉非角点值。代码如下:
/*** * we still use the 3*3 windows to complete the non-max response value suppression */ private void nonMaxValueSuppression(int width, int height) { int index = 0; int radius = (int)window_radius; for(int row=0; row<height; row++) { for(int col=0; col<width; col++) { index = row * width + col; HarrisMatrix hm = harrisMatrixList.get(index); double maxR = hm.getR(); boolean isMaxR = true; for(int subrow =-radius; subrow<=radius; subrow++) { for(int subcol=-radius; subcol<=radius; subcol++) { int nrow = row + subrow; int ncol = col + subcol; if(nrow >= height || nrow < 0) { nrow = 0; } if(ncol >= width || ncol < 0) { ncol = 0; } int index2 = nrow * width + ncol; HarrisMatrix hmr = harrisMatrixList.get(index2); if(hmr.getR() > maxR) { isMaxR = false; } } } if(isMaxR) { hm.setMax(maxR); } } } }
运行效果:
程序完整源代码:
package com.gloomyfish.image.harris.corner; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; import com.gloomyfish.filter.study.GrayFilter; public class HarrisCornerDetector extends GrayFilter { private GaussianDerivativeFilter filter; private List<HarrisMatrix> harrisMatrixList; private double lambda = 0.04; // scope : 0.04 ~ 0.06 // i hard code the window size just keep it' size is same as // first order derivation Gaussian window size private double sigma = 1; // always private double window_radius = 1; // always public HarrisCornerDetector() { filter = new GaussianDerivativeFilter(); harrisMatrixList = new ArrayList<HarrisMatrix>(); } @Override public BufferedImage filter(BufferedImage src, BufferedImage dest) { int width = src.getWidth(); int height = src.getHeight(); initSettings(height, width); if ( dest == null ) dest = createCompatibleDestImage( src, null ); BufferedImage grayImage = super.filter(src, null); int[] inPixels = new int[width*height]; // first step - Gaussian first-order Derivatives (3 × 3) - X - gradient, (3 × 3) - Y - gradient filter.setDirectionType(GaussianDerivativeFilter.X_DIRECTION); BufferedImage xImage = filter.filter(grayImage, null); getRGB( xImage, 0, 0, width, height, inPixels ); extractPixelData(inPixels, GaussianDerivativeFilter.X_DIRECTION, height, width); filter.setDirectionType(GaussianDerivativeFilter.Y_DIRECTION); BufferedImage yImage = filter.filter(grayImage, null); getRGB( yImage, 0, 0, width, height, inPixels ); extractPixelData(inPixels, GaussianDerivativeFilter.Y_DIRECTION, height, width); // second step - calculate the Ix^2, Iy^2 and Ix^Iy for(HarrisMatrix hm : harrisMatrixList) { double Ix = hm.getXGradient(); double Iy = hm.getYGradient(); hm.setIxIy(Ix * Iy); hm.setXGradient(Ix*Ix); hm.setYGradient(Iy*Iy); } // 基于高斯方法,中心点化窗口计算一阶导数和,关键一步 SumIx2, SumIy2 and SumIxIy, 高斯模糊 calculateGaussianBlur(width, height); // 求取Harris Matrix 特征值 // 计算角度相应值R R= Det(H) - lambda * (Trace(H))^2 harrisResponse(width, height); // based on R, compute non-max suppression nonMaxValueSuppression(width, height); // match result to original image and highlight the key points int[] outPixels = matchToImage(width, height, src); // return result image setRGB( dest, 0, 0, width, height, outPixels ); return dest; } private int[] matchToImage(int width, int height, BufferedImage src) { 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; HarrisMatrix hm = harrisMatrixList.get(index); if(hm.getMax() > 0) { tr = 0; tg = 255; // make it as green for corner key pointers tb = 0; outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb; } else { outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb; } } } return outPixels; } /*** * we still use the 3*3 windows to complete the non-max response value suppression */ private void nonMaxValueSuppression(int width, int height) { int index = 0; int radius = (int)window_radius; for(int row=0; row<height; row++) { for(int col=0; col<width; col++) { index = row * width + col; HarrisMatrix hm = harrisMatrixList.get(index); double maxR = hm.getR(); boolean isMaxR = true; for(int subrow =-radius; subrow<=radius; subrow++) { for(int subcol=-radius; subcol<=radius; subcol++) { int nrow = row + subrow; int ncol = col + subcol; if(nrow >= height || nrow < 0) { nrow = 0; } if(ncol >= width || ncol < 0) { ncol = 0; } int index2 = nrow * width + ncol; HarrisMatrix hmr = harrisMatrixList.get(index2); if(hmr.getR() > maxR) { isMaxR = false; } } } if(isMaxR) { hm.setMax(maxR); } } } } /*** * 计算两个特征值,然后得到R,公式如下,可以自己推导,关于怎么计算矩阵特征值,请看这里: * http://www.sosmath.com/matrix/eigen1/eigen1.html * * A = Sxx; * B = Syy; * C = Sxy*Sxy*4; * lambda = 0.04; * H = (A*B - C) - lambda*(A+B)^2; * * @param width * @param height */ private void harrisResponse(int width, int height) { int index = 0; for(int row=0; row<height; row++) { for(int col=0; col<width; col++) { index = row * width + col; HarrisMatrix hm = harrisMatrixList.get(index); double c = hm.getIxIy() * hm.getIxIy(); double ab = hm.getXGradient() * hm.getYGradient(); double aplusb = hm.getXGradient() + hm.getYGradient(); double response = (ab -c) - lambda * Math.pow(aplusb, 2); hm.setR(response); } } } private void calculateGaussianBlur(int width, int height) { int index = 0; int radius = (int)window_radius; double[][] gw = get2DKernalData(radius, sigma); double sumxx = 0, sumyy = 0, sumxy = 0; for(int row=0; row<height; row++) { for(int col=0; col<width; col++) { for(int subrow =-radius; subrow<=radius; subrow++) { for(int subcol=-radius; subcol<=radius; subcol++) { int nrow = row + subrow; int ncol = col + subcol; if(nrow >= height || nrow < 0) { nrow = 0; } if(ncol >= width || ncol < 0) { ncol = 0; } int index2 = nrow * width + ncol; HarrisMatrix whm = harrisMatrixList.get(index2); sumxx += (gw[subrow + radius][subcol + radius] * whm.getXGradient()); sumyy += (gw[subrow + radius][subcol + radius] * whm.getYGradient()); sumxy += (gw[subrow + radius][subcol + radius] * whm.getIxIy()); } } index = row * width + col; HarrisMatrix hm = harrisMatrixList.get(index); hm.setXGradient(sumxx); hm.setYGradient(sumyy); hm.setIxIy(sumxy); // clean up for next loop sumxx = 0; sumyy = 0; sumxy = 0; } } } public double[][] get2DKernalData(int n, double sigma) { int size = 2*n +1; double sigma22 = 2*sigma*sigma; double sigma22PI = Math.PI * sigma22; double[][] kernalData = new double[size][size]; int row = 0; for(int i=-n; i<=n; i++) { int column = 0; for(int j=-n; j<=n; j++) { double xDistance = i*i; double yDistance = j*j; kernalData[row][column] = Math.exp(-(xDistance + yDistance)/sigma22)/sigma22PI; column++; } row++; } // for(int i=0; i<size; i++) { // for(int j=0; j<size; j++) { // System.out.print("\t" + kernalData[i][j]); // } // System.out.println(); // System.out.println("\t ---------------------------"); // } return kernalData; } private void extractPixelData(int[] pixels, int type, int height, int width) { 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 = (pixels[index] >> 24) & 0xff; tr = (pixels[index] >> 16) & 0xff; tg = (pixels[index] >> 8) & 0xff; tb = pixels[index] & 0xff; HarrisMatrix matrix = harrisMatrixList.get(index); if(type == GaussianDerivativeFilter.X_DIRECTION) { matrix.setXGradient(tr); } if(type == GaussianDerivativeFilter.Y_DIRECTION) { matrix.setYGradient(tr); } } } } private void initSettings(int height, int width) { int index = 0; for(int row=0; row<height; row++) { for(int col=0; col<width; col++) { index = row * width + col; HarrisMatrix matrix = new HarrisMatrix(); harrisMatrixList.add(index, matrix); } } } }
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
持证人简介:贺渲雯 ,CDA 数据分析师一级持证人,互联网行业数据分析师 今天我将为大家带来一个关于用户私域用户质量数据分析 ...
2025-04-18一、CDA持证人介绍 在数字化浪潮席卷商业领域的当下,数据分析已成为企业发展的关键驱动力。为助力大家深入了解数据分析在电商行 ...
2025-04-17CDA持证人简介:居瑜 ,CDA一级持证人,国企财务经理,13年财务管理运营经验,在数据分析实践方面积累了丰富的行业经验。 一、 ...
2025-04-16持证人简介: CDA持证人刘凌峰,CDA L1持证人,微软认证讲师(MCT)金山办公最有价值专家(KVP),工信部高级项目管理师,拥有 ...
2025-04-15持证人简介:CDA持证人黄葛英,ICF国际教练联盟认证教练,前字节跳动销售主管,拥有丰富的行业经验。在实际生活中,我们可能会 ...
2025-04-14在 Python 编程学习与实践中,Anaconda 是一款极为重要的工具。它作为一个开源的 Python 发行版本,集成了众多常用的科学计算库 ...
2025-04-14随着大数据时代的深入发展,数据运营成为企业不可或缺的岗位之一。这个职位的核心是通过收集、整理和分析数据,帮助企业做出科 ...
2025-04-11持证人简介:CDA持证人黄葛英,ICF国际教练联盟认证教练,前字节跳动销售主管,拥有丰富的行业经验。 本次分享我将以教培行业为 ...
2025-04-11近日《2025中国城市长租市场发展蓝皮书》(下称《蓝皮书》)正式发布。《蓝皮书》指出,当前我国城市住房正经历从“增量扩张”向 ...
2025-04-10在数字化时代的浪潮中,数据已经成为企业决策和运营的核心。每一位客户,每一次交易,都承载着丰富的信息和价值。 如何在海量客 ...
2025-04-09数据是数字化的基础。随着工业4.0的推进,企业生产运作过程中的在线数据变得更加丰富;而互联网、新零售等C端应用的丰富多彩,产 ...
2025-04-094月7日,美国关税政策对全球金融市场的冲击仍在肆虐,周一亚市早盘,美股股指、原油期货、加密货币、贵金属等资产齐齐重挫,市场 ...
2025-04-08背景 3月26日,科技圈迎来一则重磅消息,苹果公司宣布向浙江大学捐赠 3000 万元人民币,用于支持编程教育。 这一举措并非偶然, ...
2025-04-07在当今数据驱动的时代,数据分析能力备受青睐,数据分析能力频繁出现在岗位需求的描述中,不分岗位的任职要求中,会特意标出“熟 ...
2025-04-03在当今数字化时代,数据分析师的重要性与日俱增。但许多人在踏上这条职业道路时,往往充满疑惑: 如何成为一名数据分析师?成为 ...
2025-04-02最近我发现一个绝招,用DeepSeek AI处理Excel数据简直太爽了!处理速度嘎嘎快! 平常一整天的表格处理工作,现在只要三步就能搞 ...
2025-04-01你是否被统计学复杂的理论和晦涩的公式劝退过?别担心,“山有木兮:统计学极简入门(Python)” 将为你一一化解这些难题。课程 ...
2025-03-31在电商、零售、甚至内容付费业务中,你真的了解你的客户吗? 有些客户下了一两次单就消失了,有些人每个月都回购,有些人曾经是 ...
2025-03-31在数字化浪潮中,数据驱动决策已成为企业发展的核心竞争力,数据分析人才的需求持续飙升。世界经济论坛发布的《未来就业报告》, ...
2025-03-28你有没有遇到过这样的情况?流量进来了,转化率却不高,辛辛苦苦拉来的用户,最后大部分都悄无声息地离开了,这时候漏斗分析就非 ...
2025-03-27