OpenCV Tutorials —— Creating yor own corner detector

  • Use the OpenCV function cornerEigenValsAndVecs to find the eigenvalues and eigenvectors to determine if a pixel is a corner.
  • Use the OpenCV function cornerMinEigenVal to find the minimum eigenvalues for corner detection.

 

最小特征值对应的角点监测 ~~

对自相关矩阵 M 进行特征值分析,产生两个特征值和两个特征方向向量。因为较大的不确定度取决于较小的特征值,也就是,所以通过寻找最小特征值的最大值来寻找好的特征点

 

void cornerEigenValsAndVecs(InputArray src, OutputArray dst, int blockSize, int ksize, intborderType=BORDER_DEFAULT )

Parameters:

  • src – Input single-channel 8-bit or floating-point image.
  • dst – Image to store the results. It has the same size as src and the type CV_32FC(6) .
  • blockSize – Neighborhood size (see details below).
  • ksize – Aperture parameter for the Sobel() operator.
  • borderType – Pixel extrapolation method. See borderInterpolate() .

 

For every pixel , the function cornerEigenValsAndVecs considers a blockSize blockSize neighborhood . It calculates the covariation matrix of derivatives over the neighborhood as:  导数的共生矩阵 ~~ 导数的自相关矩阵

 

void cornerMinEigenVal(InputArray src, OutputArray dst, int blockSize, int ksize=3, intborderType=BORDER_DEFAULT )

Parameters:

  • src – Input single-channel 8-bit or floating-point image.
  • dst – Image to store the minimal eigenvalues. It has the type CV_32FC1 and the same size as src .
  • blockSize – Neighborhood size (see the details on cornerEigenValsAndVecs() ).
  • ksize – Aperture parameter for the Sobel() operator.
  • borderType – Pixel extrapolation method. See borderInterpolate() .

 

The function is similar to cornerEigenValsAndVecs() but it calculates and stores only the minimal eigenvalue of the covariance matrix of derivatives

 

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;
Mat myHarris_dst; Mat myHarris_copy; Mat Mc;
Mat myShiTomasi_dst; Mat myShiTomasi_copy;

int myShiTomasi_qualityLevel = 50;
int myHarris_qualityLevel = 50;
int max_qualityLevel = 100;

double myHarris_minVal; double myHarris_maxVal;
double myShiTomasi_minVal; double myShiTomasi_maxVal;

RNG rng(12345);

const char* myHarris_window = "My Harris corner detector";
const char* myShiTomasi_window = "My Shi Tomasi corner detector";

/// Function headers
void myShiTomasi_function( int, void* );
void myHarris_function( int, void* );

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

  /// Set some parameters
  int blockSize = 3; int apertureSize = 3;

  /// My Harris matrix -- Using cornerEigenValsAndVecs
  myHarris_dst = Mat::zeros( src_gray.size(), CV_32FC(6) );
  Mc = Mat::zeros( src_gray.size(), CV_32FC1 );

  cornerEigenValsAndVecs( src_gray, myHarris_dst, blockSize, apertureSize, BORDER_DEFAULT );

  /* calculate Mc */
  for( int j = 0; j < src_gray.rows; j++ )
     { for( int i = 0; i < src_gray.cols; i++ )
          {
            float lambda_1 = myHarris_dst.at<Vec6f>(j, i)[0];
            float lambda_2 = myHarris_dst.at<Vec6f>(j, i)[1];
            Mc.at<float>(j,i) = lambda_1*lambda_2 - 0.04f*pow( ( lambda_1 + lambda_2 ), 2 );
          }
     }

  minMaxLoc( Mc, &myHarris_minVal, &myHarris_maxVal, 0, 0, Mat() );

  /* Create Window and Trackbar */
  namedWindow( myHarris_window, WINDOW_AUTOSIZE );
  createTrackbar( " Quality Level:", myHarris_window, &myHarris_qualityLevel, max_qualityLevel, myHarris_function );
  myHarris_function( 0, 0 );

  /// My Shi-Tomasi -- Using cornerMinEigenVal
  myShiTomasi_dst = Mat::zeros( src_gray.size(), CV_32FC1 );
  cornerMinEigenVal( src_gray, myShiTomasi_dst, blockSize, apertureSize, BORDER_DEFAULT );

  minMaxLoc( myShiTomasi_dst, &myShiTomasi_minVal, &myShiTomasi_maxVal, 0, 0, Mat() );

  /* Create Window and Trackbar */
  namedWindow( myShiTomasi_window, WINDOW_AUTOSIZE );
  createTrackbar( " Quality Level:", myShiTomasi_window, &myShiTomasi_qualityLevel, max_qualityLevel, myShiTomasi_function );
  myShiTomasi_function( 0, 0 );

  waitKey(0);
  return(0);
}

