Feature extraction and detection 1-Harris corner detection

Harris corner detection

    Harris corner detection theory (proposed in 1998)

    Parameter Description

    code demo

Harris corner detection theory

   The recognition of the corners of the human eye is usually done in a small local area or small window. If the small window of this feature is moved in all directions, and the gray level of the area within the window changes greatly, then it is considered that a corner point is encountered in the window. If the specific window moves in all directions of the image, the grayscale of the image in the window does not change, then there is no corner point in the window; if the window moves in a certain direction, the grayscale of the image in the window changes. Large changes, but no changes in other directions, then the image in the window may be a straight line segment. As shown below:

Among them, I(x,y) represents the gray value of the pixel

-blockSize – the size of the matrix when calculating

-Ksize window size

-K indicates the parameter size when calculating the angle response, the default is 0.04~0.06

- Threshold t, used to filter the angular response

Corner detection code

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


using namespace cv;
using namespace std;
Mat src, gray_src;
int thresh = 130;
int max_count = 255;
const char* output_title = "HarrisCornerDetection Result";
void Harris_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);


    namedWindow(output_title, CV_WINDOW_AUTOSIZE);
    cvtColor(src, gray_src, COLOR_BGR2GRAY);
    createTrackbar("Threshold:", output_title, &thresh, max_count, Harris_Demo);
    Harris_Demo(0, 0);


    waitKey(0);
    return 0;
}


void Harris_Demo(int, void*) {
    Mat dst, norm_dst, normScaleDst;
    dst = Mat::zeros(gray_src.size(), CV_32FC1);


    int blockSize = 2;
    int ksize = 3;
    double k = 0.04;
    cornerHarris(gray_src, dst, blockSize, ksize, k, BORDER_DEFAULT);
    normalize(dst, norm_dst, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
    convertScaleAbs(norm_dst, normScaleDst);


    Mat resultImg = src.clone();
    for (int row = 0; row < resultImg.rows; row++) {
        uchar* currentRow = normScaleDst.ptr(row);
        for (int col = 0; col < resultImg.cols; col++) {
            int value = (int)*currentRow;
            if (value > thresh) {
                circle(resultImg, Point(col, row), 2, Scalar(0, 0, 255), 2, 8, 0);
            }
            currentRow++;
        }
    }


    imshow(output_title, resultImg);
}

personal self-study code

void DlgImageProcessing::on_btnHarris_clicked()
{       
    if (m_srcImage.data)       
    {              
        Mat grayImage;              
        if (m_srcImage.type() != CV_8UC1)              
        {                     
            cvtColor(m_srcImage, grayImage, COLOR_RGB2GRAY);              
        }              
        else              
        {                     
            grayImage = m_srcImage.clone();              
        }              
        Mat dstImage = Mat::zeros(grayImage.size(), CV_32FC1);             
        //harris角点核心函数  
        int blockSize = 2;//矩阵大小              
        int ksize = 3;//窗口大小              
        int k = 0.04;//计算角度响应时候的参数大小,默认在0.04~0.06  
        cornerHarris(grayImage, dstImage, blockSize, ksize, k,  BORDER_DEFAULT);                              
        //上述输出的取值范围并不是0-255 需要按照最大最小值进行归一化              
        Mat normImage, normScaleDst;              
        normalize(dstImage, normImage, 0, 255, NORM_MINMAX, CV_32FC1,  Mat()); 
        convertScaleAbs(normImage, normScaleDst);              
        Mat resultImg = m_srcImage.clone();                   
        int thresholdValue = ui.horizontalSliderThresholdValue->value();           
        RNG rng(12345);              //用彩色来显示             
        for (int row = 0; row < resultImg.rows; row++)             
        {                     
            //定义每一行的指针                    
             uchar* currentRow = normScaleDst.ptr(row);                     
            for (int col = 0; col < resultImg.cols; col++)                     
            {                           
                int value = (int)*currentRow;                           
                if (value > thresholdValue)                          
                {                                  
                    circle(resultImg, Point(col, row), 2,  Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 2, 8, 0);                          
                 }                           
                currentRow++;                     
            }             
         }             
         emit ShowImage(resultImg);              
        cvtColor(resultImg, resultImg, COLOR_RGB2BGR);              
        cv::imshow("this result", resultImg);       
    }
}

Guess you like

Origin blog.csdn.net/u013480226/article/details/122665514#comments_20034487