码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV Tutorials —— Detecting corners location in subpixeles

时间:2014-11-26 18:34:59      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   color   os   sp   for   strong   

亚像素精度(更加精确) ~~

void cornerSubPix(InputArray image, InputOutputArray corners, Size winSize, Size zeroZone, TermCriteriacriteria)

Parameters:

  • image – Input image.
  • corners – Initial coordinates of the input corners and refined coordinates provided for output.
  • winSize – Half of the side length of the search window. For example, if winSize=Size(5,5) , then a bubuko.com,布布扣 search window is used.
  • zeroZone – Half of the size of the dead region in the middle of the search zone over which the summation in the formula below is not done. It is used sometimes to avoid possible singularities of the autocorrelation matrix. The value of (-1,-1) indicates that there is no such a size.
  • criteria – Criteria for termination of the iterative process of corner refinement. That is, the process of corner position refinement stops either after criteria.maxCount iterations or when the corner position moves by less than criteria.epsilon on some iteration.

 

The function iterates to find the sub-pixel accurate location of corners or radial saddle points, as shown on the figure below.

bubuko.com,布布扣

Sub-pixel accurate corner locator is based on the observation that every vector from the center bubuko.com,布布扣 to a point bubuko.com,布布扣 located within a neighborhood of bubuko.com,布布扣 is orthogonal to the image gradient at bubuko.com,布布扣 subject to image and measurement noise. Consider the expression:

bubuko.com,布布扣

where bubuko.com,布布扣 is an image gradient at one of the points bubuko.com,布布扣 in a neighborhood of bubuko.com,布布扣 . The value of bubuko.com,布布扣 is to be found so thatbubuko.com,布布扣 is minimized. A system of equations may be set up with bubuko.com,布布扣 set to zero:

bubuko.com,布布扣

where the gradients are summed within a neighborhood (“search window”) of bubuko.com,布布扣 . Calling the first gradient term bubuko.com,布布扣 and the second gradient term bubuko.com,布布扣 gives:

bubuko.com,布布扣

The algorithm sets the center of the neighborhood window at this new center bubuko.com,布布扣 and then iterates until the center stays within a set threshold.

 

Code

#include "stdafx.h"

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

/// Global variables
Mat src, src_gray;

int maxCorners = 10;
int maxTrackbar = 25;

RNG rng(12345);
char* source_window = "Image";

/// Function header
void goodFeaturesToTrack_Demo( int, void* );

/** @function main */
int main( int argc, char** argv )
{
  /// Load source image and convert it to gray
  src = imread( "xue.jpg", 1 );
  cvtColor( src, src_gray, CV_BGR2GRAY );

  /// Create Window
  namedWindow( source_window, CV_WINDOW_AUTOSIZE );

  /// Create Trackbar to set the number of corners
  createTrackbar( "Max  corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo);

  imshow( source_window, src );

  goodFeaturesToTrack_Demo( 0, 0 );

  waitKey(0);
  return(0);
}

/**
 * @function goodFeaturesToTrack_Demo.cpp
 * @brief Apply Shi-Tomasi corner detector
 */
void goodFeaturesToTrack_Demo( int, void* )
{
  if( maxCorners < 1 ) { maxCorners = 1; }

  /// Parameters for Shi-Tomasi algorithm
  vector<Point2f> corners;
  double qualityLevel = 0.01;
  double minDistance = 10;
  int blockSize = 3;
  bool useHarrisDetector = false;
  double k = 0.04;

  /// Copy the source image
  Mat copy;
  copy = src.clone();

  /// Apply corner detection
  goodFeaturesToTrack( src_gray,
                       corners,
                       maxCorners,
                       qualityLevel,
                       minDistance,
                       Mat(),
                       blockSize,
                       useHarrisDetector,
                       k );


  /// Draw corners detected
  cout<<"** Number of corners detected: "<<corners.size()<<endl;
  int r = 4;
  for( int 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 ); }

  /// Show what you got
  namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  imshow( source_window, copy );

  /// Set the neeed parameters to find the refined corners
  Size winSize = Size( 5, 5 );
  Size zeroZone = Size( -1, -1 );
  TermCriteria criteria = TermCriteria( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 40, 0.001 );

  /// Calculate the refined corner locations
  cornerSubPix( src_gray, corners, winSize, zeroZone, criteria );

  /// Write them down
  for( int i = 0; i < corners.size(); i++ )
     { cout<<" -- Refined Corner ["<<i<<"]  ("<<corners[i].x<<","<<corners[i].y<<")"<<endl; }
}

OpenCV Tutorials —— Detecting corners location in subpixeles

标签:style   http   io   ar   color   os   sp   for   strong   

原文地址:http://www.cnblogs.com/sprint1989/p/4123813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!