OpenCV-4.0.0在Ubuntu16.04 64位中的安装

步骤一:安装必要的库

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

步骤二:下载opencv和opencv_contrib的源代码

cd ~/work/opencv-4.0
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

步骤三:建立build文件夹并设置设置cmake的编译指令,这里采用支持python3的OpenCVIDE选择,IDE选择使用Qtcreator

cd ~/work/opencv-4.0/opencv-4.0.0
mkdir build
cd build
sudo apt-get install qtcreator
sudo apt-get install python3-pip
sudo pip3 install numpy

步骤四:检查各个文件是否在路径下

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local
-D OPENCV_EXTRA_MODULES_PATH=~/work/opencv-4.0/opencv_contrib-4.0.0/modules
-D PYTHON3_EXECUTABLE=/usr/bin/python3
-D PYTHON_INCLUDE_DIR=/usr/include/python3.5
-D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.5m
-D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so
-D PYTHON3_NUMPY_INCLUDE_DIRS=~/.local/lib/python3.5/site-packages/numpy/core/include/
..

步骤五:编译

make -j2

步骤六:安装

sudo make install

步骤七:首先将OpenCV库添加到路径让系统可以找到,执行以下操作即可

sudo gedit /etc/ld.so.conf.d/opencv.conf 
sudo ldconfig

步骤八:配置bash和pkg-config

sudo gedit /etc/bash.bashrc  

在末尾添加:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH

保存退出,然后执行下面的命令使得配置生效:

source /etc/bash.bashrc  

可使用下属命令查看安装的opencv的版本号:

pkg-config --modversion opencv

步骤九:安装python-opencv,并测试opencv是否安装成功

方法一:

cd ../samples/cpp/example_camke
cmake .
make
./opencv_example

程序运行成功,即可调起笔记本自带的摄像头

方法二:

pip3 install python-opencv
python3
import cv2
cv2.__version__

步骤十:qtcreator中opencv的pro文件配置

QT += core
QT -= gui
 
QMAKE_CXXFLAGS += -std=c++11
 
OR
 
CONFIG += c++11  #opencv需要C++11支持
 
 
TARGET = opencv_test
CONFIG += console
CONFIG -= app_bundle
 
TEMPLATE = app
\
SOURCES += main.cpp
 
 
INCLUDEPATH += /usr/local/include \
               /usr/local/include/opencv \
               /usr/local/include/opencv2
 
 
LIBS += /usr/local/lib/libopencv_aruco.so.4.0 \
        /usr/local/lib/libopencv_bgsegm.so.4.0 \
        /usr/local/lib/libopencv_bioinspired.so.4.0 \
        /usr/local/lib/libopencv_calib3d.so.4.0 \
        /usr/local/lib/libopencv_ccalib.so.4.0 \
        /usr/local/lib/libopencv_core.so.4.0 \
        /usr/local/lib/libopencv_datasets.so.4.0 \
        /usr/local/lib/libopencv_dnn_objdetect.so.4.0 \
        /usr/local/lib/libopencv_dnn.so.4.0 \
        /usr/local/lib/libopencv_dpm.so.4.0 \
        /usr/local/lib/libopencv_face.so.4.0 \
        /usr/local/lib/libopencv_features2d.so.4.0 \
        /usr/local/lib/libopencv_flann.so.4.0 \
        /usr/local/lib/libopencv_freetype.so.4.0 \
        /usr/local/lib/libopencv_fuzzy.so.4.0 \
        /usr/local/lib/libopencv_hfs.so.4.0 \
        /usr/local/lib/libopencv_highgui.so.4.0 \
        /usr/local/lib/libopencv_imgcodecs.so.4.0 \
        /usr/local/lib/libopencv_img_hash.so.4.0 \
        /usr/local/lib/libopencv_imgproc.so.4.0 \
        /usr/local/lib/libopencv_line_descriptor.so.4.0 \
        /usr/local/lib/libopencv_ml.so.4.0 \
        /usr/local/lib/libopencv_objdetect.so.4.0 \
        /usr/local/lib/libopencv_optflow.so.4.0 \
        /usr/local/lib/libopencv_phase_unwrapping.so.4.0 \
        /usr/local/lib/libopencv_photo.so.4.0 \
        /usr/local/lib/libopencv_plot.so.4.0 \
        /usr/local/lib/libopencv_reg.so.4.0 \
        /usr/local/lib/libopencv_rgbd.so.4.0 \
        /usr/local/lib/libopencv_saliency.so.4.0 \
        /usr/local/lib/libopencv_shape.so.4.0 \
        /usr/local/lib/libopencv_stereo.so.4.0 \
        /usr/local/lib/libopencv_stitching.so.4.0 \
        /usr/local/lib/libopencv_structured_light.so.4.0 \
        /usr/local/lib/libopencv_superres.so.4.0 \
        /usr/local/lib/libopencv_surface_matching.so.4.0 \
        /usr/local/lib/libopencv_text.so.4.0 \
        /usr/local/lib/libopencv_tracking.so.4.0 \
        /usr/local/lib/libopencv_videoio.so.4.0 \
        /usr/local/lib/libopencv_video.so.4.0 \
        /usr/local/lib/libopencv_videostab.so.4.0 \
        /usr/local/lib/libopencv_xfeatures2d.so.4.0 \
        /usr/local/lib/libopencv_ximgproc.so.4.0 \
        /usr/local/lib/libopencv_xobjdetect.so.4.0 \
        /usr/local/lib/libopencv_xphoto.so.4.0 \
        /usr/local/lib/libopencv_aruco.so.4.0 \

`

ps:
在步骤三这里numpy可能sudo pip3 install numpy安装报错如下:

```cpp
The directory '/home/ds/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ds/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/52/e6/1715e592ef47f28f3f50065322423bb75619ed2f7c24be86380ecc93503c/numpy-1.18.1-cp35-cp35m-manylinux1_x86_64.whl (19.9MB)
    4% |█▌                              | 952kB 7.3kB/s eta 0:43:31Exception:
Traceback (most recent call last):
  File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/response.py", line 226, in _error_catcher
    yield
  File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/response.py", line 301, in read
    data = self._fp.read(amt)
  File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/filewrapper.py", line 49, in read
    data = self.__fp.read(amt)
  File "/usr/lib/python3.5/http/client.py", line 458, in read
    n = self.readinto(b)
  File "/usr/lib/python3.5/http/client.py", line 498, in readinto
    n = self.fp.readinto(b)
  File "/usr/lib/python3.5/socket.py", line 575, in readinto
    return self._sock.recv_into(b)
  File "/usr/lib/python3.5/ssl.py", line 929, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/lib/python3.5/ssl.py", line 791, in read
    return self._sslobj.read(len, buffer)
  File "/usr/lib/python3.5/ssl.py", line 575, in read
    v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

解决办法:
第一步:先从地址下载.whl文件

https://files.pythonhosted.org/packages/52/e6/1715e592ef47f28f3f50065322423bb75619ed2f7c24be86380ecc93503c/numpy-1.18.1-cp35-cp35m-manylinux1_x86_64.whl

第二步:执行如下指令

sudo pip3 install numpy-1.18.1-cp35-cp35m-manylinux1_x86_64.whl

第三步:测试numpy,执行如下指令

python3

在这里插入图片描述至此,numpy安装成功。

你可能感兴趣的:(OpenCV-4.0.0在Ubuntu16.04 64位中的安装)