OpenCV Feature Detection and Description -- Harris Corner Detection Harris角点检测
Posted mraaron
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV Feature Detection and Description -- Harris Corner Detection Harris角点检测相关的知识,希望对你有一定的参考价值。
原文链接 https://docs.opencv.org/4.1.2/dc/d0d/tutorial_py_features_harris.html
阅读文档学习opencv 如有问题,大家指出~~
Goal
In this chapter,
- We will understand the concepts behind Harris Corner Detection.
- We will see the functions: cv.cornerHarris(), cv.cornerSubPix()
目标:
(1)理解Harris角点检测
(2)知道这几个函数 cv.cornerHarris(), cv.cornerSubPix()
Theory
In last chapter, we saw that corners are regions in the image with large variation in intensity in all the directions. One early attempt to find these corners was done by Chris Harris & Mike Stephens in their paper A Combined Corner and Edge Detector in 1988, so now it is called Harris Corner Detector. He took this simple idea to a mathematical form. It basically finds the difference in intensity for a displacement of (u,v) in all directions. This is expressed as below:
理论:
在上一个章节中,我们知道了角点在每个方向上的变化都是巨大的。在早期就有Chris Harris 和 Mike Stephens 就做到了,在他们的论文(《A Combined Corner and Edge Detecor 》在1988年)中。所以现在的人把这样的角点叫做Harris角点检测。他把这个简单的想法变成了数学公式,它基本找到了所有方向上(u,v)位移的差异。表示如下:
Window function is either a rectangular window or gaussian window which gives weights to pixels underneath.
窗口函数可以是矩形窗口或高斯窗口,可以为其下方的像素赋予权重。
**备注:**
window function: 窗函数,在信号处理的过程中,我们经常使用FFT(傅立叶变换)来把信号从时域转换到频域。在转换之前,需要有一个函数来截取信号,这个函数我们经常使用的就是窗函数。
那么为什么要截取信号呢?因为信号不可能被无限的测量和运算。只能在一定周期下截取,用窗函数截取的信号,会对以后的FFT有帮助,减少频谱能量的泄漏。
**end**
We have to maximize this function E(u,v) for corner detection. That means, we have to maximize the second term. Applying Taylor Expansion to above equation and using some mathematical steps (please refer any standard text books you like for full derivation), we get the final equation as:
我们必须最大化此函数E(u,v)进行拐角检测。这意味着,我们必须最大化第二项。将泰勒展开式应用于上述方程式,并有一些数学步骤(请参考您喜欢的任何标准教科书以进行完整推导),得出的最终方程式为:
其中
Here, Ix and Iy are image derivatives in x and y directions respectively. (Can be easily found out using cv.Sobel()).
在此,Ix和Iy分别是在x和y方向上的图像导数。 (可以使用cv.Sobel()函数轻松找到)。
**备注:**
引用:https://blog.csdn.net/u011534057/article/details/77775974 https://blog.csdn.net/c337134154/article/details/50086547 这两个博客里面讲解了怎么推导的,实际上就是二元函数泰勒展开,取一介近似。怎么推导不是文章重点,不赘述
**end**
Then comes the main part. After this, they created a score, basically an equation, which will determine if a window can contain a corner or not.
然后是主要部分。此后,他们创建了一个分数,基本上是一个等式,它将确定一个窗口是否可以包含一个角。
其中
- det(M)=λ1λ2
- trace(M)=λ1+λ2
- λ1+λ2是矩阵M的特征值
So the values of these eigen values decide whether a region is corner, edge or flat.
- When |R| is small, which happens when λ1 and λ2 are small, the region is flat.
- When R<0, which happens when λ1>>λ2 or vice versa, the region is edge.
- When R is large, which happens when λ1 and λ2 are large and λ1∼λ2, the region is a corner.
根据λ1,λ2的值我们可以把其分为三类:
1.λ1,λ2都很小且近似,E在所以方向接近于常数;
2.λ1>>λ2,或者λ2>>λ1, E将在某一方向上很大;
3.λ1,λ2都很大且近似,E将在所有方向上很大;
It can be represented in a nice picture as follows:
下面有张很好理解的图片:
So the result of Harris Corner Detection is a grayscale image with these scores. Thresholding for a suitable give you the corners in the image. We will do it with a simple image.
因此,Harris Corner Detection的结果是具有这些值的灰度图像。合适的阈值可作为提供图像的角点。我们将用一个简单的图像来做到这一点。
下面就是怎么使用cv.cornerHarris的例子。不做翻译。
Corner with SubPixel Accuracy
Sometimes, you may need to find the corners with maximum accuracy. OpenCV comes with a function cv.cornerSubPix() which further refines the corners detected with sub-pixel accuracy. Below is an example. As usual, we need to find the harris corners first. Then we pass the centroids of these corners (There may be a bunch of pixels at a corner, we take their centroid) to refine them. Harris corners are marked in red pixels and refined corners are marked in green pixels. For this function, we have to define the criteria when to stop the iteration. We stop it after a specified number of iteration or a certain accuracy is achieved, whichever occurs first. We also need to define the size of neighbourhood it would search for corners.
亚像素精度的角点
有时,你可能需要以最大的精度去找角点。 OpenCV带有一个函数cv.cornerSubPix(),该函数进一步细化了像素以亚像素精度检测。下面是一个例子。一样的,我们需要先找到Harris corner。然后我们传递这些角的质心(在角处可能有一堆像素,我们取它们的质心)以细化它们。 Harris角用红色像素标记,精制角用绿色像素标记。对于此功能,我们必须定义何时停止迭代的条件。我们会在指定的迭代次数或达到一定的精度之后(以先到者为准)停止它。我们还需要定义搜索拐角的邻域的大小。
**备注:**
上面讲述了寻找角点的函数 cornerHarris()
用法上要注意些什么?单通道,灰度 ;在什么场合下使用? 我感觉是在灰度下有明显不一样的情况下使用,或者说为了使用这个函数,要进行一定的处理,制造出好的识别场景
**end**
以上是关于OpenCV Feature Detection and Description -- Harris Corner Detection Harris角点检测的主要内容,如果未能解决你的问题,请参考以下文章
OpenCV Feature Detection and Description -- Shi-Tomasi Corner Detector
综述:A survey of recent advances in visual feature detection
使用 CSS Capability/Feature Detection 检测 IE11
Feature Pyramid Networks for Object Detection(FPN)-论文阅读笔记
Joint Detection and Identification Feature Learning for Person Search