ThangNguyen
ThangNguyen

Reputation: 551

How to build and install OpenCV 4.0?

First I would like to state that I am learning, so there is many thing I don't know.

I am using Jetson TX1 already imaged with JetPack 3.3

The board use Ubuntu 16.04

I installed cmake 3.13.4. I installed Python 3.7 (including Python 2.7 (default)/ Python3/ Python3.5/ Python 3.7).

I use command python3.7 -m pip install numpy. It shows

Successfully installed numpy-1.16.1

I have question here: how to find out where the numpy is installed? When I use ls command to view /usr/local/lib/python3.7/dist-packages it shows empty.

First time I tried to build openCV, I got this

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)

I would like to use Python3.7 to build openCV. I don't have much experience with cmake but I know that I can change the parameters in file "CMakeLists.txt" by defines the parameter when using cmake command line.

I have question here: Is there another way to defines these parameters so I don't have to includes these parameters in command lines every time I want to build with cmake?

Then I used below cmd line

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 ..

I got

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

I have question here: What is version 3.5.2 after Python 3.7? I am not sure why packages path point to Python 3.5. There is no libpython3.7 so I choose the one I found which is libpython3.7m.so. Is this correct?

Then I use

sudo make -j4

At the end I got below error messages:

[ 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

How can I fix these issues?

Upvotes: 1

Views: 1906

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207345

Here are some answers...

You can get the Numpy installation path and version with:

import numpy as np

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

print(np.__version__)
'1.15.1

You can also get the installation directory with pip:

pip show numpy

Sample Output

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 

Generally, you should not use sudo for cmake, instead just:

cmake ...

If you want to avoid typing all the switches, I generally make a shell script outside all the OpenCV stuff and in $HOME/scripts and I keep all the switches in there:

#!/bin/bash

cd $HOME/OpenCV/build/somewhere

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

Avoid using sudo for make, prefer:

make -j 4

Upvotes: 1

Related Questions