如何构建和安装OpenCV 4.0?

时间:2019-02-15 14:39:34

标签: python opencv ubuntu cmake

首先,我想说我正在学习,所以有很多我不知道的事情。

我正在使用已经用JetPack 3.3成像的Jetson TX1

董事会使用Ubuntu 16.04

我安装了cmake 3.13.4。我安装了Python 3.7(包括Python 2.7(默认)/ Python3 / Python3.5 / Python 3.7)。

我使用命令python3.7 -m pip install numpy。它显示

Successfully installed numpy-1.16.1

我在这里有一个问题:如何找出numpy的安装位置?当我使用ls命令查看/usr/local/lib/python3.7/dist-packages时,它显示为空。

我第一次尝试构建openCV时,我就得到了

Python 2:
    Interpreter:                 /usr/bin/python2.7 (ver 2.7.12)
    Libraries:                   /usr/lib/aarch64-linux-gnu/libpython2.7.so (ver 2.7.12)
    numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.11.0)
    packages path:               lib/python2.7/dist-packages

  Python 3:
    Interpreter:                 /usr/bin/python3 (ver 3.5.1)

我想使用Python3.7构建openCV。我对cmake经验不足,但是我知道我可以在使用cmake命令行时通过定义参数来更改文件“ CMakeLists.txt”中的参数。

我在这里有一个问题:是否还有另一种定义这些参数的方法,这样我每次使用cmake进行构建时都不必在命令行中包含这些参数?

然后我在cmd行下面使用了

sudo cmake -D ENABLE_PRECOMPILED_HEADERS=OFF -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D WITH_CUFFT=OFF -D WITH_CUBLAS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=OFF -D INSTALL_C_EXAMPLES=OFF -D PYTHON3_EXECUTABLE=/usr/bin/python3.7 -D PYTHON3_LIBRARIES=/usr/lib/aarch64-linux-gnu/libpython3.7m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3.7/dist-packages/numpy/core/include -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.7 -D BUILD_EXAMPLES=OFF ..

我知道了

Python 3:
--     Interpreter:                 /usr/bin/python3.7 (ver 3.5.2)
--     Libraries:                   /usr/lib/aarch64-linux-gnu/libpython3.7m.so (ver 3.5.2)
--     numpy:                       /usr/lib/python3.7/dist-packages/numpy/core/include (ver 1.16.1)
--     packages path:               lib/python3.5/dist-packages
-- 
--   Python (for build):            /usr/bin/python3.7

我在这里有疑问:Python 3.7之后的3.5.2版本是什么?我不确定为什么软件包路径指向Python 3.5。没有libpython3.7,所以我选择了找到的libpython3.7m.so。这是正确的吗?

然后我用

sudo make -j4

最后,我收到以下错误消息:

[ 98%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o
/mnt/data/Software/opencv/modules/python/src2/cv2.cpp:39:33: fatal error: numpy/ndarrayobject.h: No such file or directory
compilation terminated.
modules/python3/CMakeFiles/opencv_python3.dir/build.make:62: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o' failed
make[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
CMakeFiles/Makefile2:3371: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/all' failed
make[1]: *** [modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 98%] Building CXX object modules/video/CMakeFiles/opencv_perf_video.dir/perf/opencl/perf_motempl.cpp.o
[ 98%] Building CXX object modules/video/CMakeFiles/opencv_test_video.dir/test/test_estimaterigid.cpp.o
[ 98%] Building CXX object modules/video/CMakeFiles/opencv_perf_video.dir/perf/opencl/perf_optflow_farneback.cpp.o
[ 98%] Building CXX object modules/video/CMakeFiles/opencv_perf_video.dir/perf/opencl/perf_optflow_pyrlk.cpp.o
[ 98%] Building CXX object modules/video/CMakeFiles/opencv_test_video.dir/test/test_kalman.cpp.o
[ 98%] Linking CXX executable ../../bin/opencv_test_calib3d
[ 98%] Built target opencv_test_calib3d
[ 99%] Building CXX object modules/video/CMakeFiles/opencv_perf_video.dir/perf/perf_bgfg_knn.cpp.o
[100%] Building CXX object modules/video/CMakeFiles/opencv_test_video.dir/test/test_main.cpp.o
[100%] Building CXX object modules/video/CMakeFiles/opencv_test_video.dir/test/test_optflowpyrlk.cpp.o
[100%] Building CXX object modules/video/CMakeFiles/opencv_perf_video.dir/perf/perf_bgfg_mog2.cpp.o
[100%] Building CXX object modules/video/CMakeFiles/opencv_perf_video.dir/perf/perf_disflow.cpp.o
[100%] Linking CXX executable ../../bin/opencv_test_video
[100%] Built target opencv_test_video
[100%] Building CXX object modules/video/CMakeFiles/opencv_perf_video.dir/perf/perf_ecc.cpp.o
[100%] Building CXX object modules/video/CMakeFiles/opencv_perf_video.dir/perf/perf_main.cpp.o
[100%] Building CXX object modules/video/CMakeFiles/opencv_perf_video.dir/perf/perf_optflowpyrlk.cpp.o
[100%] Building CXX object modules/video/CMakeFiles/opencv_perf_video.dir/perf/perf_variational_refinement.cpp.o
[100%] Linking CXX executable ../../bin/opencv_perf_video
[100%] Built target opencv_perf_video
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2

如何解决这些问题?

1 个答案:

答案 0 :(得分:1)

以下是一些答案...

您可以通过以下方式获取Numpy安装路径和版本:

import numpy as np

print(np.__path__)
'/usr/local/lib/python3.7/site-packages/numpy'

print(np.__version__)
'1.15.1

您还可以使用pip获取安装目录:

pip show numpy

示例输出

Name: numpy
Version: 1.15.1
Summary: NumPy: array processing for numbers, strings, records, and objects.
Home-page: http://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: None
License: BSD
Location: /usr/local/lib/python3.7/site-packages
Requires: 
Required-by: tifffile, scipy, scikit-learn, PyWavelets, numexpr, numba, matplotlib, imageio 

通常,您不应该将sudo用于cmake,而应该使用:

cmake ...

如果您想避免键入所有开关,通常我会在所有 OpenCV 东西的 outside 内并在$HOME/scripts中创建一个shell脚本,我会保留所有切换到那里:

#!/bin/bash

cd $HOME/OpenCV/build/somewhere

cmake -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D CMAKE_BUILD_TYPE=RELEASE ..

避免为sudo使用make,而是:

make -j 4