Python+OpenCV:图像快速角点检测算法(FAST Algorithm for Corner Detection)

82 篇文章 20 订阅

Python+OpenCV:图像快速角点检测算法(FAST Algorithm for Corner Detection)

理论

Feature Detection using FAST

  1. Select a pixel p in the image which is to be identified as an interest point or not. Let its intensity be Ip.
  2. Select appropriate threshold value t.
  3. Consider a circle of 16 pixels around the pixel under test. (See the image below).

  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 Ip+t, or all darker than Ip−t. (Shown as white dash lines in the above image). n was chosen to be 12.

  5. 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

  1. Select a set of images for training (preferably from the target application domain)
  2. Run FAST algorithm in every images to find feature points.
  3. For every feature point, store the 16 pixels around it as a vector. Do it for all the images to get feature vector P.
  4. Each pixel (say x) in these 16 pixels can have one of the following three states:

  5. Depending on these states, the feature vector P is subdivided into 3 subsets, Pd, Ps, Pb.
  6. Define a new boolean variable, Kp, which is true if p is a corner and false otherwise.
  7. 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.
  8. This is recursively applied to all the subsets until its entropy is zero.
  9. 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.

  1. 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.
  2. Consider two adjacent keypoints and compute their V values.
  3. 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值