OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Creating yor own corner detector

Goal

In this tutorial you will learn how to:

Theory

Code

This tutorial code's is shown lines below. You can also download it from here

1 
6 #include "opencv2/imgcodecs.hpp"
9 #include <iostream>
10 #include <stdio.h>
11 #include <stdlib.h>
12 
13 using namespace cv;
14 using namespace std;
15 
17 Mat src, src_gray;
18 Mat myHarris_dst; Mat myHarris_copy; Mat Mc;
19 Mat myShiTomasi_dst; Mat myShiTomasi_copy;
20 
21 int myShiTomasi_qualityLevel = 50;
22 int myHarris_qualityLevel = 50;
23 int max_qualityLevel = 100;
24 
25 double myHarris_minVal; double myHarris_maxVal;
26 double myShiTomasi_minVal; double myShiTomasi_maxVal;
27 
28 RNG rng(12345);
29 
30 const char* myHarris_window = "My Harris corner detector";
31 const char* myShiTomasi_window = "My Shi Tomasi corner detector";
32 
34 void myShiTomasi_function( int, void* );
35 void myHarris_function( int, void* );
36 
40 int main( int, char** argv )
41 {
43  src = imread( argv[1], 1 );
44  cvtColor( src, src_gray, COLOR_BGR2GRAY );
45 
47  int blockSize = 3; int apertureSize = 3;
48 
50  myHarris_dst = Mat::zeros( src_gray.size(), CV_32FC(6) );
51  Mc = Mat::zeros( src_gray.size(), CV_32FC1 );
52 
53  cornerEigenValsAndVecs( src_gray, myHarris_dst, blockSize, apertureSize, BORDER_DEFAULT );
54 
55  /* calculate Mc */
56  for( int j = 0; j < src_gray.rows; j++ )
57  { for( int i = 0; i < src_gray.cols; i++ )
58  {
59  float lambda_1 = myHarris_dst.at<Vec6f>(j, i)[0];
60  float lambda_2 = myHarris_dst.at<Vec6f>(j, i)[1];
61  Mc.at<float>(j,i) = lambda_1*lambda_2 - 0.04f*pow( ( lambda_1 + lambda_2 ), 2 );
62  }
63  }
64 
65  minMaxLoc( Mc, &myHarris_minVal, &myHarris_maxVal, 0, 0, Mat() );
66 
67  /* Create Window and Trackbar */
68  namedWindow( myHarris_window, WINDOW_AUTOSIZE );
69  createTrackbar( " Quality Level:", myHarris_window, &myHarris_qualityLevel, max_qualityLevel, myHarris_function );
70  myHarris_function( 0, 0 );
71 
73  myShiTomasi_dst = Mat::zeros( src_gray.size(), CV_32FC1 );
74  cornerMinEigenVal( src_gray, myShiTomasi_dst, blockSize, apertureSize, BORDER_DEFAULT );
75 
76  minMaxLoc( myShiTomasi_dst, &myShiTomasi_minVal, &myShiTomasi_maxVal, 0, 0, Mat() );
77 
78  /* Create Window and Trackbar */
79  namedWindow( myShiTomasi_window, WINDOW_AUTOSIZE );
80  createTrackbar( " Quality Level:", myShiTomasi_window, &myShiTomasi_qualityLevel, max_qualityLevel, myShiTomasi_function );
81  myShiTomasi_function( 0, 0 );
82 
83  waitKey(0);
84  return(0);
85 }
86 
90 void myShiTomasi_function( int, void* )
91 {
92  myShiTomasi_copy = src.clone();
93 
94  if( myShiTomasi_qualityLevel < 1 ) { myShiTomasi_qualityLevel = 1; }
95 
96  for( int j = 0; j < src_gray.rows; j++ )
97  { for( int i = 0; i < src_gray.cols; i++ )
98  {
99  if( myShiTomasi_dst.at<float>(j,i) > myShiTomasi_minVal + ( myShiTomasi_maxVal - myShiTomasi_minVal )*myShiTomasi_qualityLevel/max_qualityLevel )
100  { circle( myShiTomasi_copy, Point(i,j), 4, Scalar( rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255) ), -1, 8, 0 ); }
101  }
102  }
103  imshow( myShiTomasi_window, myShiTomasi_copy );
104 }
105 
109 void myHarris_function( int, void* )
110 {
111  myHarris_copy = src.clone();
112 
113  if( myHarris_qualityLevel < 1 ) { myHarris_qualityLevel = 1; }
114 
115  for( int j = 0; j < src_gray.rows; j++ )
116  { for( int i = 0; i < src_gray.cols; i++ )
117  {
118  if( Mc.at<float>(j,i) > myHarris_minVal + ( myHarris_maxVal - myHarris_minVal )*myHarris_qualityLevel/max_qualityLevel )
119  { circle( myHarris_copy, Point(i,j), 4, Scalar( rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255) ), -1, 8, 0 ); }
120  }
121  }
122  imshow( myHarris_window, myHarris_copy );
123 }
Scalar_< double > Scalar
Definition: types.hpp:597
void minMaxLoc(InputArray src, double *minVal, double *maxVal=0, Point *minLoc=0, Point *maxLoc=0, InputArray mask=noArray())
Finds the global minimum and maximum in an array.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
static MatExpr zeros(int rows, int cols, int type)
Returns a zero array of the specified size and type.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
#define CV_32FC(n)
Definition: cvdef.h:150
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
Template class for short numerical vectors, a partial case of Matx.
Definition: matx.hpp:300
void cornerEigenValsAndVecs(InputArray src, OutputArray dst, int blockSize, int ksize, int borderType=BORDER_DEFAULT)
Calculates eigenvalues and eigenvectors of image blocks for corner detection.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
for i
Definition: modelConvert.m:63
#define CV_32FC1
Definition: cvdef.h:146
Random Number Generator.
Definition: core.hpp:2600
void pow(InputArray src, double power, OutputArray dst)
Raises every array element to a power.
Definition: highgui.hpp:138
void cornerMinEigenVal(InputArray src, OutputArray dst, int blockSize, int ksize=3, int borderType=BORDER_DEFAULT)
Calculates the minimal eigenvalue of gradient matrices for corner detection.
same as BORDER_REFLECT_101
Definition: base.hpp:242
int main(int argc, const char *argv[])
Definition: facerec_demo.cpp:67
n-dimensional dense array class
Definition: mat.hpp:726
void circle(InputOutputArray img, Point center, int radius, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a circle.
Point2i Point
Definition: types.hpp:181
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:511
int waitKey(int delay=0)
Waits for a pressed key.

Explanation

Result

My_Harris_corner_detector_Result.jpg
My_Shi_Tomasi_corner_detector_Result.jpg