当前位置:网站首页>(8) HS corner detection
(8) HS corner detection
2022-07-03 17:45:00 【Hengyoucheng】
1. The basic theory
When processing feature descriptors suitable for multiple images , There are two main feature detection methods , One is angle based detection , The other is to process all areas in the image . Here we mainly discuss angle based detection .
1988 year ,Harris and Stephens An angle detection algorithm is proposed , HS Angle detector , See paper A Combined Corner and Edge Detector.
HS The principle of the detector is as follows , There are three cases of gray changes ,1 Is that the gray level does not change in all directions , 2 It changes in a certain direction , But in the other direction, the grayscale remains unchanged ,3 It's a change in both directions .HS The angle detector uses mathematical formulas to distinguish these three situations .

f Represents an image ,f(s, t) By (s, t) A small piece of image defined by the value of patch,f(s+x, t+y) Is the same size but moved (x,y) Small image of patch, The weighted sum of the squares of the differences between the pixel values of each pixel of the two image blocks is expressed as :
C ( x , y ) = ∑ ∑ ω ( s , t ) [ f ( s + x , t + y ) − f ( s , t ) ] 2 C(x,y) = \sum\sum\omega(s,t)[f(s+x, t+y)-f(s,t)]^2 C(x,y)=∑∑ω(s,t)[f(s+x,t+y)−f(s,t)]2
ω ( s , t ) \omega(s,t) ω(s,t) Is a weighting function . Where there are corners , namely C(x,y) Taking the maximum . Take a pixel , Calculate in its adjacent pixels C ( x , y ) C(x,y) C(x,y).
take f ( s + x , t + y ) f(s+x, t+y) f(s+x,t+y) Represents the approximation of Taylor expansion ,
f ( s + x , t + y ) ≈ f ( s , t ) + x f x ( s , t ) + y f y ( s , t ) f(s+x, t+y)\approx f(s,t)+xf_x(s,t)+yf_y(s,t) f(s+x,t+y)≈f(s,t)+xfx(s,t)+yfy(s,t)
then :
C ( x , y ) = ∑ ∑ ω ( s , t ) [ x f x ( s , t ) + y f y ( s , t ) ] 2 C(x,y) = \sum\sum\omega(s,t)[xf_x(s,t)+yf_y(s,t)]^2 C(x,y)=∑∑ω(s,t)[xfx(s,t)+yfy(s,t)]2
The above equation can be expressed in matrix form :
C ( x , y ) = [ x , y ] M [ x y ] C(x,y) = [x,y]M\begin{bmatrix} x\\ y \end{bmatrix} C(x,y)=[x,y]M[xy]
among ,
M = ∑ ∑ ω ( s , t ) A M = \sum\sum\omega(s,t)A M=∑∑ω(s,t)A
A = [ f x 2 f x f y f x f y f y 2 ] A = \begin{bmatrix} f_x^2 & f_xf_y\\ f_xf_y & f_y^2 \end{bmatrix} A=[fx2fxfyfxfyfy2]
M M M Sometimes called Harris matrix .
matrix M M M The feature vector of points to the largest data expansion direction , And the corresponding eigenvalue is directly proportional to the amount of data expansion in the direction of the eigenvector . So we can know , Two small eigenvalues represent almost constant grayscale , A small eigenvalue and a large eigenvalue indicate the existence of a vertical or horizontal boundary , Two large eigenvalues indicate the existence of an isolated point or angle .
However , Because the computation cost of solving eigenvalues is large ,HS The angle detector does not use eigenvalues , Instead, it makes use of the properties of the square matrix , The trace is equal to the sum of the eigenvalues of the matrix , The determinant is equal to the product of eigenvalues ,
R = λ x λ y − k ( λ x + λ y ) 2 = d e t ( M ) − k t r a c e 2 ( M ) R=\lambda_x\lambda_y - k(\lambda_x + \lambda_y)^2=det(M)-ktrace^2(M) R=λxλy−k(λx+λy)2=det(M)−ktrace2(M)

