Search code examples
feature-extractionkeypointcorner-detection

How to extract keypoints from Harris Corner Detector using Opencv


  • First I would use cv::cornerHarris() to detect the corners (which i could do easily).
  • Second I want to extract keypoints from Harris detector and store them in std::vector<KeyPoint> (which i have no idea how to do). I will use this later to calculate descriptors and match those.
  • I could do them using SURF quite easily but I want to do it using Harris corner detector.

    /// Detecting corners
    cv::cornerHarris(leftRoi, dst, blockSize, apertureSize, k, BORDER_DEFAULT);
    
    /// Normalizing
    normalize(dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
    convertScaleAbs(dst_norm, dst_norm_scaled);
    
    /// Drawing a circle around corners
    for (int j = 0; j < dst_norm.rows; j++)
    {
        for (int i = 0; i < dst_norm.cols; i++)
        {
            if ((int)dst_norm.at<float>(j, i) > 165)
            {
    
                circle(dst_norm_scaled, Point(i, j), 5, Scalar(0), 2, 8, 0);
            }
        }
    }
    /// Showing the result
    namedWindow("corners_window", CV_WINDOW_AUTOSIZE);
    imshow("corners_window", dst_norm_scaled);
    

-Having problem with this part (How do i extract the keypoints from above Harris detector)

    std::vector<KeyPoint> keypoints;

Solution

  • Python

    This is a how I wrote it in Python:

    # convert coordinates to Keypoint type
    eye_corner_keypoints = [cv2.KeyPoint(crd[0], crd[1], 13) for crd in eye_corner_coordinates]
    
    # compute SIFT descriptors from corner keypoints
    sift = cv2.xfeatures2d.SIFT_create()
    eye_corner_descriptors = [sift.compute(gray,[kp])[1] for kp in eye_corner_keypoints]
    

    C++

    Looking at the constructor signature in the OpenCV reference documentation for the KeyPoint class:

    KeyPoint (float x, float y, float _size, float _angle=-1, float _response=0, int _octave=0, int _class_id=-1)
    

    It looks like you can iterate through your coordinate points and instantiate your KeyPoint objects at each iteration (roughly) like so:

    for (int i = 0; i < num_points; i++) {
        KeyPoint kp(points_x[i], points_y[i], points_size[i]);
        /* ... */
    

    Warning: code is untested, I'm not a C++ programmer.