Ubuntu系统---配置OpenCV

Ubuntu系统---配置OpenCV

目录

一、Ubuntu下配OpenCV

二、Ubuntu下配python-opencv

 

说明

  上述一、二两种方式,配置OpenCV还是有区别的。按个人已有知识的理解,“Ubuntu下配OpenCV”是在系统下装了一个opencv,OpenCV的库全;“Ubuntu下配python-opencv”是Python可以调用OpenCV的相关库,OpenCV的库不全,仅供python用。

 

正文

一、Ubuntu下配OpenCV

  @https://blog.csdn.net/baidu_34971492/article/details/81665538

  下载安装包,进行一步步的安装。

  (1)在Opencv官网下载OpenCV3.4.2 Sources, 网址链接:https://opencv.org/releases.html,解压。

  (2)安装cmake 和 依赖库。

1.快速安装cmake(也可以下载cmake安装包进行安装)
sudo apt-get install cmake #查看安装的cmake版本:cmake --version #https://www.cnblogs.com/zhangjiansheng/p/7990028.html sudo apt-get update 2.依赖库 sudo apt-get install build-essential sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

  (3)OpenCV3.4.2安装。( 前提已安装好cmake:sudo apt-get install cmake)

(3.1)创建build文件夹
mkdir build cd build

(3.2)cmake一下
#cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
#cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=~/opencv-3.4.1/build/installed -DWITH_CUDA=OFF .. (建立opencv-3.4.1/build/installed这几个文件夹
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3.4.2 ..

注意:如果已经在新的文件夹中编译,但是还会出现之前的报错,把cmakecache.txt删了再编译就可
不报错,继续。。。

(3.3)make一下
sudo make
sudo make install #执行完毕后OpenCV编译过程就结束

(3.4)配置一些OpenCV的编译环境
第一步:系统环境
1.首先将OpenCV的库添加到路径,从而可以让系统找到:
sudo gedit /etc/ld.so.conf.d/opencv.conf

2.只需要在文件末尾添加:
/usr/local/lib 

3.使得刚才的配置路径生效:
sudo ldconfig

第二步:配置bash
1.打开bash.bashrc

sudo gedit /etc/bash.bashrc # sudo gedit ~/.bashrc

2.在最末尾添加
#@多版本OpenCV切换 https://blog.csdn.net/learning_tortosie/article/details/80594399
#export PKG_CONFIG_PATH=~/opencv-3.4.1/build/installed/lib/pkgconfig
#export LD_LIBRARY_PATH=~/opencv-3.4.1/build/installed/lib
export PKG_CONFIG_PATH=/usr/local/opencv3.4.2/lib/pkgconfig 
export LD_LIBRARY_PATH=/usr/local/opencv3.4.2/lib

3.使配置生效
source /etc/bash.bashrc  # source ~/.bashrc

(3.5)查询OpenCV版本
pkg-config --modversion opencv  # 如果输出3.4.2,就表明配置成功。 如果前面没报错,输出不是3.4.2,可能是配置没生效,重启电脑
也可以:@https://blog.csdn.net/cocoaqin/article/details/78376382
配置.bashrc

echo '/usr/local/lib' | sudo tee -a /etc/ld.so.conf.d/opencv.conf sudo ldconfig printf '# OpenCV\nPKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig\nexport PKG_CONFIG_PATH\n' >> ~/.bashrc source ~/.bashrc

  (4)OpenCV3.4.2 测试安装成功

  测试1:

  cd到opencv3.4.2/samples/cpp/example_cmake目录下,这个目录里官方已经给出了一个cmake的example,可以拿来测试下,按顺序执行:

cmake .
make
./opencv_example

  看到打开了摄像头,在左上角有一个hello opencv ,即表示配置成功。

  测试2:

  @https://blog.csdn.net/cocoaqin/article/details/78376382  (还没实践)

 

过程记录:

 

二、Ubuntu下配python-opencv

   查看系统中的python版本,应该有Python2.x 和 Python3.x, 切换到python3.x下(想配 python3.x + OpenCV3.4.2)。

  (1)若无pip,先安装pip3,执行命令: sudo apt install python3-pip

  (2)安装依赖项,安装libopencv-dev依赖包,运行命令: sudo apt install libopencv-dev

  (3)安装opencv-python库
    因为系统中已经安装了python3和pip3,所以直接运行。

         1. 直接安装最新版:sudo pip3 install opencv-python

    2. 或者可以进行指定版本安装:pip3 install opencv_python==版本号
    由于我这里是安装opencv3.4.2,目前的最新版是3.4.2.16
    所以直接执行:pip3 install opencv_python==3.4.2.16

 

  (4)  成功之后,运行python3,进入编译界面,导入库查看版本print(cv2._version__)

python3

import cv2

cv2.__version__

备注:
同样的方法,python2安装,不好用,如下的好使:
sudo python2 -m pip install opencv-python  -i https://pypi.tuna.tsinghua.edu.cn/simple

u@u1604:~$ python
Python 2.7.12 (default, Nov 12 2018, 14:36:49) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.0'
>>> 


u@u1604:~$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.0'
>>>


 

 

附:《Ubuntu16.04下安装opencv3.4.2》
给出了配置编译opencv的四种方法,值得参考。
从下面四种选择里面选一种进行编译
- 配置编译opencv (无NVIDIA CUDA版本)
- 配置编译opencv (NVIDIA CUDA版本)
- 配置编译opencv(NVIDIA Jetson TX2开发板)
- 简化配置编译
  @https://blog.csdn.net/liuxiaodong400/article/details/81089058

--------------------------------------------------------------------------------

@https://blog.csdn.net/YuYunTan/article/details/85017065

@https://www.learnopencv.com/install-opencv-3-4-4-on-ubuntu-16-04/

你需要下载opencv3.4.1和opencv_contrib 3.4.1,然后对其解压,这些基础命令和操作则不概述。
  将安装包解压到某一自己指定的目录,记为{Opencv_Origin_Dir},目前我指定的目录解压到了,/home/tanqiwei/Documents/environment,所以{Opencv_Origin_Dir}对应就是/home/tanqiwei/Documents/environment/opencv-3.4.1

tanqiwei@ubuntu:~/Documents/environment$ pwd
/home/tanqiwei/Documents/environment
tanqiwei@ubuntu:~/Documents/environment$ ls
opencv-3.4.1         opencv_contrib-3.4.1


安装前的必备包

  这些安装不算十分完全,我只安装自己够用就成的某些包。
  安装一些必要的库,还有cmake,git,g++。

sudo apt-get install build-essential 
sudo apt-get install cmake git g++


安装依赖包

  安装一些依赖包。

sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install checkinstall yasm libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev libavresample-dev x264 v4l-utils


处理图像所需的包

sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev 


处理视频所需包

sudo apt-get install libxvidcore-dev libx264-dev ffmpeg


opencv功能优化

sudo apt-get install libatlas-base-dev gfortran 
   

部分依赖包

sudo apt-get install libopencv-dev  libqt4-dev qt4-qmake libqglviewer-dev libsuitesparse-dev libcxsparse3.1.4 libcholmod3.0.6 
sudo apt-get install python-dev python-numpy


可选依赖

sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen


编译和安装

  进入OpenCV的源码解压目录,{Opencv_Origin_Dir},我的是/home/tanqiwei/Documents/environment/opencv-3.4.1
  我的opencv_contrib目录和其同级,/home/tanqiwei/Documents/environment/opencv_contrib-3.4.1均在/home/tanqiwei/Documents/environment下然后在{Opencv_Origin_Dir}下运行

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 WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.1/modules -D PYTHON_EXECUTABLE=/usr/bin/python3.5 -D BUILD_EXAMPLES=ON .. 


  这里面过多参数都是属于cmake的范畴,我这里不去描述,大概就是表示opencv应该安装在哪里,扩展包在何处,需要开启什么功能。

  其实编译过程中会发现,自行下载IPPICV,tiny-dnn等等。

    IPPICV是个链接的免费子库,如果想要禁用IPP加速,CMake的时候,加上-D WITH_IPP=OFF。

  其实很多可能可选的从cmake的编译输出看来我们并没有安装,比如java,VTK等等,看下面这种类型的输出就知道了,到时候你只需要对应安装,然后修改CMake编译命令,一般来说,opencv编译过程中,自发也会去寻找这些东西。

-- Checking for module 'gstreamer-base-1.0'
--   No package 'gstreamer-base-1.0' found
-- Checking for module 'gstreamer-video-1.0'
--   No package 'gstreamer-video-1.0' found
-- Checking for module 'gstreamer-app-1.0'
--   No package 'gstreamer-app-1.0' found
-- Checking for module 'gstreamer-riff-1.0'
--   No package 'gstreamer-riff-1.0' found
-- Checking for module 'gstreamer-pbutils-1.0'
--   No package 'gstreamer-pbutils-1.0' found


-- Could NOT find JNI (missing:  JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH) 
-- Could NOT find Pylint (missing:  PYLINT_EXECUTABLE) 
-- Could NOT find Matlab (missing:  MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIRS MATLAB_ROOT_DIR MATLAB_LIBRARIES MATLAB_LIBRARY_DIRS MATLAB_MEXEXT MATLAB_ARCH MATLAB_BIN) 
-- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file

-- No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
-- Failed to find installed gflags CMake configuration, searching for gflags build directories exported with CMake.
-- Failed to find gflags - Failed to find an installed/exported CMake configuration for gflags, will perform search for installed gflags components.

-- CERES support is disabled. Ceres Solver for reconstruction API is required.
-- Module opencv_ovis disabled because OGRE3D was not found
-- Caffe:   NO
-- Protobuf:   NO

-- Checking for modules 'tesseract;lept'
--   No package 'tesseract' found
--   No package 'lept' found


  最后会列出其编译后的模块列表。

