Linux 下 Opencv 的配置与安装
在Linux环境下安装OpenCV可以通过源码编译或使用包管理器。以下是通过源码编译安装OpenCV的步骤:
- 安装依赖项:
sudo apt-update
sudo apt-get 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的Git仓库:
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout <opencv-version> # 替换<opencv-version>为想要的版本号,例如4.5.2
- 克隆OpenCV额外模块的Git仓库:
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout <opencv_contrib-version> # 替换<opencv_contrib-version>为对应的版本号
- 编译OpenCV:
cd ..
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_contrib/modules \
-D BUILD_EXAMPLES=ON ..
make -j$(nproc) # 使用所有CPU核心进行编译
sudo make install
sudo ldconfig
- 验证安装:
pkg-config --modversion opencv4
如果您使用的是Python,您可能还需要设置PYTHONPATH环境变量:
export PYTHONPATH=/usr/local/lib/python3.6/dist-packages:$PYTHONPATH
请根据您的Python版本和OpenCV版本调整上述命令中的路径。
评论已关闭