Memo‎ > ‎

Build OpenCV with SIFT

posted Sep 27, 2019, 12:37 AM by Teng-Yok Lee

Since version 3.0, OpenCV source code remove the parts for feature matching with SIFT/SURF. This guide is about how to build OpenCV with them. It will indicate how to build the python binding.

Windows

  1. Create a folder to hold both source code and build folder. For instance, G:/src/opencv/.
  2. Clone OpenCV source code from https://github.com/Itseez/opencv.git to G:/src/opencv/src.
  3. Check out the tag to 3.2.0.
  4. Clone extra module's source code from https://github.com/opencv/opencv_contrib to G:/src/opencv/contrib.
  5. Check out the tag to 3.2.0.
  6. Open cmake.
    1. Set the source folder as G:/src/opencv/src.
    2. Set the binary folder as  G:/src/opencv/build/win32/vs2013/x64.
  7. Click Configure, and then
    1. Turn OFF: BUILD_WITH_STATIC_CRT
    2. Turn OFF: BUILD_SHARED_LIBS
    3. Turn OFF: BUILD_PERF_TESTS
    4. Turn OFF: BUILD_TESTS
    5. Turn OFF: WITH_CUDA
    6. Turn OFF: WITH_CUFFT
    7. Turn OFF: WITH_LAPACK
    8. Turn OFF: BUILD_opencv_bioinspired
    9. Turn ON: BUILD_ZLIB
    10. Set OPENCV_EXTRA_MODULES_PATH: G:/src/opencv/contrib/modules
    11. Set CMAKE_INSTALL_PREFIX to where you want to install OpenCV
  8. Also configure with Python2. I am using WinPython, which is installed under G:/usr/win32/WinPython-64bit-2.7.10.3/.
    1. Set PYTHON2_EXECUTABLE: G:/usr/win32/WinPython-64bit-2.7.10.3/python-2.7.10.amd64/python.exe
    2. Set PYTHON2_INCLUDE_DIR: G:/usr/win32/WinPython-64bit-2.7.10.3/python-2.7.10.amd64/include
    3. Set PYTHON2_LIBRARY: G:/usr/win32/WinPython-64bit-2.7.10.3/python-2.7.10.amd64/libs/python27.lib
    4. Set PYTHON2_PACKAGES_PATH: G:/usr/win32/WinPython-64bit-2.7.10.3/python-2.7.10.amd64/Lib/site-packages
    5. Set PYTHON2_NUMPY_INCLUDE_DIRS: G:/usr/win32/WinPython-64bit-2.7.10.3/python-2.7.10.amd64/Lib/site-packages/numpy/core/include
  9. Configure.
  10. Generate the Visual Studio projects/solutions.
  11. Open H:/src/opencv/build/vs2013/x64/opencv.sln.
  12. Build all projects.
  13. Build INSTALL. Note that it will try to overwrite cv2.pyd in the site-package folder of python. Backup first if needed.

Ubuntu

  1. Create a folder to hold both source code and build folder. For instance, ~/src/opencv/.
  2. Clone OpenCV source code from https://github.com/Itseez/opencv.git to ~/src/opencv/src.
  3. Check out the tag to 3.0.0.
  4. Clone extra module's source code from https://github.com/opencv/opencv_contrib to ~/src/opencv/contrib.
  5. Check out the tag to 3.0.0.
  6. See commands below.
# Assume that current folder is ~/src/opencv/build] $ mkdir linux $ cd linux # Use cmake to configure the files. # Note: Do not use ccmake here! # Otherwise it cannot configure python correctly # and thus will not build cv2.so. $ cmake \ -DBUILD_SHARED_LIBS=OFF \ -DBUILD_PERF_TESTS=OFF \ -DBUILD_TESTS=OFF \ -DWITH_CUDA=OFF \ -DWITH_CUFFT=OFF \ -DWITH_OPENCL=OFF \ -DBUILD_ZLIB=ON \ -DOPENCV_EXTRA_MODULES_PATH=/homes/tlee/src/opencv/contrib/modules \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_PREFIX=/homes/tlee/usr/linux/opencv \ -DWITH_LAPACK=OFF \ ../../src/ $ make -j8 $ make install # NOTE: Different from Windows, # cv2.pyd will be copied to /homes/tlee/usr/linux/opencv/lib/python2.7/dist-packages # because CMAKE_INSTALL_PREFIX is /homes/tlee/usr/linux/opencv.

Test?

# First set PYTHONPATH # by putting the path to the new-built cv2.so # before original paths. $ export PYTHONPATH=$PWD/lib:$PYTHONPATH $ python >>> import cv2; # Confirm the path. >>> cv2.__file__ # Confirm that SIFT can be used. >>> sift = cv2.xfeatures2d.SIFT_create(); # Also confirm that videos can be opened. # Replace the string by the file path of any video on your computer. >>> cap = cv2.VideoCapture('/data/datasets/tmp/RecFile_6_20170122_153530_PointGrey2_output.mp4') >>> cap.isOpened() True

Comments