Shi-Tomasi角点检测

#define WINDOW_NAME "角点检测"

Mat g_srcImage, g_grayImage;
int g_maxCornerNumber = 40;
int g_maxTrackbarNumber = 500;
RNG g_rng(12345);//初始化随机数生成器

void on_GoodFeaturesToTrack(int, void*)
{
    if (g_maxCornerNumber <= 1) { g_maxCornerNumber = 1; }

    //Shi-Tomasi算法(goodFeaturesToTrack函数)的参数准备
    vector<Point2f>corners;
    double qualityLevel = 0.01;//角点检测可接受的最小特征值
    double minDistance = 10;//角点之间的最小距离
    int blockSize = 3;//计算导数自相关矩阵时指定的邻域范围
    double k = 0.04;//权重系数
    Mat copy = g_srcImage.clone();  //复制源图像到一个临时变量中,作为感兴趣区域

    //进行Shi-Tomasi角点检测
    goodFeaturesToTrack(g_grayImage, corners, g_maxCornerNumber, qualityLevel, minDistance,
        Mat(), blockSize, false, k);

    cout << "\t>此次检测到的角点数量为:" << corners.size() << endl;

    //绘制检测到的角点
    int r = 4;
    for (int i = 0; i < corners.size(); i++)
    {
        //以随机的颜色绘制出角点
        circle(copy, corners[i], r, Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255),
            g_rng.uniform(0, 255)), -1, 8, 0);
    }
    imshow(WINDOW_NAME, copy);

}

void opencv233(){
    g_srcImage = imread("conTest03.png", 1);
    //预处理,先高斯模糊再进行膨胀,去除线段边缘的毛刺
    cvtColor(g_srcImage, g_grayImage, CV_BGR2GRAY);
    GaussianBlur(g_grayImage, g_grayImage, Size(9, 9), 0, 0);
    Mat element = getStructuringElement(MORPH_RECT, Size(9, 9));
    dilate(g_grayImage, g_grayImage, element);

    namedWindow(WINDOW_NAME, CV_WINDOW_AUTOSIZE);
    createTrackbar("最大角点数", WINDOW_NAME, &g_maxCornerNumber, g_maxTrackbarNumber, on_GoodFeaturesToTrack);
    imshow(WINDOW_NAME, g_srcImage);
    on_GoodFeaturesToTrack(0, 0);

    waitKey(0);
}

你可能感兴趣的:(计算机视觉)