1 Harris corner detector {#tutorial_harris_detector}
4 @next_tutorial{tutorial_good_features_to_track}
9 In this tutorial you will learn:
11 - What features are and why they are important
12 - Use the function @ref cv::cornerHarris to detect corners using the Harris-Stephens method.
17 ### What is a feature?
19 - In computer vision, usually we need to find matching points between different frames of an
20 environment. Why? If we know how two images relate to each other, we can use *both* images to
21 extract information of them.
22 - When we say **matching points** we are referring, in a general sense, to *characteristics* in
23 the scene that we can recognize easily. We call these characteristics **features**.
24 - **So, what characteristics should a feature have?**
25 - It must be *uniquely recognizable*
27 ### Types of Image Features
32 - **Corners** (also known as interest points)
33 - Blobs (also known as regions of interest )
35 In this tutorial we will study the *corner* features, specifically.
37 ### Why is a corner so special?
39 - Because, since it is the intersection of two edges, it represents a point in which the
40 directions of these two edges *change*. Hence, the gradient of the image (in both directions)
41 have a high variation, which can be used to detect it.
45 - Let's look for corners. Since corners represents a variation in the gradient in the image, we
46 will look for this "variation".
47 - Consider a grayscale image \f$I\f$. We are going to sweep a window \f$w(x,y)\f$ (with displacements \f$u\f$
48 in the x direction and \f$v\f$ in the y direction) \f$I\f$ and will calculate the variation of
51 \f[E(u,v) = \sum _{x,y} w(x,y)[ I(x+u,y+v) - I(x,y)]^{2}\f]
55 - \f$w(x,y)\f$ is the window at position \f$(x,y)\f$
56 - \f$I(x,y)\f$ is the intensity at \f$(x,y)\f$
57 - \f$I(x+u,y+v)\f$ is the intensity at the moved window \f$(x+u,y+v)\f$
58 - Since we are looking for windows with corners, we are looking for windows with a large variation
59 in intensity. Hence, we have to maximize the equation above, specifically the term:
61 \f[\sum _{x,y}[ I(x+u,y+v) - I(x,y)]^{2}\f]
63 - Using *Taylor expansion*:
65 \f[E(u,v) \approx \sum _{x,y}[ I(x,y) + u I_{x} + vI_{y} - I(x,y)]^{2}\f]
67 - Expanding the equation and cancelling properly:
69 \f[E(u,v) \approx \sum _{x,y} u^{2}I_{x}^{2} + 2uvI_{x}I_{y} + v^{2}I_{y}^{2}\f]
71 - Which can be expressed in a matrix form as:
73 \f[E(u,v) \approx \begin{bmatrix}
77 \displaystyle \sum_{x,y}
80 I_x^{2} & I_{x}I_{y} \\
91 \f[M = \displaystyle \sum_{x,y}
94 I_x^{2} & I_{x}I_{y} \\
98 - So, our equation now is:
100 \f[E(u,v) \approx \begin{bmatrix}
109 - A score is calculated for each window, to determine if it can possibly contain a corner:
111 \f[R = det(M) - k(trace(M))^{2}\f]
115 - det(M) = \f$\lambda_{1}\lambda_{2}\f$
116 - trace(M) = \f$\lambda_{1}+\lambda_{2}\f$
118 a window with a score \f$R\f$ greater than a certain value is considered a "corner"
124 This tutorial code's is shown lines below. You can also download it from
125 [here](https://github.com/opencv/opencv/tree/3.4/samples/cpp/tutorial_code/TrackingMotion/cornerHarris_Demo.cpp)
126 @include samples/cpp/tutorial_code/TrackingMotion/cornerHarris_Demo.cpp
130 This tutorial code's is shown lines below. You can also download it from
131 [here](https://github.com/opencv/opencv/tree/3.4/samples/java/tutorial_code/TrackingMotion/harris_detector/CornerHarrisDemo.java)
132 @include samples/java/tutorial_code/TrackingMotion/harris_detector/CornerHarrisDemo.java
136 This tutorial code's is shown lines below. You can also download it from
137 [here](https://github.com/opencv/opencv/tree/3.4/samples/python/tutorial_code/TrackingMotion/harris_detector/cornerHarris_Demo.py)
138 @include samples/python/tutorial_code/TrackingMotion/harris_detector/cornerHarris_Demo.py
149 ![](images/Harris_Detector_Original_Image.jpg)
151 The detected corners are surrounded by a small black circle
153 ![](images/Harris_Detector_Result.jpg)