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

Harris角点检测

时间:2016-07-30 16:43:59      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

代码示例一:

#include<opencv2/opencv.hpp>
using namespace cv;

int main(){
    Mat src = imread("church.jpg", 0);
    imshow("原始图", src);

    //进行Harris角点检测找出角点
    Mat cornerStrength;
    cornerHarris(src, cornerStrength, 2, 3, 0.01);

    //对灰度图进行阈值操作,得到二值图并显示
    Mat harrisCorner;
    threshold(cornerStrength, harrisCorner, 0.0001, 255, THRESH_BINARY);
    imshow("二值效果图",harrisCorner);

    waitKey(0);
    return 0;
}

效果:

技术分享

代码示例二:

#include<opencv2/opencv.hpp>
using namespace cv;

#define WINDOW_NAME1 "窗口1"
#define WINDOW_NAME2 "窗口2"

Mat src,srcClone,gray;
int thresh = 30;
int max_thresh = 175;

//函数声明
void onCornerHarris(int, void*);//回调函数

int main(){
    src = imread("church.jpg", 1);
    imshow("原始图", src);
    srcClone = src.clone();
    cvtColor(srcClone, gray, COLOR_BGR2GRAY);

    //创建窗口和滚动条
    namedWindow(WINDOW_NAME1, WINDOW_AUTOSIZE);
    createTrackbar("阈值:", WINDOW_NAME1, &thresh, max_thresh, onCornerHarris);
    //调用一次回调函数进行初始化
    onCornerHarris(0, 0);

    waitKey(0);
    return 0;
}


void onCornerHarris(int, void*){
    Mat dstImage;//目标图
    Mat normImage;//归一化后的图
    Mat scaledImage;//线性变换后的8位无符号整形图

    //初始化
    dstImage = Mat::zeros(src.size(), CV_32FC1);
    srcClone = src.clone();

    //进行角点检测
    cornerHarris(gray, dstImage, 2, 3, 0.04, BORDER_DEFAULT);
    //归一化与转换
    normalize(dstImage, normImage, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
    convertScaleAbs(normImage, scaledImage);

    //绘制:将检测到的,符合阈值条件的角点绘制出来
    for (int j = 0; j < normImage.rows; j++){
        for (int i = 0; i < normImage.cols; i++){
            if ((int)normImage.at<float>(j, i)>thresh + 80){
                circle(srcClone, Point(i, j), 5, Scalar(10, 10, 255), 2, 8, 0);
                circle(scaledImage, Point(i, j), 5, Scalar(0, 10, 255), 2, 8, 0);
            }
        }
    }

    imshow(WINDOW_NAME1, srcClone);
    imshow(WINDOW_NAME2, scaledImage);
}

效果:

技术分享

 

Harris角点检测

标签:

原文地址:http://www.cnblogs.com/bluebean/p/5721032.html

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