在Ubuntu上配置OpenCV可以通过以下步骤进行:
安装依赖项:
sudo apt update
sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
gfortran openexr libatlas-base-dev python3-dev python3-numpy \
libtbb2 libtbb-dev libdc1394-22-dev
下载OpenCV和OpenCV_contrib源码:
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
编译OpenCV:
cd ~/opencv_build/opencv
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
make -j$(nproc)
sudo make install
sudo ldconfig
配置Python环境:
cd ~/opencv_build/opencv/build/lib/python3/dist-packages/
sudo cp -r opencv_contrib-3.4.X/python_bindings_generator/. /usr/local/lib/python3.6/dist-packages/
sudo cp -r opencv_contrib-3.4.X/python_bindings_generator_boost_py36/. /usr/local/lib/python3.6/dist-packages/
验证安装:
import cv2
print(cv2.__version__)
请注意,上面的步骤是基于OpenCV 3.4.X的安装。如果你需要安装OpenCV的其他版本,请替换相应的版本号。