danglingfarpointer's memoization

仕事周りでの気付き、メモ、愚痴などを書いていきます。

OpenCV 3でPythonからSIFT

OpenCV 3では、ライセンスやパテント上の理由だろうか、SIFTやSURFはopencv_contribという別モジュールに分かれてしまっている。

opencv_contribを使うには、インストールの際に--with-contribオプションを付ける必要があるようだ。Mac OSだとHomebrewで以下のようにインストールできる。

brew install python
brew install opencv3 --with-contrib
brew link opencv3 --force

これでSIFTが使えるようになる。

# -*- coding: utf-8 -*-

import cv2

def main():
    img = cv2.imread('image.jpg', 0)

    # detector = cv2.FeatureDetector_create('ORB')
    detector = cv2.xfeatures2d.SIFT_create()
    keypoints = detector.detect(img)

    out = cv2.drawKeypoints(img, keypoints, None)

    cv2.imshow('sift', out)
    cv2.waitKey(0)

if __name__ == '__main__':
    main()