posted Sep 27, 2019, 12:37 AM by Teng-Yok Lee
[
updated Jan 2, 2021, 7:19 PM
]
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- Create a folder to hold both source code and build folder. For instance, G:/src/opencv/.
- Clone OpenCV source code from https://github.com/Itseez/opencv.git to G:/src/opencv/src.
- Check out the tag to 3.2.0.
- Clone extra module's source code from https://github.com/opencv/opencv_contrib to G:/src/opencv/contrib.
- Check out the tag to 3.2.0.
- Open cmake.
- Set the source folder as G:/src/opencv/src.
- Set the binary folder as G:/src/opencv/build/win32/vs2013/x64.
- Click Configure, and then
- Turn OFF: BUILD_WITH_STATIC_CRT
- Turn OFF: BUILD_SHARED_LIBS
- Turn OFF: BUILD_PERF_TESTS
- Turn OFF: BUILD_TESTS
- Turn OFF: WITH_CUDA
- Turn OFF: WITH_CUFFT
- Turn OFF: WITH_LAPACK
- Turn OFF: BUILD_opencv_bioinspired
- Turn ON: BUILD_ZLIB
- Set OPENCV_EXTRA_MODULES_PATH: G:/src/opencv/contrib/modules
- Set CMAKE_INSTALL_PREFIX to where you want to install OpenCV
- Also configure with Python2. I am using WinPython, which is installed under G:/usr/win32/WinPython-64bit-2.7.10.3/.
- Set PYTHON2_EXECUTABLE: G:/usr/win32/WinPython-64bit-2.7.10.3/python-2.7.10.amd64/python.exe
- Set PYTHON2_INCLUDE_DIR: G:/usr/win32/WinPython-64bit-2.7.10.3/python-2.7.10.amd64/include
- Set PYTHON2_LIBRARY: G:/usr/win32/WinPython-64bit-2.7.10.3/python-2.7.10.amd64/libs/python27.lib
- Set PYTHON2_PACKAGES_PATH: G:/usr/win32/WinPython-64bit-2.7.10.3/python-2.7.10.amd64/Lib/site-packages
- 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
- Configure.
- Generate the Visual Studio projects/solutions.
- Open H:/src/opencv/build/vs2013/x64/opencv.sln.
- Build all projects.
- Build INSTALL. Note that it will try to overwrite cv2.pyd in the site-package folder of python. Backup first if needed.
Ubuntu- Create a folder to hold both source code and build folder. For instance, ~/src/opencv/.
- Clone OpenCV source code from https://github.com/Itseez/opencv.git to ~/src/opencv/src.
- Check out the tag to 3.0.0.
- Clone extra module's source code from https://github.com/opencv/opencv_contrib to ~/src/opencv/contrib.
- Check out the tag to 3.0.0.
- 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
|
|