k k k It's a constant , Usually take ( 0 , 0.25 ) (0, 0.25) (0,0.25).
When both eigenvalues are large , R Take the larger positive value , A larger eigenvalue and a smaller eigenvalue ,R Take a larger negative value , Both eigenvalues are small ,R The absolute value of is small
k It can be regarded as a sensitive factor ,k The smaller, the more corners you find
Shi-Tomasi Corner detector
1994 year ,Jianbo Shi and Carlo Tomasi Put forward Good features to track
Use it directly R = m i n ( λ x , λ y ) R=min(\lambda_x,\lambda_y) R=min(λx,λy) As a measure , Super parameters are avoided k k k
2.OpenCV API
2.1cornerHarris
void cv::cornerHarris(
InputArray src,
OutputArray dst,
int blockSize,
int ksize,
double k,
int borderType = BORDER_DEFAULT
)
src: Input ,8 Bit single channel image , grayscaledst: StorageHarrisData of test results , data typeCV_32FC1blockSize: CalculationC(x,y)Size of time neighborhoodksize: UseSobelWhen the operator calculates the gradient , The size of the convolution kernelk: CalculationRSuper parameter of time , That is, the mediation factor of the sensitivity of the detection angle , Small makes many angles , The bigger the horn, the lessborderType: How to deal with boundary pixels
2.2 goodFeaturesToTrack
void cv::goodFeaturesToTrack (
InputArray image,
OutputArray corners,
int maxCorners,
double qualityLevel,
double minDistance,
InputArray mask = noArray(),
int blockSize = 3,
bool useHarrisDetector = false,
double k = 0.04
)
What this function does ,
1. First use cornerHarris Calculate the measurement of each corner
2. In each pixel 3x3 The neighborhood range of performs maximum suppression
3. Each corner corresponds to Harris The minimum eigenvalue of the matrix is less than q u a l i t y L e v e l ∗ m a x ( q u a l i t y M e a s u r e M a p ( x , y ) ) qualityLevel*max(qualityMeasureMap(x,y)) qualityLevel∗max(qualityMeasureMap(x,y)), This corner will be discarded
4. According to quality measure Descending order
5. Remove those with distances less than maxDistance The corner of
image: Single channel imagecorners: The output vector of the anglemaxCorners: The maximum number of detected corners is supportedqualityLevel: Control the quality level of the angle , For example, the best is1500,qualityLevelby0.01, bequality measureLess than1500*0.01What you want will be abandonedminDistance: The minimum distance between cornersmask: Mask , Controls which part of the image to detect cornersblockSize: The neighborhood size used in calculating the gradient correlation matrix , Reference resources cornerEigenValsAndVecsuseHarrisDetector: Whether to useHarrisCorner detectionk:HarrisSuper parameter of corner detection
3. Example
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Mat src, src_gray;
int maxCorners = 23;
int maxTrackbar = 100;
RNG rng(12345);
int thresh = 200;
int max_thresh = 255;
const char* source_window = "Source image";
const char* corners_window = "Corners detected";
void cornerHarris_demo( int, void* );
void goodFeaturesToTrack_Demo( int, void* );
int main( int argc, char** argv )
{
CommandLineParser parser( argc, argv, "{@input | building.jpg | input image}" );
src = imread( samples::findFile( parser.get<String>( "@input" ) ) );
if ( src.empty() )
{
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
}
cvtColor( src, src_gray, COLOR_BGR2GRAY );
namedWindow( source_window );
createTrackbar( "Threshold: ", source_window, &thresh, max_thresh, cornerHarris_demo );
imshow( source_window, src );
// cornerHarris_demo( 0, 0 );
goodFeaturesToTrack_Demo( 0, 0 );
waitKey();
return 0;
}
void cornerHarris_demo( int, void* )
{
int blockSize = 2;
int apertureSize = 3;
double k = 0.04;
Mat dst = Mat::zeros( src.size(), CV_32FC1 );
cornerHarris( src_gray, dst, blockSize, apertureSize, k );
Mat dst_norm, dst_norm_scaled;
normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );
convertScaleAbs( dst_norm, dst_norm_scaled );
for( int i = 0; i < dst_norm.rows ; i++ )
{
for( int j = 0; j < dst_norm.cols; j++ )
{
if( (int) dst_norm.at<float>(i,j) > thresh )
{
circle( dst_norm_scaled, Point(j,i), 5, Scalar(0), 2, 8, 0 );
}
}
}
namedWindow( corners_window );
imshow( corners_window, dst_norm_scaled );
imwrite("corner_grid.png", dst_norm_scaled);
}
void goodFeaturesToTrack_Demo( int, void* )
{
maxCorners = MAX(maxCorners, 1);
vector<Point2f> corners;
double qualityLevel = 0.01;
double minDistance = 10;
int blockSize = 3, gradientSize = 3;
bool useHarrisDetector = false;
double k = 0.04;
Mat copy = src.clone();
goodFeaturesToTrack( src_gray,
corners,
maxCorners,
qualityLevel,
minDistance,
Mat(),
blockSize,
gradientSize,
useHarrisDetector,
k );
cout << "** Number of corners detected: " << corners.size() << endl;
int radius = 8;
for( size_t i = 0; i < corners.size(); i++ )
{
circle( copy, corners[i], radius, Scalar(0, 0, rng.uniform(0, 256)), FILLED );
}
namedWindow( source_window );
imwrite("corner_grid_st.png", copy);
imshow( source_window, copy );
}

