Python+OpenCV:图像快速角点检测算法(FAST Algorithm for Corner Detection)
理论
Feature Detection using FAST
- Select a pixel p in the image which is to be identified as an interest point or not. Let its intensity be Ip.
- Select appropriate threshold value t.
-
Consider a circle of 16 pixels around the pixel under test. (See the image below).
-
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 Ip+t, or all darker than Ip−t. (Shown as white dash lines in the above image). n was chosen to be 12.
-
A high-speed test was proposed to exclude a large number of non-corners. This test examines only the four pixels at 1, 9, 5 and 13 (First 1 and 9 are tested if they are too brighter or darker. If so, then checks 5 and 13). If p is a corner, then at least three of these must all be brighter than Ip+t or darker than Ip−t. If neither of these is the case, then p cannot be a corner. The full segment test criterion can then be applied to the passed candidates by examining all pixels in the circle. This detector in itself exhibits high performance, but there are several weaknesses:
- It does not reject as many candidates for n < 12.
- The choice of pixels is not optimal because its efficiency depends on ordering of the questions and distribution of corner appearances.
- Results of high-speed tests are thrown away.
- Multiple features are detected adjacent to one another.
First 3 points are addressed with a machine learning approach. Last one is addressed using non-maximal suppression.
Machine Learning a Corner Detector
- Select a set of images for training (preferably from the target application domain)
- Run FAST algorithm in every images to find feature points.
- For every feature point, store the 16 pixels around it as a vector. Do it for all the images to get feature vector P.
-
Each pixel (say x) in these 16 pixels can have one of the following three states:
- Depending on these states, the feature vector P is subdivided into 3 subsets, Pd, Ps, Pb.
- Define a new boolean variable, Kp, which is true if p is a corner and false otherwise.
- Use the ID3 algorithm (decision tree classifier) to query each subset using the variable Kp for the knowledge about the true class. It selects the x which yields the most information about whether the candidate pixel is a corner, measured by the entropy of Kp.
- This is recursively applied to all the subsets until its entropy is zero.
- The decision tree so created is used for fast detection in other images.
Non-maximal Suppression
Detecting multiple interest points in adjacent locations is another problem. It is solved by using Non-maximum Suppression.
- Compute a score function, V for all the detected feature points. V is the sum of absolute difference between p and 16 surrounding pixels values.
- Consider two adjacent keypoints and compute their V values.
- Discard the one with lower V value.
FAST Feature Detector in OpenCV
####################################################################################################
# 图像快速角点检测算法(FAST Algorithm for Corner Detection)
def lmc_cv_image_fast_corner_detection():
"""
函数功能: 图像快速角点检测算法(FAST Algorithm for Corner Detection)。
"""
stacking_images = []
image_file_name = ['D:/99-Research/Python/Image/Butterfly01.jpg',
'D:/99-Research/Python/Image/Butterfly02.jpg',
'D:/99-Research/Python/Image/Butterfly03.jpg',
'D:/99-Research/Python/Image/Butterfly04.jpg']
for i in range(len(image_file_name)):
# 读取图像
image = lmc_cv.imread(image_file_name[i])
image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2RGB)
gray_image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2GRAY)
# Initiate FAST object with default values
fast = lmc_cv.FastFeatureDetector_create()
# find and draw the keypoints
keypoints = fast.detect(gray_image, None)
result_image1 = lmc_cv.drawKeypoints(gray_image, keypoints, None, color=(255, 0, 0),
flags=lmc_cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Print all default params
print("Threshold: {}".format(fast.getThreshold()))
print("nonmaxSuppression:{}".format(fast.getNonmaxSuppression()))
print("neighborhood: {}".format(fast.getType()))
print("Total Keypoints with nonmaxSuppression: {}".format(len(keypoints)))
# Disable nonmaxSuppression
fast.setNonmaxSuppression(0)
kp = fast.detect(gray_image, None)
print("Total Keypoints without nonmaxSuppression: {}".format(len(kp)))
result_image2 = lmc_cv.drawKeypoints(gray_image, keypoints, None, color=(255, 0, 0),
flags=lmc_cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# stacking images side-by-side
stacking_image = np.hstack((image, result_image1, result_image2))
stacking_images.append(stacking_image)
# 显示图像
for i in range(len(stacking_images)):
pyplot.figure('FAST Algorithm for Corner Detection %d' % (i + 1))
pyplot.subplot(1, 1, 1)
pyplot.imshow(stacking_images[i], 'gray')
pyplot.title('FAST Algorithm for Corner Detection')
pyplot.xticks([])
pyplot.yticks([])
pyplot.show()
# 根据用户输入保存图像
if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
# 销毁窗口
pyplot.close('all')
return