OpenCV Tutorials —— 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) have a high variation, which can be used to detect it.

 

  • Consider a grayscale image 灰度图像. We are going to sweep a window (with displacements in the x direction and in the right direction) and will calculate the variation of intensity.

    where:

    • is the window at position
    • is the intensity at
    • is the intensity at the moved window

 

  • Since we are looking for windows with corners, we are looking for windows with a large variation in intensity. Hence, we have to maximize the equation above, specifically the term:  角点 ~~ 梯度值

 

  • Using Taylor expansion:  使用泰勒展开

  • Expanding the equation and cancelling properly:

  • Which can be expressed in a matrix form as:

  • Let’s denote:

  • So, our equation now is:

 

  • A score is calculated for each window, to determine if it can possibly contain a corner:

    where:

    • det(M) =
    • trace(M) =

    a window with a score greater than a certain value is considered a “corner”

通过阈值判断是否属于角点 ~~

 

程序流程

1,检测角点:

cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT );

2,归一化:
normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );

3,计算绝对值

convertScaleAbs( dst_norm, dst_norm_scaled );

4,阈值判断

 

void cornerHarris(InputArray src, OutputArray dst, int blockSize, int ksize, double k, intborderType=BORDER_DEFAULT )

Parameters:

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

 

C++: void convertScaleAbs(InputArray src, OutputArray dst, double alpha=1, double beta=0)

On each element of the input array, the function convertScaleAbs performs three operations sequentially: scaling, taking an absolute value, conversion to an unsigned 8-bit type:

In case of multi-channel arrays, the function processes each channel independently.

 

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 thresh = 200;
int max_thresh = 255;

char* source_window = "Source image";
char* corners_window = "Corners detected";

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

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

	/// Create a window and a trackbar
	namedWindow( source_window, CV_WINDOW_AUTOSIZE );
	createTrackbar( "Threshold: ", source_window, &thresh, max_thresh, cornerHarris_demo );
	imshow( source_window, src );

	cornerHarris_demo( 0, 0 );

	waitKey(0);
	return(0);
}

/** @function cornerHarris_demo */
void cornerHarris_demo( int, void* )
{

	Mat dst, dst_norm, dst_norm_scaled;
	dst = Mat::zeros( src.size(), CV_32FC1 );

	/// Detector parameters
	int blockSize = 2;
	int apertureSize = 3;
	double k = 0.04;

	/// Detecting corners
	cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT );

	/// Normalizing
	normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );
	convertScaleAbs( dst_norm, dst_norm_scaled );

	/// Drawing a circle around corners
	for( int j = 0; j < dst_norm.rows ; j++ )
	{ for( int i = 0; i < dst_norm.cols; i++ )
	{
		if( (int) dst_norm.at<float>(j,i) > thresh )
		{
			circle( dst_norm_scaled, Point( i, j ), 5,  Scalar(0), 2, 8, 0 );
		}
	}
	}
	/// Showing the result
	namedWindow( corners_window, CV_WINDOW_AUTOSIZE );
	imshow( corners_window, dst_norm_scaled );
}

 

注意:

apertureSize  和 参数k 的取值 ~~

时间: 2024-09-30 11:09:15

OpenCV Tutorials —— Harris corner detector的相关文章

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 Tutorials &mdash;&mdash; Canny Edge Detector

最好的边缘检测子 Canny algorithm aims to satisfy three main criteria: Low error rate: Meaning a good detection of only existent edges. Good localization: The distance between edge pixels detected and real edge pixels have to be minimized. Minimal response: O

OpenCV Tutorials &mdash;&mdash; 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 进行

OpenCV Feature Detection and Description -- Harris Corner Detection Harris角点检测

原文链接 https://docs.opencv.org/4.1.2/dc/d0d/tutorial_py_features_harris.html 阅读文档学习opencv 如有问题,大家指出-- Goal In this chapter, We will understand the concepts behind Harris Corner Detection. We will see the functions: cv.cornerHarris(), cv.cornerSubPix()

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使用Harris算法实现角点检测

纯粹阅读,请移步OpenCV使用Harris算法实现角点检测 效果图 源码 KqwOpenCVFeaturesDemo 角点是两条边缘的交点或者在局部邻域中有多个显著边缘方向的点.Harris角点检测是一种在角点检测中最常见的技术. Harris角点检测器在图像上使用滑动窗口计算亮度的变化. 封装 这里用到了RxJava.主要是因为图片处理是耗时操作,会阻塞线程,为了防止界面卡顿,这里使用RxJava进行了线程切换. /** * Harris角点检测 * * @param bitmap 要检测的

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

Harris Corner(Harris角检测)

在做图像匹配时,常需要对两幅图像中的特征点进行匹配.为了保证匹配的准确性,所选择的特征必须有其独特性,角点可以作为一种不错的特征. 那么为什么角点有其独特性呢?角点往往是两条边缘的交点,它是两条边缘方向变换的一种表示,因此其两个方向的梯度变换通常都比较大并且容易检测到. 这里我们理解一下Harris Corner 一种角点检测的算法 角点检测基本原理: 人们通常通过在一个小的窗口区域内观察点的灰度值大小来识别角点,如果往任何方向移动窗口都会引起比较大的灰度变换那么往往这就是我们要找的角点.如下图