opencv::亞像素級別角點檢測


 

提高檢測精准度理論與現實總是不一致的,實際情況下幾乎所有的角點不會是一個真正的准確像素點。(100,5)實際上(100.2345.789)
 - 跟蹤 
 - 三維重建 
 - 相機校正

 

亞像素定位 
- 插值方法 
- 基於圖像矩計算 
- 曲線擬合方法 (高斯曲面、多項式、橢圓曲面)

 

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;
int max_corners = 20;
int max_count = 50;
Mat src, gray_src;
const char* output_title = "SubPixel Result";
void SubPixel_Demo(int, void*);
int main(int argc, char** argv) {
    src = imread("D:/vcprojects/images/home.jpg");
    if (src.empty()) {
        printf("could not load image...\n");
        return -1;
    }
    namedWindow("input image", CV_WINDOW_AUTOSIZE);
    imshow("input image", src);
    cvtColor(src, gray_src, COLOR_BGR2GRAY);
    namedWindow(output_title, CV_WINDOW_AUTOSIZE);
    createTrackbar("Corners:", output_title, &max_corners, max_count, SubPixel_Demo);
    SubPixel_Demo(0, 0);

    waitKey(0);
    return 0;
}

void SubPixel_Demo(int, void*) {
    if (max_corners < 5) {
        max_corners = 5;
    }
    vector<Point2f> corners;
    double qualityLevel = 0.01;
    double minDistance = 10;
    int blockSize = 3;
    double k = 0.04;
    //先做角點檢測
    goodFeaturesToTrack(gray_src, corners, max_corners, qualityLevel, minDistance, Mat(), blockSize, false, k);
    cout << "number of corners: " << corners.size() << endl;
    Mat resultImg = src.clone();
    for (size_t t = 0; t < corners.size(); t++) {
        circle(resultImg, corners[t], 2, Scalar(0, 0, 255), 2, 8, 0);
    }
    imshow(output_title, resultImg);
    
    //再找亞像素角點
    Size winSize = Size(5, 5);
    Size zerozone = Size(-1, -1);
    TermCriteria tc = TermCriteria(TermCriteria::EPS + TermCriteria::MAX_ITER, 40, 0.001);
    cornerSubPix(gray_src, corners, winSize, zerozone, tc);

    for (size_t t = 0; t < corners.size(); t++) {
        cout << (t + 1) << " .point[x, y] = " << corners[t].x << " , " << corners[t].y << endl;
    }
    return;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM