centos上opencv安装

系统:centos7-没界面只能安装源码版
python:3.7

1下载opencv和opencv-contrib源码包

我安装的是最新版4.4,也可以通过git下载,但是我的网比较慢所以在windows上提前下好,下载地址
安装过程参考了这个博客,地址

2安装步骤

1.安装依赖包

sudo yum -y install epel-release
sudo yum -y install git gcc gcc-c++ cmake3
sudo yum install -y python37 python37-devel python37-pip
sudo yum install -y python python-devel python-pip
sudo yum -y install python-devel numpy python37-numpy
sudo yum -y install gtk2-devel
sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
sudo yum install -y ffmpeg
sudo yum install -y ffmpeg-devel
sudo yum install -y libpng-devel
sudo yum install -y jasper-devel
sudo yum install -y openexr-devel
sudo yum install -y libwebp-devel
sudo yum -y install libjpeg-turbo-devel
sudo yum install -y freeglut-devel mesa-libGL mesa-libGL-devel
sudo yum -y install libtiff-devel
sudo yum -y install libdc1394-devel
sudo yum -y install tbb-devel eigen3-devel
sudo yum -y install boost boost-thread boost-devel
sudo yum -y install libv4l-devel
sudo yum -y install gstreamer-plugins-base-devel

安装过程中,yum会提示有一些包没有
那就换一种安装方法,比如gstreamerXXXX就没有,我以为是版本的问题,换了好多都不对。
换了很多名字都不行你就这样安装

yum install  gstreamer*

因为可能是这个包的名字变了,所以安装不上。
2.进入虚拟环境,我之前创建过了就直接进了

source activate 环境名

3.进入opencv包下构建目录

cd /XXX/opencv
mkdir build && cd build

4.编译

cmake3 -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=$cwd/installation/OpenCV-"$cvVersion" \ -D ENABLE_PRECOMPILED_HEADERS=OFF \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D WITH_TBB=ON \ -D WITH_1394=OFF \ -D WITH_V4L=ON \ -D OPENCV_SKIP_PYTHON_LOADER=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D OPENCV_PYTHON3_INSTALL_PATH=/usr/local/anaconda/anaconda3/lib/python3.7/site-packages \ -D BUILD_opencv_python3=ON \ -D HAVE_opencv_python3=ON \ -D OPENCV_PYTHON2_INSTALL_PATH=OFF \ -D WITH_QT=OFF \ -D WITH_OPENGL=OFF \ -D WITH_GSTREAMER = ON\ -D WITH_FFMPEG=OFF \ -D PYTHON_DEFAULT_EXECUTABLE=/usr/local/anaconda/anaconda3/bin/python3 \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ -D ENABLE_CXX11=ON \ -D BUILD_EXAMPLES=ON .. 

需要哪个参数,哪个参数就给ON,不需要的就OFF.
地址根据你自己的修改一下

5.编译遇到的问题
cmake时总使用python2的环境,centos上opencv安装_第1张图片
如果想换到python3,就要先把python屏蔽掉,cmake结束之后再弄回来。

sudo mv /usr/bin/python2.7 /usr/bin/pythonNO-temp
sudo mv /usr/bin/pythonNO-temp /usr/bin/python2.7

yum需要python2,所以不能轻易卸载python2.

编译过程中会出现缺少文件的问题,参考。另外还有一个更详细的链接找不到了。
cmake编译得到的后半部分是这样的

  OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching ts video videoio
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 java js python2
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 
--   GUI: 
--     GTK+:                        YES (ver 3.22.30)
--       GThread :                  YES (ver 2.56.1)
--       GtkGlExt:                  NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /lib64/libz.so (ver 1.2.7)
--     JPEG:                        /lib64/libjpeg.so (ver 62)
--     WEBP:                        /lib64/libwebp.so (ver encoder: 0x0201)
--     PNG:                         /lib64/libpng.so (ver 1.5.13)
--     TIFF:                        /lib64/libtiff.so (ver 42 / 4.0.3)
--     JPEG 2000:                   /lib64/libjasper.so (ver 1.900.1)
--     OpenEXR:                     /usr/lib64/libImath.so /usr/lib64/libIlmImf.so /usr/lib64/libIex.so /usr/lib64/libHalf.so /usr/lib64/libIlmThread.so (ver 1.7.1)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.2)
--     FFMPEG:                      YES
--       avcodec:                   YES (58.91.100)
--       avformat:                  YES (58.45.100)
--       avutil:                    YES (56.51.100)
--       swscale:                   YES (5.7.100)
--       avresample:                NO
--     GStreamer:                   YES (1.10.4)
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2020.0.0 Gold [2020.0.0]
--            at:                   /root/opencv_build/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2020.0.0)
--               at:                /root/opencv_build/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.3.7)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /root/opencv_build/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /usr/local/anaconda/anaconda3/bin/python3 (ver 3.7)
--     Libraries:                   /usr/local/anaconda/anaconda3/lib/libpython3.7m.so (ver 3.7.0)
--     numpy:                       /usr/local/anaconda/anaconda3/lib/python3.7/site-packages/numpy/core/include (ver 1.15.1)
--     install path:                lib/python3.7/site-packages/cv2/python-3.7
-- 
--   Python (for build):            /usr/local/anaconda/anaconda3/bin/python3
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         /usr/local/jdk/include /usr/local/jdk/include/linux /usr/local/jdk/include
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /root/opencv_build/opencv/build

据说看到有Configuring done和Generating done就成功了。
6.make编译

make

过程比较久。
编译过程如果遇到这个问题时,应该是你的依赖包gstreamer没安装好。安装gstreamer*就好了。我一般是在27%或30%的时候报这个错误。

make[2]: * [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o] Error 1
make[1]: * [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2

重新编译的时候这里可能还会报错,我是单独安装了ffmpeg的源码包,重新编译一下并加上这个参数./configure --enable-shared。就解决了。

编译到40%左右会遇到这个问题

[modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/test/test_drawing.cpp.o] 错误 1 make[1]: *** 
[modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/all] 错误 2

解决:cmake命令中添加参数-D ENABLE_PRECOMPILED_HEADERS=OFF
可能还会遇到c++的问题,就在cmake命令里加上c++,一般就是啥没有就加啥,但是咋加还是要找一下前人的经验。我曾经编译成功过。
但是我编译我的tensorrt的时候,他说我的opencv里的cmake版本太低,所以我就重新编译了opencv。

更新中。。。

你可能感兴趣的:(opencv,c++,centos,c++,centos,opencv,linux,cmake)