边栏推荐
- PR second time
- 小程序 多tab 多swiper + 每个tab分页
- [combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
- Website with JS doesn't work in IE9 until the Developer Tools is activated
- 【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
- Assembly for unloading Loadfrom() loaded assembly - unloading the assembly loaded with assembly LoadFrom()
- Discussion sur la logique de conception et de mise en oeuvre du processus de paiement
- 企业级自定义表单引擎解决方案(十一)--表单规则引擎1
- Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
- 基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
猜你喜欢
![Luogu: p2685 [tjoi2012] Bridge](/img/f5/f77027288a211ae466781b09ce650f.jpg)
Luogu: p2685 [tjoi2012] Bridge
![[set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela](/img/df/a034032e203e7935dafaf8a71cb6c8.jpg)
[set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela

Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you

SWM32系列教程4-端口映射及串口应用

1164 Good in C

TCP拥塞控制详解 | 3. 设计空间

How to train mask r-cnn model with your own data

Vs2013 has blocked the installer, and ie10 needs to be installed

Qt调节Win屏幕亮度和声音大小

SQL injection database operation foundation
随机推荐
ArrayList分析3 : 删除元素
List的stream中Long对象与long判等问题记录
The third day of writing C language by Yabo people
How to enforce parameters in PowerShell- How do I make parameters mandatory in PowerShell?
Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028
TensorBoard快速入门(Pytorch使用TensorBoard)
[vscode] convert tabs to spaces
微服务组件Sentinel控制台调用
基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
自动渗透测试工具核心功能简述
聊聊支付流程的设计与实现逻辑
Detailed explanation of common network attacks
UE4 official charging resources, with a total price of several thousand
Leetcode540: a single element in an ordered array
Loop through JSON object list
1164 Good in C
[combinatorics] recursive equation (case where the non-homogeneous part is exponential | example where the non-homogeneous part is exponential)
问题随记 —— 在 edge 上看视频会绿屏
[combinatorics] recursive equation (four cases where the non-homogeneous part of a linear non-homogeneous recursive equation with constant coefficients is the general solution of the combination of po
TCP congestion control details | 3 design space