src/matching2D_Student.cpp
/* Detect key points using Harris corner detector */
Detect key points using Harris corner detector
Detect key points using Harris corner detector
double detKeypointsHarris(vector<cv::KeyPoint> &keypoints, cv::Mat &img, bool bVis)
{
    int blockSize = 2;     
    int apertureSize = 3;  
    int minResponse = 100; 
    double k = 0.04;       
    double t = (double)cv::getTickCount();
    cv::Mat dst, dst_norm, dst_norm_scaled;
    dst = cv::Mat::zeros(img.size(), CV_32FC1);
    cv::cornerHarris(img, dst, blockSize, apertureSize, k, cv::BORDER_DEFAULT);
    cv::normalize(dst, dst_norm, 0, 255, cv::NORM_MINMAX, CV_32FC1, cv::Mat());
    cv::convertScaleAbs(dst_norm, dst_norm_scaled);
    float maxOverlap = 0.0; 
    for (int j = 0; j < dst_norm.rows; j++)
    {
        for (int i = 0; i < dst_norm.cols; i++)
        {
            int response = (int)dst_norm.at<float>(j, i);
            if (response > minResponse)
            { 
                cv::KeyPoint newKeyPoint;
                newKeyPoint.pt = cv::Point2f(i, j);
                newKeyPoint.size = 2 * apertureSize;
                newKeyPoint.response = response;
                bool bOverlap = false;
                for (int k = 0; k < keypoints.size(); k++)
                {
                    float keyPtOverlap = cv::KeyPoint::overlap(newKeyPoint,keypoints.at(k));
                    if (keyPtOverlap > maxOverlap)
                    {
                        bOverlap = true;
                        if (newKeyPoint.response > keypoints.at(k).response)
                        {
                            keypoints.at(k) = newKeyPoint;
                            break;
                        }   
                    }  
                }
                if (!bOverlap)
                {                                     
                    keypoints.push_back(newKeyPoint); 
                }
            }
        } 
    }  
    t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
    cout << "Harris detection with n=" << keypoints.size() << " keypoints in " << 1000 * t / 1.0 << " ms" << endl;
    if (bVis)
    {
        cv::Mat visImage = img.clone();
        cv::drawKeypoints(img, keypoints, visImage, cv::Scalar::all(-1), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
        string windowName = "Harris Corner Detector Results";
        cv::namedWindow(windowName, 7);
        imshow(windowName, visImage);
        cv::waitKey(0);
    }   
    return 1000 * t / 1.0;
}
double detKeypointsHarris ( vector < cv :: KeyPoint > & keypoints , cv :: Mat & img , bool bVis ) { int blockSize = 2 ; int apertureSize = 3 ; int minResponse = 100 ; double k = 0.04 ; double t = ( double ) cv :: getTickCount ( ) ; cv :: Mat dst , dst_norm , dst_norm_scaled ; dst = cv :: Mat :: zeros ( img . size ( ) , CV_32FC1 ) ; cv :: cornerHarris ( img , dst , blockSize , apertureSize , k , cv :: BORDER_DEFAULT ) ; cv :: normalize ( dst , dst_norm , 0 , 255 , cv :: NORM_MINMAX , CV_32FC1 , cv :: Mat ( ) ) ; cv :: convertScaleAbs ( dst_norm , dst_norm_scaled ) ; float maxOverlap = 0.0 ; for ( int j = 0 ; j < dst_norm . rows ; j ++ ) { for ( int i = 0 ; i < dst_norm . cols ; i ++ ) { int response = ( int ) dst_norm . at < float > ( j , i ) ; if ( response > minResponse ) { cv :: KeyPoint newKeyPoint ; newKeyPoint . pt = cv :: Point2f ( i , j ) ; newKeyPoint . size = 2 * apertureSize ; newKeyPoint . response = response ; bool bOverlap = false ; for ( int k = 0 ; k < keypoints . size ( ) ; k ++ ) { float keyPtOverlap = cv :: KeyPoint :: overlap ( newKeyPoint , keypoints . at ( k ) ) ; if ( keyPtOverlap > maxOverlap ) { bOverlap = true ; if ( newKeyPoint . response > keypoints . at ( k ) . response ) { keypoints . at ( k ) = newKeyPoint ; break ; } } } if ( ! bOverlap ) { keypoints . push_back ( newKeyPoint ) ; } } } } t = ( ( double ) cv :: getTickCount ( ) - t ) / cv :: getTickFrequency ( ) ; cout << " " << keypoints . size ( ) << " " << 1000 * t / 1.0 << " " << endl ; if ( bVis ) { cv :: Mat visImage = img . clone ( ) ; cv :: drawKeypoints ( img , keypoints , visImage , cv :: Scalar :: all ( -1 ) , cv :: DrawMatchesFlags :: DRAW_RICH_KEYPOINTS ) ; string windowName = " " ; cv :: namedWindow ( windowName , 7 ) ; imshow ( windowName , visImage ) ; cv :: waitKey ( 0 ) ; } return 1000 * t / 1.0 ; }
Detect key points using Harris corner detector
Detect key points using Harris corner detector
// Detector parameters // for every pixel, a blockSize × blockSize neighborhood is considered // aperture parameter for Sobel operator (must be odd) // minimum value for a corner in the 8bit scaled response matrix // Harris parameter (see equation for details) // Detect Harris corners and normalize output // Look for prominent corners and instantiate keypoints // max. permissible overlap between two features in %, used during non-maxima suppression // only store points above a threshold // perform non-maximum suppression (NMS) in local neighbourhood around new key point /* code */ /* code */ /* code */ // only add new key point if no overlap has been found in previous NMS // store new keypoint in dynamic list // visualize results