HCRM博客

如何在CentOS系统上安装OpenCV?

在CentOS上安装OpenCV涉及多个步骤,包括安装必要的依赖包、下载和编译OpenCV源码以及配置环境变量,以下是详细的指南:

一、安装依赖包

1、安装系统依赖

如何在CentOS系统上安装OpenCV?-图1
(图片来源网络,侵权删除)
   sudo yum y install epelrelease
   sudo yum y install gcc gccc++ kerneldevel gccessential gccgfortran
   sudo yum y install cmake make bzip2 zip unzip
   sudo yum y install libjpegturbodevel libtiffdevel libpngdevel libv4ldevel
   sudo yum y install openexrdevel gtk2devel gtk3devel
   sudo yum y install libavcodecdevel libavformatdevel libswscaledevel libdc1394devel libtheoradevel
   sudo yum y install libvorbisdevel xvidcoredevel x264devel frei0rdevel sphinxdevel
   sudo yum y install flacdevel opusdevel og   sudo yum y install libaomdevel libvpxdevel ffmpegdevel nasm yasm
   sudo yum y install python3devel numpy

2、安装FFmpeg

   sudo yum localinstall nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusionfreerelease7.noarch.rpm
   sudo yum localinstall nogpgcheck https://download1.rpmfusion.org/nonfree/el/rpmfusionnonfreerelease7.noarch.rpm
   rpm import http://li.nux.ro/download/nux/RPMGPGKEYnux.ro
   sudo yum y install ffmpeg ffmpegdevel

二、下载和编译OpenCV

1、下载OpenCV源码

   cd /opt
   wget https://github.com/opencv/opencv/archive/4.5.0.zip
   unzip 4.5.0.zip
   mv opencv4.5.0 opencv
   cd opencv
   git clone https://github.com/opencv/opencv_contrib.git

2、创建构建目录并进入

   mkdir build
   cd build

3、配置编译选项

   cmake D CMAKE_BUILD_TYPE=Release \
         D CMAKE_INSTALL_PREFIX=/usr/local \
         D INSTALL_C_EXAMPLES=ON \
         D INSTALL_PYTHON_EXAMPLES=ON \
         D BUILD_EXAMPLES=ON \
         D OPENCV_GENERATE_PKGCONFIG=ON \
         D WITH_TBB=ON \
         D WITH_GTK=ON \
         D ENABLE_FAST_MATH=1 \
         D CUDA_FAST_MATH=1 \
         ..

4、编译和安装

   make j$(nproc)
   sudo make install
   sudo ldconfig

三、配置环境变量

1、添加库路径

如何在CentOS系统上安装OpenCV?-图2
(图片来源网络,侵权删除)
   sudo ln s /usr/local/lib64/pkgconfig/opencv4.pc /usr/share/pkgconfig/
   sudo sh c 'echo "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig:$PKG_CONFIG_PATH" >> /etc/profile'
   sudo sh c 'echo "export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH" >> /etc/profile'
   source /etc/profile

2、验证安装

   pkgconfig modversion opencv4

四、使用OpenCV编写C++图像处理程序

1、创建项目结构

   mkdir ~/opencv_projects
   cd ~/opencv_projects
   mkdir my_image_processor
   cd my_image_processor

2、编写CMakeLists.txt

   cmake_minimum_required(VERSION 3.1)
   project(MyImageProcessor)
   find_package(OpenCV REQUIRED)
   include_directories(${OpenCV_INCLUDE_DIRS})
   add_executable(my_image_processor main.cpp)
   target_link_libraries(my_image_processor ${OpenCV_LIBS})

3、编写main.cpp

   #include <opencv2/opencv.hpp>
   #include <iostream>
   int main() {
       cv::Mat image = cv::imread("path_to_your_image.jpg");
       if (image.empty()) {
           std::cout << "Could not read the image" << std::endl;
           return 1;
       }
       cv::imshow("Display window", image);
       cv::waitKey(0);
       return 0;
   }

4、编译和运行

如何在CentOS系统上安装OpenCV?-图3
(图片来源网络,侵权删除)
   mkdir build
   cd build
   cmake ..
   make
   ./my_image_processor

五、相关FAQ问答

Q1: 如何更改OpenCV的安装路径?

A1: 在执行cmake命令时,使用D CMAKE_INSTALL_PREFIX选项来指定安装路径。D CMAKE_INSTALL_PREFIX=/custom/path

Q2: 如果编译过程中出现错误怎么办?

A2: 确保所有依赖项都已正确安装,并且检查cmake输出以确定具体的错误信息,常见的问题包括缺少开发包或路径配置错误,根据错误信息调整配置并重新编译。

Q3: 如何更新OpenCV到最新版本?

A3: 访问OpenCV官网或GitHub仓库,下载最新版本的源码,然后按照上述步骤重新编译和安装,确保先卸载旧版本以避免冲突。

本站部分图片及内容来源网络,版权归原作者所有,转载目的为传递知识,不代表本站立场。若侵权或违规联系Email:zjx77377423@163.com 核实后第一时间删除。 转载请注明出处:https://blog.huochengrm.cn/pc/21619.html

分享:
扫描分享到社交APP
上一篇
下一篇