HM Moniruzzaman
HM Moniruzzaman

Reputation: 155

How to extract keypoints from Harris Corner Detector using Opencv

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

    std::vector<KeyPoint> keypoints;

Upvotes: 6

Views: 5006

Answers (2)

Bilal Chandio
Bilal Chandio

Reputation: 89

Try my code:
import cv2
import numpy as np
from skimage.io import imread
from skimage.color import rgb2gray
import matplotlib.pyplot as plt

img = imread('images/box.jpg')
img_gray = rgb2gray(img)


img_gray = np.float32(img_gray)
#cv2.imshow("Image",img)
#cv2.imshow("Gray Image",img_gray)
#Ix = cv2.Sobel(img_gray,cv2.CV_64F,1,0,ksize=5)
#Iy = cv2.Sobel(img_gray,cv2.CV_64F,0,1,ksize=5)
kernel_x = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]])
kernel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
Ix = cv2.filter2D(img_gray,-1,kernel_x)
Iy = cv2.filter2D(img_gray,-1,kernel_y)
Ixx = Ix**2
Ixy = Ix*Iy
Iyy = Iy**2
#cv2.imshow("Ixx",Ixx)
#cv2.imshow("Iyy Image",Iyy)
#cv2.imshow("Ixy Image",Ixy)

# Loop through image and find our corners
k = 0.05

height = img_gray.shape[0]
width = img_gray.shape[1]
harris_response = []
window_size = 6
offset = int(window_size/2)
for y in range(offset, height-offset):
    for x in range(offset, width-offset):
        Sxx = np.sum(Ixx[y-offset:y+1+offset, x-offset:x+1+offset])
        Syy = np.sum(Iyy[y-offset:y+1+offset, x-offset:x+1+offset])
        Sxy = np.sum(Ixy[y-offset:y+1+offset, x-offset:x+1+offset])

        # Find determinant and trace, use to get corner response
        det = (Sxx * Syy) - (Sxy ** 2)
        trace = Sxx + Syy
        r = det - k * (trace ** 2)

        harris_response.append([x, y, r])
img_copy = np.copy(img)
thresh = 500
#sift = cv2.xfeatures2d.SIFT_create()
#kp,dc = sift.compute(img,None)
for response in harris_response:
    x, y, r = response
    if r > thresh:
        img_copy[y, x] = [255, 0, 0]

plt.imshow(img_copy)
cv2.waitKey(0)
plt.show()

Upvotes: 0

Roman Smirnov
Roman Smirnov

Reputation: 359

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.

Upvotes: 2

Related Questions