/**
 * @function myShiTomasi_function
 */
void myShiTomasi_function( int, void* )
{
  myShiTomasi_copy = src.clone();

  if( myShiTomasi_qualityLevel < 1 ) { myShiTomasi_qualityLevel = 1; }

  for( int j = 0; j < src_gray.rows; j++ )
     { for( int i = 0; i < src_gray.cols; i++ )
          {
            if( myShiTomasi_dst.at<float>(j,i) > myShiTomasi_minVal + ( myShiTomasi_maxVal - myShiTomasi_minVal )*myShiTomasi_qualityLevel/max_qualityLevel )
              { circle( myShiTomasi_copy, Point(i,j), 4, Scalar( rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255) ), -1, 8, 0 ); }
          }
     }
  imshow( myShiTomasi_window, myShiTomasi_copy );
}

/**
 * @function myHarris_function
 */
void myHarris_function( int, void* )
{
  myHarris_copy = src.clone();

  if( myHarris_qualityLevel < 1 ) { myHarris_qualityLevel = 1; }

  for( int j = 0; j < src_gray.rows; j++ )
     { for( int i = 0; i < src_gray.cols; i++ )
          {
            if( Mc.at<float>(j,i) > myHarris_minVal + ( myHarris_maxVal - myHarris_minVal )*myHarris_qualityLevel/max_qualityLevel )
              { circle( myHarris_copy, Point(i,j), 4, Scalar( rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255) ), -1, 8, 0 ); }
          }
     }
  imshow( myHarris_window, myHarris_copy );
}
时间: 2024-10-27 07:25:05

OpenCV Tutorials —— Creating yor own corner detector的相关文章

OpenCV Tutorials &mdash;&mdash; Creating a video with OpenCV

写video 需要用到 VideoWriter  视频文件可看作一个容器 视频的类型由视频文件的后缀名来指定   Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you ca

OpenCV Tutorials —— Creating Widgets

Explanation Extend Widget3D class to create a new 3D widget. Assign a VTK actor to the widget. Set color of the widget. Construct a triangle widget and display it in the window. Code #include <opencv2/viz/vizcore.hpp> #include <opencv2/viz/widget

OpenCV Tutorials &mdash;&mdash; Creating Bounding rotated boxes and ellipses for contours

外接旋转矩形 ,或外接椭圆   RotatedRect minAreaRect(InputArray points) 返回面积最小的外接矩形 RotatedRect fitEllipse(InputArray points) The function calculates the ellipse that fits (in a least-squares sense) a set of 2D points best of all. It returns the rotated rectangle

OpenCV Tutorials &mdash;&mdash; Creating Bounding boxes and circles for contours

同样是提取出轮廓之后的处理 ~~   void approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed) Parameters: curve – Input vector of a 2D point stored in: std::vector or Mat (C++ interface) Nx2 numpy array (Python interface) CvSeq or CvMa

OpenCV Tutorials &mdash;&mdash; Shi-Tomasi corner detector

Shi-Tomasi 算法是Harris 算法的改进. Harris 算法最原始的定义是将矩阵 M 的行列式值与 M 的迹相减,再将差值同预先给定的阈值进行比较.后来Shi 和Tomasi 提出改进的方法,若两个特征值中较小的一个大于最小阈值,则会得到强角点.   void goodFeaturesToTrack(InputArray image, OutputArray corners, int maxCorners, double qualityLevel, doubleminDistanc

OpenCV Feature Detection and Description -- Shi-Tomasi Corner Detector

原文链接:https://docs.opencv.org/4.1.2/d4/d8c/tutorial_py_shi_tomasi.html 如有错误欢迎指出-谢谢 Goal In this chapter, We will learn about the another corner detector: Shi-Tomasi Corner Detector We will see the function: cv.goodFeaturesToTrack() 目标: 在此章节, 我们会学习其他的角

学习opencv tutorials

1.opencv里头动态库和静态库的区别 lib是动态库,staticlib是静态库. 这是opencv tutorials中对动态库和静态库的说明.动态库是在runtime时候才load的库文件.而静态库文件会在你build的时候build-in inside your exe file.优点是可以避免误删,缺点是应用程序变大,加载时间也会变长. 2.  Visual Studio中solution和project的关系 在VS中,一个solution中可以包含多个project. 3.  两

OpenCV Tutorials &mdash;&mdash; Detecting corners location in subpixeles

亚像素精度(更加精确) ~~ 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 o

OpenCV Tutorials &mdash;&mdash; Harris corner detector

Harris 角点检测 ~~   Why is a corner so special Because, since it is the intersection of two edges, it represents a point in which the directions of these two edges change.  角点是两条边界的交点,体现了两个梯度方向上的变化 Hence, the gradient of the image (in both directions) h