--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core cvv datasets dnn dnn_objdetect dpm face features2d flann freetype fuzzy hdf hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python_bindings_generator reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    js world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev dnn_modern java matlab ovis python2 python3 viz
--     Applications:                tests perf_tests examples apps
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 
--   GUI: 
--     QT:                          YES (ver 5.5.1)
--       QT OpenGL support:         YES (Qt5::OpenGL 5.5.1)
--     GTK+:                        NO
--     OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib/x86_64-linux-gnu/libGL.so)
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.8)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.2.54)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.6)
--     JPEG 2000:                   /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
--     OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2.2.0)
-- 
--   Video I/O:
--     DC1394:                      YES (ver 2.2.4)
--     FFMPEG:                      YES
--       avcodec:                   YES (ver 56.60.100)
--       avformat:                  YES (ver 56.40.101)
--       avutil:                    YES (ver 54.31.100)
--       swscale:                   YES (ver 3.1.101)
--       avresample:                YES (ver 2.1.0)
--     GStreamer:                   
--       base:                      YES (ver 0.10.36)
--       video:                     YES (ver 0.10.36)
--       app:                       YES (ver 0.10.36)
--       riff:                      YES (ver 0.10.36)
--       pbutils:                   YES (ver 0.10.36)
--     libv4l/libv4l2:              NO
--     v4l/v4l2:                    linux/videodev2.h
--     gPhoto2:                     YES
-- 
--   Parallel framework:            TBB (ver 4.4 interface 9002)
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2017.0.3 [2017.0.3]
--            at:                   /home/tanqiwei/Documents/environment/opencv-3.4.1/build/3rdparty/ippicv/ippicv_lnx
--     Intel IPP IW:                sources (2017.0.3)
--               at:                /home/tanqiwei/Documents/environment/opencv-3.4.1/build/3rdparty/ippicv/ippiw_lnx
--     Lapack:                      YES (/usr/lib/liblapack.so /usr/lib/libcblas.so /usr/lib/libatlas.so)
--     Eigen:                       YES (ver 3.2.92)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   NVIDIA CUDA:                   NO
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/tanqiwei/Documents/environment/opencv-3.4.1/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python (for build):            /usr/bin/python3
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Matlab:                        NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tanqiwei/Documents/environment/opencv-3.4.1/build


  我们可以发现,我们编译已经成功,可以进行下一步,即make,但是值得注意的是,如果用多核make可能会报错,为了保险起见,我还是原始的make命令,不加-j。

make


  然后安装。

sudo make install

  最后最好开启重启一次,本人曾安装过后,虚拟机重启直接奔溃,无法进入系统内部,主要原因不太清楚,但是进入到了某种图形模式,说是图形模式损坏,之后只好从备份的快照中恢复,当然也有当时可能装少了部分必要依赖项的可能性也说不定,建议安装的机器内存要大一点,4GB为一般,6GB不错,8GB很好。
运行测试

  我们运行例子进行测试。你可以选择任意例子,这里我选择在我的github的opencv例子进行测试。

git clone https://github.com/tanqiwei/myOpencvStudyCode.git


  大概几M的内容,然后进入myOpencvStudyCode/LearningOpencv3/chapter2/example2.1
  接着按下面命令

mkdir build
cd build
cmake ..
make
./example2_1 ../data/test.jpg


  你可以发现运行成功,故而咱们安装顺利。会发现显示图片窗口,按ESC键退出。
重启后,发现还能开启,说明虚拟机的16.04的系统安装就成功了。
安装过程命令总结

# 安装及下载,该操作不解释,都放在一个统一目录下
# 我的是/home/tanqiwei/Documents/environment
# 也就是在environment文件夹里有opencv-3.4.1和opencv_contrib-3.4.1两个文件夹

# 安装必备库,cmake,git,g++
sudo apt-get install build-essential 
sudo apt-get install cmake git g++
# 安装依赖项
sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install checkinstall yasm libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev libavresample-dev x264 v4l-utils
# 处理图像所需的包
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev 
# 处理视频所需的包
sudo apt-get install libxvidcore-dev libx264-dev ffmpeg
# opencv功能优化
sudo apt-get install libatlas-base-dev gfortran 
# 某些依赖包
sudo apt-get install libopencv-dev  libqt4-dev qt4-qmake libqglviewer-dev libsuitesparse-dev libcxsparse3.1.4 libcholmod3.0.6 
sudo apt-get install python-dev python-numpy
# 可选依赖项
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

# 进入opencv源码目录,注意opencv和opencv_contrib同级,
# 即都属于同一个主目录下,我的目录为/home/tanqiwei/Documents/environment,
# 下面有opencv-3.4.1和opencv_contrib-3.4.1


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 WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.1/modules -D PYTHON_EXECUTABLE=/usr/bin/python3.5 -D BUILD_EXAMPLES=ON .. 
make

 

posted on 2019-07-29 12:56  WP的烂笔头  阅读(3943)  评论(0编辑  收藏  举报