本文将详细介绍角点检测的算法内容。目前的角点检测算法可归纳为3类:基于灰度图像的角点检测、基于二值图像的角点检测、基于轮廓曲线的角点检测。基于灰度图像的角点检测又可分为基于梯度、基于模板和基于模板梯度组合3类方法,其中基于模板的方法主要考虑像素领域点的灰度变化,即图像亮度的变化,将与邻点亮度对比足够大的点定义为角点。本文将介绍一种改进的Harris角点检测算法,该算法是一种基于模板与梯度组合的方法。
算法过程如下:
1 使用梯度模板卷积求取梯度矩阵
对于灰度图像上的每一点,我们计算其在x,y方向上的一阶导数以及两者乘积,得到梯度图像矩阵Ix,Iy,Ixy。所使用模板如下:
Fig.1梯度模板
2 对于梯度矩阵Ix,Iy,Ixy分别进行高斯滤波,这里使用3*3的高斯模板进行滤波,模板如下:
Fig.2高斯模板
3 计算图像中每个象素对应的角点响应函数值,得到响应矩阵,公式如下:
4 设置CRF 的门限, 对提取的角点个数进行限制。局部极值点的数目往往很多, 通过设置CRF 的门限, 根据实际需要提取一定数量的最优点作为最后的结果。在矩阵cim中, 同时满足“cim 大于阈值thresh 和cim 是某邻域内的局部极大值” 这两个条件的点被认为是角点。提高阈值, 则提取的角点数目变少; 降低阈值, 则提取的角点数目变多。
- <span style=< span="" style="word-wrap: normal;">"font-size:14px;">[函数代码]
- ///
- /// Harris counter-detect.
- ///
- /// The source image.
- /// The threshould to control counters number.
- ///
- public static int[,] HarrisDetect(WriteableBitmap src, int CRF)
- {
- int x = src.PixelWidth;
- int y = src.PixelHeight;
- double[,] Ix = new double[x, y];
- double[,] Iy = new double[x, y];
- double[,] Ixy = new double[x, y];
- double[,] cim = new double[x, y];
- int[,] re = new int[x, y];
- double[,] srcBytes = GetImageBytes(src);
- GetIV(srcBytes, Ix, Iy, Ixy,x,y);
- GaussFilter(Ix, Iy, Ixy,x,y);
- cim = GetCim(Ix, Iy, Ixy,x,y);
- for (int j = 1; j < y - 1; j++)
- {
- for (int i = 1; i < x - 1; i++)
- {
- if ((cim[i, j] == GetMax(cim[i - 1, j - 1], cim[i, j - 1], cim[i + 1, j - 1], cim[i - 1, j], cim[i, j], cim[i + 1, j], cim[i - 1, j + 1], cim[i, j + 1], cim[i + 1, j + 1])) && (cim[i, j] > CRF))
- {
- re[i, j] = 1;
- }
- }
- }
- return re;
- }
- //获得角点图像的 原始 信息
- public static double[,] GetImageBytes(WriteableBitmap src)
- {
- if (src != null)
- {
- int w = src.PixelWidth;
- int h = src.PixelHeight;
- double[,] imageBytes = new double[w, h];
- int b = 0, g = 0, r = 0;
- byte[] temp = src.PixelBuffer.ToArray();
- for (int y = 0; y < h; y++)
- {
- for (int x = 0; x < w * 4; x += 4)
- {
- b = temp[x + y * w * 4];
- g = temp[x + 1 + y * w * 4];
- r = temp[x + 2 + y * w * 4];
- imageBytes[x, y] = (b * 0.114 + g * 0.587 + r * 0.299);
- }
- }
- return imageBytes;
- }
- else
- {
- return null;
- }
- }
- //梯度求取函数
- private static void GetIV(double[,] src, double[,] Ix, double[,] Iy, double[,] Ixy,int x,int y)
- {
- for (int j = 1; j < y - 1; j++)
- {
- for (int i = 1; i < x - 1; i++)
- {
- Ix[i, j] = Math.Abs(src[i + 1, j - 1] + src[i + 1, j] + src[i + 1, j + 1] - src[i - 1, j - 1] - src[i - 1, j] - src[i - 1, j + 1]);
- Iy[i, j] = Math.Abs(src[i - 1, j + 1] + src[i, j + 1] + src[i + 1, j + 1] - src[i - 1, j - 1] - src[i, j - 1] - src[i + 1, j - 1]);
- Ixy[i, j] = Math.Abs(Ix[i, j] * Iy[i, j]);
- }
- }
- }
- //高斯滤波函数(对梯度图像进行高斯滤波,这里采用的是3*3的高斯滤波模板)
- private static void GaussFilter(double[,] Ix, double[,] Iy, double[,] Ixy,int x,int y)
- {
- for (int j = 1; j < y - 1; j++)
- {
- for (int i = 1; i < x - 1; i++)
- {
- Ix[i, j] = (Ix[i - 1, j - 1] + Ix[i, j - 1] * 2 + Ix[i + 1, j - 1] + 2 * Ix[i - 1, j] + 4 * Ix[i, j] + 2 * Ix[i + 1, j] + Ix[i - 1, j + 1] + 2 * Ix[i, j + 1] + Ix[i + 1, j + 1]) / 16;
- Iy[i, j] = (Iy[i - 1, j - 1] + Iy[i, j - 1] * 2 + Iy[i + 1, j - 1] + 2 * Iy[i - 1, j] + 4 * Iy[i, j] + 2 * Iy[i + 1, j] + Iy[i - 1, j + 1] + 2 * Iy[i, j + 1] + Ix[i + 1, j + 1]) / 16;
- Ixy[i, j] = (Ixy[i - 1, j - 1] + Ixy[i, j - 1] * 2 + Ixy[i + 1, j - 1] + 2 * Ixy[i - 1, j] + 4 * Ixy[i, j] + 2 * Ixy[i + 1, j] + Ixy[i - 1, j + 1] + 2 * Ixy[i, j + 1] + Ix[i + 1, j + 1]) / 16;
- }
- }
- }
- //图像角点求取函数
- private static double[,] GetCim(double[,] Ix, double[,] Iy, double[,] Ixy,int x,int y)
- {
- double cim = 0;
- double[,] results = new double[x, y];
- for (int j = 1; j < y - 1; j++)
- {
- for (int i = 1; i < x - 1; i++)
- {
- if (Ix[i, j] != 0 || Iy[i, j] != 0)
- {
- cim = Math.Abs(Ix[i, j] * Iy[i, j] - Ixy[i, j] * Ixy[i, j]) / (Ix[i, j] * Ix[i, j] + Iy[i, j] * Iy[i, j]);
- results[i, j] = cim;
- }
- }
- }
- return results;
- }
- //最大值获取函数
- private static double GetMax(params double[] src)
- {
- double tMax = 0;
- for (int i = 0; i < src.Length; i++)
- {
- if (tMax < src[i])
- {
- tMax = src[i];
- }
- }
- return tMax;
- }
- <span style="font-size:14px;">[图像效果]
demo: http://www.zealfilter.com/forum.php?mod=viewthread&tid=27&extra=page%3D2