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

亚像素级角点定位

时间:2016-08-03 21:38:41      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

代码示例:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;

#define WINDOW_NAME "Shi-Tomasi角点检测"

Mat src, gray;
int maxCornerNum = 10;
int maxTrackbarNum = 500;


//滚动条回调函数
void cornersRefinement(int, void*){
    Mat copy = src.clone();
    if (maxCornerNum <= 1){
        maxCornerNum = 1;
    }
    //角点检测参数准备
    vector<Point2f> corners;
    double qualityLevel=0.01;//角点检测可接受的最小特征值
    double minDistance = 10;//角点之间的最小距离
    int blockSize = 3;//计算导数自相关矩阵时的指定的领域范围
    double k = 0.04;//权重系数

    //进行Shi-Tomasi角点检测
    goodFeaturesToTrack(gray, 
        corners, 
        maxCornerNum, 
        qualityLevel,
        minDistance, 
        Mat(), 
        blockSize, 
        false, 
        k);
    //像素级角点使用蓝色圆圈绘制
    for (int i = 0; i < corners.size(); i++){
        circle(copy, corners[i], 4, Scalar(255, 0, 0), 2, 8, 0);
    }

    /// 角点位置精准化参数
    Size winSize = Size(5, 5);
    Size zeroZone = Size(-1, -1);
    TermCriteria criteria = TermCriteria(
        CV_TERMCRIT_EPS + CV_TERMCRIT_ITER,
        40, //maxCount=40
        0.001);    //epsilon=0.001
    /// 计算精准化后的角点位置
    cornerSubPix(gray, corners, winSize, zeroZone, criteria);

    //亚像素级角点使用品红色圆圈绘制
    for (int i = 0; i < corners.size(); i++){
        circle(copy, corners[i], 4, Scalar(255, 0, 255), 2, 8, 0);
        // 输出角点坐标  
        cout << " [" << i << "]  (" << corners[i].x << "," << corners[i].y << ")" << endl;
    }
    cout << endl;
    imshow(WINDOW_NAME, copy);
}

int main(){
    src = imread("church.jpg", 1);
    cvtColor(src, gray, COLOR_BGR2GRAY);

    namedWindow(WINDOW_NAME, WINDOW_AUTOSIZE);
    createTrackbar("最大角点数", WINDOW_NAME, &maxCornerNum, maxTrackbarNum, cornersRefinement);
    imshow(WINDOW_NAME, src);
    cornersRefinement(0, 0);

    waitKey(0);
    return 0;
}

效果:

技术分享

技术分享

 

亚像素级角点定位

标签:

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

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