Author: Job van Dieten < >

Maintainer: Jordi Pages < >



(!) Please ask about problems and questions regarding this tutorial on Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Corner Detection (C++)

Description: There are two corner detector algorithms often used in the OpenCV library, the Shi-Tomasi and Harris functions. In this simple tutorial you will see how changing two parameters can affect the corner detection

Keywords: OpenCV

Tutorial Level: BEGINNER

Next Tutorial: Find Keypoints



This tutorial aims to show how to use the Shi-Tomasi and Harris corner detection functions changing two simple parameters.


First, make sure that the tutorials are properly installed along with the TIAGo simulation, as shown in the Tutorials Installation Section.


In order to execute the demo first open a couple of consoles and source the public simulation workspace

cd /tiago_public_ws/
source ./devel/setup.bash

In the first console run the following simulation of TIAGo

$ roslaunch tiago_gazebo tiago_gazebo.launch public_sim:=true robot:=steel world:=tutorial_office gzpose:="-x -3.73 -y -3.83 -z 0.001 -R 0.0 -P 0.0 -Y -1.57"

The robot will be spawned in front of a shelve.

In the second console run the Corner Detection node as follows

$ rosrun tiago_opencv_tutorial corner_detection

Two windows will show up, one with the corners detected by the Harris function and one with the Shi-Tomasi one.


Sliders are provided in order to tune the paramteres of each corner detector.

Concepts & Code

The difference in the Shi-Tomasi and Harris functions is slight, however it becomes more apparent when their equation is shown.

Firstly, a corner in the OpenCV Mat structure is defined as a region in which the intensity varies greatly in all directions. The most important feature in the two methods is the scoring function R. The Harris method defines the scoring function as

R = v * w - k(v + w)^2

as represented in the source code as

   1   for( int j = 0; j < img_gray.rows; j++ )
   2     for( int i = 0; i < img_gray.cols; i++ )
   3     {
   4       float lambda_1 =<cv::Vec6f>(j, i)[0];
   5       float lambda_2 =<cv::Vec6f>(j, i)[1];
   6<float>(j,i) = lambda_1*lambda_2 - 0.04f*pow( ( lambda_1 + lambda_2 ), 2 );
   7     }

where v and w are the Eigen values of the matrix containing the image. In this case the difference in t Shi-Tomasi proposed a different cost function, namely

R = min(v,w)}

which simply states that if the eigen values are higher than a certain threshold value, the region is considered to be a corner. The resulting corners detected are slightly better, and more suited for some applications such as object tracking.

Wiki: Robots/TIAGo/Tutorials/CornerDetection (last edited 2021-08-19 07:04:39 by thomaspeyrucain)