OpenCV2.4.4实现Harris角点检测

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

using namespace cv;
using namespace std;

//全局变量
Mat src, src_gray;//源图和灰度图
int thresh = 200;//默认阈值
int max_thresh = 255;//滑块条最大值


void cornerHarris_demo( int, void* )
{
    Mat dst, dst_norm, dst_norm_scaled;
    dst = Mat::zeros( src.size(), CV_32FC1 );

    //参数
    int blockSize = 2;
    int apertureSize = 3;
    double k = 0.04;

    //Harris角点检测
    cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT );

    //像素归一化到[0,255]
    normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );
    convertScaleAbs( dst_norm, dst_norm_scaled );//去绝对值后变换为8位无符号类型

    Mat src_copy = src.clone();
    //在角点上画圈
    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),3,Scalar(0));
                circle(src_copy,Point(i,j),3,Scalar(0,255,0));
            }
        }
    }
    /// Showing the result
    namedWindow( "Corners", CV_WINDOW_AUTOSIZE );
    imshow( "Corners", dst_norm_scaled );
    imshow( "Src", src_copy );

}

int main()
{
    //加载源图像并转换为灰度图
    src = imread("horse.jpg");
    cvtColor( src, src_gray, CV_BGR2GRAY );

    //显示源图
    namedWindow( "Src", CV_WINDOW_AUTOSIZE );
    imshow( "Src", src );

    //创建滑块条,并指定回调函数为cornerHarris_demo(),每次滑块位置改变时都会调用此函数
    createTrackbar( "阈值:", "Src", &thresh, max_thresh, cornerHarris_demo );

    //调用函数,计算角点
    cornerHarris_demo( 0, 0 );

    waitKey(0);
    return(0);
}

效果图:

OpenCV2.4.4实现Harris角点检测_第1张图片

你可能感兴趣的:(OpenCV2.4.4实现Harris角点检测)