File: harris.py

package info (click to toggle)
python-imutils 0.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,652 kB
  • sloc: python: 1,178; makefile: 3
file content (26 lines) | stat: -rwxr-xr-x 874 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import cv2
import numpy as np
from .helpers import corners_to_keypoints


class HARRIS:
    def __init__(self, blockSize=2, apertureSize=3, k=0.1, T=0.02):
        self.blockSize = blockSize
        self.apertureSize = apertureSize
        self.k = k
        self.T = T

    def detect(self, img):
        # convert our input image to a floating point data type and then
        # compute the Harris corner matrix
        gray = np.float32(img)
        H = cv2.cornerHarris(gray, self.blockSize, self.apertureSize, self.k)

        # for every (x, y)-coordinate where the Harris value is above the
        # threshold, create a keypoint (the Harris detector returns
        # keypoint size a 3-pixel radius)
        kps = np.argwhere(H > self.T * H.max())
        kps = [cv2.KeyPoint(pt[1], pt[0], 3) for pt in kps]

        # return the Harris keypoints
        return kps