其实不止适用于Ubuntu16.04,同样适用于Ubuntu14.04,以及安装opencv3.4.5以下版本!
1 开发环境:
-
Ubuntu 16.04 64位
-
cmake
-
ant
-
jdk
-
git
-
python
2 下载安装包
从github中down下OpenCV + OpenCV_Contrib
库,可以从https://github.com/opencv上下载(注意版本对应),也可以通过指令下载(建议使用):
1 2 |
git clone https://github.com/Itseez/opencv.git git clone https://github.com/Itseez/opencv_contrib.git |
这种下载方法一般是下载的最新版,我们想要下载3.4.5版本的,可以到github上下载指定版本的压缩包。下载过程中,我们可以先安装依赖。
3 安装依赖
1 2 3 |
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 |
卸载apt安装的opencv:
1 |
sudo apt-get autoremove libopencv-dev |
4 编译
将下载好的压缩包解压,将contrib
放入opencv
目录下,并命名opencv_contrib
,接着创建文件夹build
,最后opencv目录结构如下:
进入build文件夹,然后打开终端,输入:
1 |
cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules .. |
注意后面两个点不要忘了输。
这时开始编译,等待一段时间(大概10分钟以内吧),出现以下结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
································· -- 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: NO -- GStreamer: NO -- libv4l/libv4l2: NO -- v4l/v4l2: linux/videodev2.h -- -- Parallel framework: pthreads -- -- Trace: YES (with Intel ITT) -- -- Other third-party libraries: -- Intel IPP: 2019.0.0 Gold [2019.0.0] -- at: /home/mcj/bigdisk/opencv-3.4.5/build/3rdparty/ippicv/ippicv_lnx/icv -- Intel IPP IW: sources (2019.0.0) -- at: /home/mcj/bigdisk/opencv-3.4.5/build/3rdparty/ippicv/ippicv_lnx/iw -- Lapack: NO -- Eigen: NO -- Custom HAL: NO -- Protobuf: build (3.5.1) -- -- OpenCL: YES (no extra features) -- Include path: /home/mcj/bigdisk/opencv-3.4.5/3rdparty/include/opencl/1.2 -- Link libraries: Dynamic load -- -- Python 2: -- Interpreter: /usr/bin/python2.7 (ver 2.7.12) -- Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.12) -- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.11.0) -- install path: lib/python2.7/dist-packages/cv2/python-2.7 -- -- Python (for build): /usr/bin/python2.7 -- -- Java: -- ant: NO -- JNI: NO -- Java wrappers: NO -- Java tests: NO -- -- Install to: /usr/local -- ----------------------------------------------------------------- -- -- Configuring done -- Generating done -- Build files have been written to: /home/mcj/bigdisk/opencv-3.4.5/build |
然后,就可以make了:
1 |
make -j8 |
这个时间也比较长,大概五分钟左右吧,安装完毕:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
···································· [100%] Building CXX object modules/stitching/CMakeFiles/opencv_perf_stitching.dir/perf/perf_main.cpp.o [100%] Linking CXX executable ../../bin/opencv_perf_optflow [100%] Building CXX object modules/stitching/CMakeFiles/opencv_perf_stitching.dir/perf/opencl/perf_warpers.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/trackerBoostingModel.cpp.o [100%] Built target opencv_perf_optflow [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/gtrUtils.cpp.o [100%] Building CXX object modules/stitching/CMakeFiles/opencv_perf_stitching.dir/perf/opencl/perf_stitch.cpp.o [100%] Building CXX object modules/stitching/CMakeFiles/opencv_perf_stitching.dir/perf/perf_matchers.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/tldModel.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/trackerSampler.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/trackerMIL.cpp.o [100%] Linking CXX executable ../../bin/opencv_test_stitching [100%] Built target opencv_test_stitching [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/trackerBoosting.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/trackerMILModel.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/trackerKCF.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/tldDetector.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/onlineBoosting.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/tracking_utils.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/trackerCSRTSegmentation.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/multiTracker.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/augmented_unscented_kalman.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/tldUtils.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/trackerSamplerAlgorithm.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/src/tldEnsembleClassifier.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_tracking.dir/opencl_kernels_tracking.cpp.o [100%] Linking CXX shared library ../../lib/libopencv_tracking.so [100%] Linking CXX executable ../../bin/opencv_perf_stitching [100%] Built target opencv_tracking Scanning dependencies of target opencv_python2 Scanning dependencies of target opencv_perf_tracking Scanning dependencies of target opencv_test_tracking [100%] Building CXX object modules/tracking/CMakeFiles/opencv_perf_tracking.dir/perf/perf_Tracker.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_perf_tracking.dir/perf/perf_main.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_test_tracking.dir/test/test_trackers.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_test_tracking.dir/test/test_aukf.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_test_tracking.dir/test/test_trackerParametersIO.cpp.o [100%] Building CXX object modules/tracking/CMakeFiles/opencv_test_tracking.dir/test/test_main.cpp.o [100%] Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o [100%] Built target opencv_perf_stitching [100%] Building CXX object modules/tracking/CMakeFiles/opencv_test_tracking.dir/test/test_ukf.cpp.o [100%] Linking CXX executable ../../bin/opencv_test_tracking [100%] Linking CXX executable ../../bin/opencv_perf_tracking [100%] Built target opencv_test_tracking [100%] Built target opencv_perf_tracking [100%] Linking CXX shared module ../../lib/cv2.so [100%] Built target opencv_python2 |
到了这一步等于已经安装成功了。接下来只需要将编译好的文件安装到系统目录即可。
1 |
sudo make install |
最后激活一下即可。
1 |
sudo ldconfig |
这样就可以使用了。
想要测试一下是不是安装成功,可以用个小例子进行测试:
错误解决:
本文最后更新于2023年6月6日,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!