Skip to content

Instantly share code, notes, and snippets.

@garybradski
Created May 3, 2020 03:11
Show Gist options
  • Save garybradski/11e092a86d6ff99b0bc8548c8f76db4b to your computer and use it in GitHub Desktop.
Save garybradski/11e092a86d6ff99b0bc8548c8f76db4b to your computer and use it in GitHub Desktop.
Use of cv2.FastFeatureDetector
import numpy as np
import cv2
def fast_corners(img,border_buffer=0):
'''
Use the fast corner detector to find (x,y) integer corner locations
:param img_resized:
:param border_buffer: If >0, prune away kepoints w/in this distance of the boarder
:return: key point corner locations: list of int cv2.KeyPoints
'''
fast = cv2.FastFeatureDetector_create()
fast.setNonmaxSuppression(True)
fast.setThreshold(15)
kp_non_max = fast.detect(img, None) # List of cv2.KeyPoints
kp_non_max = (cv2.KeyPoint_convert(kp_non_max)).astype(np.int) # float np.array of Nx2 (array of x,y points)
if border_buffer > 0:
kp_non_max = prune_out_of_bound_kp(kp_non_max,img,border_buffer,border_buffer)
return kp_non_max #.astype(np.int)
def prune_out_of_bound_kp(kp,img,max_offset=30,min_offset=30):
'''
Prune keypoings kp that will go out of bounds if a min or max offset is added to them
NOTE: min_offsets are most offten negative, so you add them below to test if the result is > 0
:param kp: cv2.points in a kp list from say Fast corner detection
:param img: image you'll be working on to establish the bounds
:param max_offset: max offset that could be applied to any kp
:param min_offset: min offset that could be applied to any kp
:return: filtered list of keypoints that will not go out of image bounds
'''
h,w,c = img.shape
kp_inbounds = []
for pt in kp:
x = pt[0]
x_min = x - min_offset
x_max = x + max_offset
if x_min < 0 or x_max >= w:
continue
y = pt[1]
y_min = y - min_offset
y_max = y + max_offset
if y_min < 0 or y_max >= h:
continue
kp_inbounds.append(pt)
return kp_inbounds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment