~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/doc/tutorials/features2d/trackingmotion/corner_subpixeles/corner_subpixeles.markdown

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Detecting corners location in subpixeles {#tutorial_corner_subpixeles}
 
2
========================================
 
3
 
 
4
Goal
 
5
----
 
6
 
 
7
In this tutorial you will learn how to:
 
8
 
 
9
-   Use the OpenCV function @ref cv::cornerSubPix to find more exact corner positions (more exact
 
10
    than integer pixels).
 
11
 
 
12
Theory
 
13
------
 
14
 
 
15
Code
 
16
----
 
17
 
 
18
This tutorial code's is shown lines below. You can also download it from
 
19
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/TrackingMotion/cornerSubPix_Demo.cpp)
 
20
@code{.cpp}
 
21
#include "opencv2/highgui.hpp"
 
22
#include "opencv2/imgproc.hpp"
 
23
#include <iostream>
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
 
 
27
using namespace cv;
 
28
using namespace std;
 
29
 
 
30
/// Global variables
 
31
Mat src, src_gray;
 
32
 
 
33
int maxCorners = 10;
 
34
int maxTrackbar = 25;
 
35
 
 
36
RNG rng(12345);
 
37
char* source_window = "Image";
 
38
 
 
39
/// Function header
 
40
void goodFeaturesToTrack_Demo( int, void* );
 
41
 
 
42
/* @function main */
 
43
int main( int argc, char** argv )
 
44
{
 
45
  /// Load source image and convert it to gray
 
46
  src = imread( argv[1], 1 );
 
47
  cvtColor( src, src_gray, COLOR_BGR2GRAY );
 
48
 
 
49
  /// Create Window
 
50
  namedWindow( source_window, WINDOW_AUTOSIZE );
 
51
 
 
52
  /// Create Trackbar to set the number of corners
 
53
  createTrackbar( "Max  corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo);
 
54
 
 
55
  imshow( source_window, src );
 
56
 
 
57
  goodFeaturesToTrack_Demo( 0, 0 );
 
58
 
 
59
  waitKey(0);
 
60
  return(0);
 
61
}
 
62
 
 
63
/*
 
64
 * @function goodFeaturesToTrack_Demo.cpp
 
65
 * @brief Apply Shi-Tomasi corner detector
 
66
 */
 
67
void goodFeaturesToTrack_Demo( int, void* )
 
68
{
 
69
  if( maxCorners < 1 ) { maxCorners = 1; }
 
70
 
 
71
  /// Parameters for Shi-Tomasi algorithm
 
72
  vector<Point2f> corners;
 
73
  double qualityLevel = 0.01;
 
74
  double minDistance = 10;
 
75
  int blockSize = 3;
 
76
  bool useHarrisDetector = false;
 
77
  double k = 0.04;
 
78
 
 
79
  /// Copy the source image
 
80
  Mat copy;
 
81
  copy = src.clone();
 
82
 
 
83
  /// Apply corner detection
 
84
  goodFeaturesToTrack( src_gray,
 
85
               corners,
 
86
               maxCorners,
 
87
               qualityLevel,
 
88
               minDistance,
 
89
               Mat(),
 
90
               blockSize,
 
91
               useHarrisDetector,
 
92
               k );
 
93
 
 
94
 
 
95
  /// Draw corners detected
 
96
  cout<<"** Number of corners detected: "<<corners.size()<<endl;
 
97
  int r = 4;
 
98
  for( int i = 0; i < corners.size(); i++ )
 
99
     { circle( copy, corners[i], r, Scalar(rng.uniform(0,255), rng.uniform(0,255),
 
100
                                                 rng.uniform(0,255)), -1, 8, 0 ); }
 
101
 
 
102
  /// Show what you got
 
103
  namedWindow( source_window, WINDOW_AUTOSIZE );
 
104
  imshow( source_window, copy );
 
105
 
 
106
  /// Set the neeed parameters to find the refined corners
 
107
  Size winSize = Size( 5, 5 );
 
108
  Size zeroZone = Size( -1, -1 );
 
109
  TermCriteria criteria = TermCriteria( TermCriteria::EPS + TermCriteria::MAX_ITER, 40, 0.001 );
 
110
 
 
111
  /// Calculate the refined corner locations
 
112
  cornerSubPix( src_gray, corners, winSize, zeroZone, criteria );
 
113
 
 
114
  /// Write them down
 
115
  for( int i = 0; i < corners.size(); i++ )
 
116
     { cout<<" -- Refined Corner ["<<i<<"]  ("<<corners[i].x<<","<<corners[i].y<<")"<<endl; }
 
117
}
 
118
@endcode
 
119
Explanation
 
120
-----------
 
121
 
 
122
Result
 
123
------
 
124
 
 
125
![](images/Corner_Subpixeles_Original_Image.jpg)
 
126
 
 
127
Here is the result:
 
128
 
 
129
![](images/Corner_Subpixeles_Result.jpg)