OpenCV FAST - too many features

dephinera picture dephinera · Dec 27, 2014 · Viewed 7.7k times · Source

I'm trying to extract features so I can later train a SVM which will be used in Android app. I'm using python to find and extract the features because it is easy to write and saves time. My problem is that I get too many features and I don't know how to get the best features only. I found that there is a method retainBest in the C++ API of OpenCV but I couldn't find it for python. Can you give an advise what to do?

This is the code I use:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('./positive_images/1.jpg',cv2.CV_LOAD_IMAGE_GRAYSCALE)
#img = cv2.resize(cv2.imread('./positive_images/3.png',cv2.CV_LOAD_IMAGE_GRAYSCALE), (100, 100))
#th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
ret,th3 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
cv2.imwrite("result1.jpg", th3)

img = th3

# Initiate FAST object with default values
fast = cv2.FastFeatureDetector()

# find and draw the keypoints
keypoints = fast.detect(img,None)
img2 = cv2.drawKeypoints(img, keypoints, color=(255,0,0))

cv2.imwrite('fast_true.png',img2)

# Disable nonmaxSuppression
fast.setBool('nonmaxSuppression',0)
keypoints = fast.detect(img,None)

print "Total Keypoints without nonmaxSuppression: ", len(keypoints)

img3 = cv2.drawKeypoints(img, keypoints, color=(255,0,0))
cv2.imwrite("result.jpg",img3)

The original image:

enter image description here

And the result image:

enter image description here

My goal is to detect a steering wheel.

Answer

JonasVautherin picture JonasVautherin · Dec 27, 2014

If you look at the documentation, you will see that you can set a threshold for your FAST detector:

FastFeatureDetector( int threshold=1, bool nonmaxSuppression=true, type=FastFeatureDetector::TYPE_9_16 );

Here, the default threshold is set to 1. In your code, try to set it to 40, and see the results, as follows:

fast = cv2.FastFeatureDetector(40)

You will find details about the meaning of the threshold here:

  1. Select a pixel p in the image which is to be identified as an interest point or not. Let its intensity be I_p
  2. ...
  3. ...
  4. Now the pixel p is a corner if there exists a set of n contiguous pixels in the circle (of 16 pixels) which are all brighter than I_p + t, or all darker than I_p − t. (Shown as white dash lines in the above image). n was chosen to be 12.