图像处理之角点检测算法(Harris Corner Detection)

简介: 图像处理之角点检测算法(Harris Corner Detection)

图像处理之角点检测算法(Harris Corner Detection)

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);
          }
        }
  }
 
}

最后注意:

我是把彩色图像变为灰度图像来计算,这个计算量小点

处理容易点,此外很多图像处理软件都会用标记来显示

关键点像素,我没有,只是将关键点像素改为绿色。

所以可以从这些方面有很大的提高空间。

相关文章
|
7天前
|
算法 JavaScript 前端开发
在JavaScript中实现基本的碰撞检测算法,我们通常会用到矩形碰撞检测,也就是AABB(Axis-Aligned Bounding Box)碰撞检测
【6月更文挑战第16天】JavaScript中的基本碰撞检测涉及AABB(轴对齐边界框)方法,常用于2D游戏。`Rectangle`类定义了矩形的属性,并包含一个`collidesWith`方法,通过比较边界来检测碰撞。若两矩形无重叠部分,四个条件(关于边界相对位置)均需满足。此基础算法适用于简单场景,复杂情况可能需采用更高级的检测技术或物理引擎库。
43 6
|
15天前
|
算法 计算机视觉
图像处理之积分图应用四(基于局部均值的图像二值化算法)
图像处理之积分图应用四(基于局部均值的图像二值化算法)
22 0
|
15天前
|
监控 算法 图计算
图像处理之积分图应用三(基于NCC快速相似度匹配算法)
图像处理之积分图应用三(基于NCC快速相似度匹配算法)
18 0
|
2天前
|
机器学习/深度学习 算法 语音技术
基于语音信号MFCC特征提取和GRNN神经网络的人员身份检测算法matlab仿真
**语音识别算法概览** MATLAB2022a中实现,结合MFCC与GRNN技术进行说话人身份检测。MFCC利用人耳感知特性提取语音频谱特征,GRNN作为非线性映射工具,擅长序列学习,确保高效识别。预加重、分帧、加窗、FFT、滤波器组、IDCT构成MFCC步骤,GRNN以其快速学习与鲁棒性处理不稳定数据。适用于多种领域。
|
3天前
|
机器学习/深度学习 算法 计算机视觉
基于ADAS的车道线检测算法matlab仿真
**摘要:** 基于ADAS的车道线检测算法利用Hough变换和边缘检测在视频中识别车道线,判断车道弯曲情况,提供行驶方向信息,并高亮显示。在MATLAB2022a中实现,系统包括图像预处理(灰度化、滤波、边缘检测)、车道线特征提取(霍夫变换、曲线拟合)和车道线跟踪,确保在实时场景中的准确性和稳定性。预处理通过灰度转换减少光照影响,滤波去除噪声,Canny算法检测边缘。霍夫变换用于直线检测,曲线拟合适应弯道,跟踪则增强连续帧的车道线检测。
|
10天前
|
机器学习/深度学习 监控 算法
基于yolov2深度学习网络的昆虫检测算法matlab仿真,并输出昆虫数量和大小判决
YOLOv2算法应用于昆虫检测,提供实时高效的方法识别和定位图像中的昆虫,提升检测精度。核心是统一检测网络,预测边界框和类别概率。通过预测框尺寸估算昆虫大小,适用于农业监控、生态研究等领域。在matlab2022A上运行,经过关键升级,如采用更优网络结构和损失函数,保证速度与精度。持续优化可增强对不同昆虫的检测能力。![image.png](https://ucc.alicdn.com/pic/developer-ecology/3tnl7rfrqv6tw_e760ff6682a3420cb4e24d1e48b10a2e.png)
|
15天前
|
资源调度 算法 计算机视觉
图像处理之积分图应用二(快速边缘保留滤波算法)
图像处理之积分图应用二(快速边缘保留滤波算法)
10 0
|
3天前
|
机器学习/深度学习 算法 数据可视化
m基于PSO-LSTM粒子群优化长短记忆网络的电力负荷数据预测算法matlab仿真
在MATLAB 2022a中,应用PSO优化的LSTM模型提升了电力负荷预测效果。优化前预测波动大,优化后预测更稳定。PSO借鉴群体智能,寻找LSTM超参数(如学习率、隐藏层大小)的最优组合,以最小化误差。LSTM通过门控机制处理序列数据。代码显示了模型训练、预测及误差可视化过程。经过优化,模型性能得到改善。
18 6
|
1天前
|
缓存 算法
基于机会网络编码(COPE)的卫星网络路由算法matlab仿真
**摘要:** 该程序实现了一个基于机会网络编码(COPE)的卫星网络路由算法,旨在提升无线网络的传输效率和吞吐量。在MATLAB2022a中测试,结果显示了不同数据流个数下的网络吞吐量。算法通过Dijkstra函数寻找路径,计算编码机会(Nab和Nx),并根据编码机会减少传输次数。当有编码机会时,中间节点执行编码和解码操作,优化传输路径。结果以图表形式展示,显示数据流与吞吐量的关系,并保存为`R0.mat`。COPE算法预测和利用编码机会,适应卫星网络的动态特性,提高数据传输的可靠性和效率。
|
3天前
|
算法 调度
基于变异混合蛙跳算法的车间调度最优化matlab仿真,可以任意调整工件数和机器数,输出甘特图
**摘要:** 实现变异混合蛙跳算法的MATLAB2022a版车间调度优化程序,支持动态调整工件和机器数,输出甘特图。核心算法结合SFLA与变异策略,解决Job-Shop Scheduling Problem,最小化总完成时间。SFLA模拟蛙群行为,分组进行局部搜索和全局信息交换。变异策略增强全局探索,避免局部最优。程序初始化随机解,按规则更新,经多次迭代和信息交换后终止。