OpenCV之图像特征提取与检测(六) 自定义角点检测器_ShiTomasi

基于Shi-Tomasi角点检测

void cornerMinEigenVal( //shi-tomasi时计算 λ1 λ2 ,参数与tomasi角点检测一致
    InputArray src, 
    OutputArray dst, 
    int blockSize, 
    int ksize = 3, 
    int borderType = BORDER_DEFAULT 
);

代码

    #include "../common/common.hpp"

    static Mat src, gray, tomasiRspImg;
    static const char title_tomasi[] = "corner_detect_tomasi";
    static int thresh_v_tomasi = 30;
    static int thresh_max_tomasi = 100;
    static RNG rng(12345);
    static double tomasi_min_rsp; // tomasi 角度响应最小值
    static double tomasi_max_rsp; // tomasi 角度响应最大值

    static void corner_detect_tomasi(int, void*);

    void main(int argc, char** argv)
    {
        src = imread(getCVImagesPath("images/home.jpg"), IMREAD_COLOR);
        imshow("src2-7", src);

        cvtColor(src, gray, CV_BGR2GRAY);
        int blockSize = 3;
        int ksize = 3;
        double k = 0.04;
        tomasiRspImg = Mat::zeros(src.size(), CV_32FC1);
        //计算最小特征值,即ShiTomasi角度响应值 R = min(λ1, λ2)
        cornerMinEigenVal(gray, tomasiRspImg, blockSize, ksize, BORDER_DEFAULT);
        minMaxLoc(tomasiRspImg, &tomasi_min_rsp, &tomasi_max_rsp, 0, 0, Mat());//寻找tomasiRspImg中角度响应最大值,最小值,及它们所在的位置,位置参数传0表示不管
        printf("tomasi_min_rsp=%f, tomasi_max_rsp=%f\n", tomasi_min_rsp, tomasi_max_rsp);//tomasi_min_rsp=-0.000000, tomasi_max_rsp=0.101390

        namedWindow(title_tomasi, CV_WINDOW_AUTOSIZE);
        createTrackbar("tomasi thresh:", title_tomasi, &thresh_v_tomasi, thresh_max_tomasi, corner_detect_tomasi);
        corner_detect_tomasi(0, 0);

        waitKey(0);
    }

    void corner_detect_tomasi(int, void*)
    {
        if (thresh_v_tomasi < 20) thresh_v_tomasi = 20;
        Mat retImg = gray.clone();
        cvtColor(retImg, retImg, CV_GRAY2BGR); // 让原图灰度,角点彩色
        //定义阈值, 以 thresh_v_tomasi / thresh_max_tomasi 的比例显示角点
        float t = tomasi_min_rsp + ((double)thresh_v_tomasi / thresh_max_tomasi)*(tomasi_max_rsp - tomasi_min_rsp);
        for (int row = 0; row < retImg.rows; row++)
        {
            for (int col = 0; col < retImg.cols; col++)
            {
                float v = tomasiRspImg.at<float>(row, col); // shiTomasi角点检测出的角度响应值有很多为0,所以减少了circle绘制,所以程序速度比harris要快
                if (v > t)
                {
                    circle(retImg, Point(col, row), 2, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 2, 8, 0);
                }
            }
        }
        imshow(title_tomasi, retImg);
    }

效果图

这里写图片描述

猜你喜欢

转载自blog.csdn.net/huanghuangjin/article/details/81263778