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

OpenCV Tutorials —— Harris corner detector

时间:2014-11-26 16:04:47      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   color   os   使用   sp   for   

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 bubuko.com,布布扣 灰度图像. We are going to sweep a window bubuko.com,布布扣 (with displacements bubuko.com,布布扣 in the x direction andbubuko.com,布布扣 in the right direction) bubuko.com,布布扣 and will calculate the variation of intensity.

    bubuko.com,布布扣

    where:

    • bubuko.com,布布扣 is the window at position bubuko.com,布布扣
    • bubuko.com,布布扣 is the intensity at bubuko.com,布布扣
    • bubuko.com,布布扣 is the intensity at the moved window bubuko.com,布布扣

 

  • 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:  角点 ~~ 梯度值

    bubuko.com,布布扣

 

  • Using Taylor expansion:  使用泰勒展开

    bubuko.com,布布扣

  • Expanding the equation and cancelling properly:

    bubuko.com,布布扣

  • Which can be expressed in a matrix form as:

    bubuko.com,布布扣

  • Let’s denote:

    bubuko.com,布布扣

  • So, our equation now is:

    bubuko.com,布布扣

 

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

    bubuko.com,布布扣

    where:

    • det(M) = bubuko.com,布布扣
    • trace(M) = bubuko.com,布布扣

    a window with a score bubuko.com,布布扣 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:

bubuko.com,布布扣

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 的取值 ~~

OpenCV Tutorials —— Harris corner detector

标签:style   http   io   ar   color   os   使用   sp   for   

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

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