 |
OpenCV
3.2.0
Open Source Computer Vision
|
.2.0+dfsg_doc_tutorials_features2d_trackingmotion_corner_subpixeles_corner_subpixeles
Goal
In this tutorial you will learn how to:
- Use the OpenCV function cv::cornerSubPix to find more exact corner positions (more exact than integer pixels).
Theory
Code
This tutorial code's is shown lines below. You can also download it from here
#include <iostream>
using namespace std;
int maxCorners = 10;
int maxTrackbar = 25;
const char* source_window = "Image";
void goodFeaturesToTrack_Demo( int, void* );
int main( int, char** argv )
{
createTrackbar(
"Max corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo );
goodFeaturesToTrack_Demo( 0, 0 );
return(0);
}
void goodFeaturesToTrack_Demo( int, void* )
{
if( maxCorners < 1 ) { maxCorners = 1; }
vector<Point2f> corners;
double qualityLevel = 0.01;
double minDistance = 10;
int blockSize = 3;
bool useHarrisDetector = false;
double k = 0.04;
corners,
maxCorners,
qualityLevel,
minDistance,
blockSize,
useHarrisDetector,
k );
cout<<"** Number of corners detected: "<<corners.size()<<endl;
int r = 4;
for(
size_t i = 0;
i < corners.size();
i++ )
{
circle( copy, corners[
i], r,
Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255)), -1, 8, 0 ); }
imshow( source_window, copy );
cornerSubPix( src_gray, corners, winSize, zeroZone, criteria );
for(
size_t i = 0;
i < corners.size();
i++ )
{ cout<<
" -- Refined Corner ["<<
i<<
"] ("<<corners[
i].x<<
","<<corners[
i].y<<
")"<<endl; }
}
Explanation
Result
Here is the result:
The class defining termination criteria for iterative algorithms.
Definition: types.hpp:781
@ IMREAD_COLOR
If set, always convert image to the 3 channel BGR color image.
Definition: imgcodecs.hpp:67
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
void goodFeaturesToTrack(InputArray image, OutputArray corners, int maxCorners, double qualityLevel, double minDistance, InputArray mask=noArray(), int blockSize=3, bool useHarrisDetector=false, double k=0.04)
Determines strong corners on an image.
void cornerSubPix(InputArray image, InputOutputArray corners, Size winSize, Size zeroZone, TermCriteria criteria)
Refines the corner locations.
int waitKey(int delay=0)
Waits for a pressed key.
@ COUNT
the maximum number of iterations or elements to compute
Definition: types.hpp:788
@ EPS
the desired accuracy or change in parameters at which the iterative algorithm stops
Definition: types.hpp:790
Template class for specifying the size of an image or rectangle.
Definition: types.hpp:291
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
Size2i Size
Definition: types.hpp:315
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
@ COLOR_BGR2GRAY
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:538
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
Scalar_< double > Scalar
Definition: types.hpp:606
Random Number Generator.
Definition: core.hpp:2690
Mat clone() const
Creates a full copy of the array and the underlying data.
n-dimensional dense array class
Definition: mat.hpp:741
for i
Definition: modelConvert.m:63
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.
Definition: affine.hpp:52
@ WINDOW_AUTOSIZE
the user cannot resize the window, the size is constrainted by the image displayed.
Definition: highgui.hpp:184
void circle(InputOutputArray img, Point center, int radius, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a circle.