Harris 角点检测算法
1. 角点
角点是水平方向、垂直方向变化都很大的像素。
角点检测算法的基本思想:
使用一个固定窗口在图像上进行任意方向上的滑动,比较滑动前与滑动后两种情况,窗口中的像素灰度变化程度,如果存在任意方向上的滑动,都有着较大灰度变化,那么我们可以认为该窗口中存在角点。
目前,角点检测算法还不是十分完善,许多算法需要依赖大量的训练集和冗余数据来防止和减少错误的特征的出现。对于角点检测算法的重要评价标准是:其对多幅图像中相同或者相似特征的检测能力,并且能够应对光照变化、或者图像旋转等影响。
关于角点的具体描述可以有几种:
- 一阶导数(即灰度的梯度)的局部最大所对应的像素点;
- 两条及两条以上边缘的交点;
- 图像中梯度值和梯度方向的变化速率都很高的点;
- 角点处的一阶导数最大,二阶导数为零,指示物体边缘变化不连续的方向。
三类角点检测算法:
- 基于二值图像的角点检测;
- 基于轮廓曲线的角点检测;
- 基于灰度图像的角点检测:基于梯度、基于模板和基于模板和梯度组合三类方法;常见的基于模板的角点检测算法有:Kitchen-Rosenfeld角点检测算法,Harris角点检测算法,KLT角点检测算法及SUSAN角点检测算法。基于模板的方法主要是考虑像素领域点灰度的变化,即亮度的变化。
2. 流程
(1)找出角点
用高斯算子求出像素水平方向和垂直方向的梯度dx, dy,–> 对梯度的平方dxdx ,dydy, dxdy滤波得到Wxx ,Wxy,Wyy --> 在求的(WxxWyy - Wxy**2)/(Wxx + Wyy)作为候选角点。
(2)筛选角点
根据阈值筛选角点–> 取得角点的坐标 -->根据角点坐标得到角点所在的行 --> 在角点周围,删除掉其他角点。–> result
(3)标记角点
3. 实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
from PIL import Image import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import filters ## 1.找出角点 ### 1.1 步骤:img求导 --> imx ,imy 滤波-->Wxx ,Wxy,Wyy --> (Wxx*Wyy - Wxy**2)/(Wxx + Wyy) def compute_harris_response(img,sigma = 3 ): # 求梯度 imgx,imgy = np.zeros(img.shape),np.zeros(img.shape) filters.gaussian_filter(img,(sigma,sigma),( 0 , 1 ),imgx) filters.gaussian_filter(img,(sigma,sigma),( 1 , 0 ),imgy) # [260,263] # 对梯度进行高斯滤波 wxx = filters.gaussian_filter(imgx * * 2 ,sigma) wyy = filters.gaussian_filter(imgy * * 2 ,sigma) # [260,263] wxy = filters.gaussian_filter(imgx * imgy,sigma) # [260,263] ## 求行列式和迹 wdet = wxx * wyy - wxy * * 2 wtr = wxx + wyy return wdet / wtr ## 2 筛选角点 ### 2.1 步骤:根据阈值筛选角点--> 取得角点的坐标 -->根据角点坐标得到角点所在的行 --> # --> 在角点周围,删除掉其他角点 def get_harris_points(harri,min_dist = 4 ,threshold = 0.1 ): corner_thre = harri. max () * threshold # 角点阈值 mask = (harri > corner_thre) * 1 # 取出大于阈值的点为候选角点 cords = np.array(mask.nonzero()).T # 取候选角点的坐标 values = [harri[i[ 0 ],i[ 1 ]] for i in cords] # 候选角点的值 cls = np.argsort(values) # 对角点排序得到排序后的序列号,序列号也是候选角点所在的行 loc = np.zeros(harri.shape) # 划出可行性区域 loc[min_dist: - min_dist,min_dist: - min_dist] = 1 re_cords = [] for i in cls : # 筛选角点。先取出角点,角点周围的点不再取出 if loc[cords[i, 0 ],cords[i, 1 ]] = = 1 : re_cords.append(cords[i]) loc[cords[i, 0 ] - min_dist:cords[i, 0 ] + min_dist,cords[i, 1 ] - min_dist:cords[i, 1 ] + min_dist] = 0 return re_cords def plot_harri(img,cords): plt.figure() plt.gray() plt.imshow(img) plt.plot([i[ 1 ] for i in cords],[i[ 0 ] for i in cords], '.' ) plt.axis( 'off' ) plt.show() ## 3 测试 if __name__ = = '__main__' : img = np.array(Image. open ( 'luna.png' ).convert( 'L' )) harri = compute_harris_response(img) re_cords = get_harris_points(harri) plot_harri(img,re_cords) |
到此这篇关于OpenCV角点检测的文章就介绍到这了,更多相关OpenCV角点检测内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_35732321/article/details/123717928