- 去官网下载opencv,在本教程中选用的时opencv3.4.1,其他版本的配置方法异曲同工。
下载链接http://opencv.org/releases.html,选择sources版本 - 解压下载下来的zip包
- 进入到解压后的文件包中
- 安装依赖库和cmake ,如果提醒需要apt-get update,那就先sudo su进入root权限,再sudo apt-get update,然后在执行下面命令
sudo apt-get install cmake
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev
5.安装完cmake之后执行命令,创建编译文件夹,不创建的会提示In-source builds are not allowed.
mkdir my_build_dir cd my_build_dir
6.cmake一下
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
7.执行命令,漫长的编译过程
期间可能会出现一些ERROR; 下面是我遇上的及解决办法.
首先如果以前有装了ffmpeg,先到ffmpeg的目录下uninstall,否则opencv安装会有冲突
- 错误一
moc: Cannot open options file specified with @ Usage: moc [options] <header-file> -o<file> write output to file rather than stdout -I<dir> add dir to the include path for header files -E preprocess only; do not generate Meta object code -D<macro>[=<def>] define macro,with optional definition -U<macro> undefine macro -i do not generate an #include statement -p<path> path prefix for included file -f[<file>] force #include,optional file name -nn do not display notes -nw do not display warnings @<file> read additional options from file -v display version of moc make[2]: *** [modules/highgui/src/moc_window_QT.cxx] Error 1 make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2 make: *** [all] Error 2
解决办法:If there is an accented character in the path name to the source files you may get this error. Move the source directory to a directory so that there is no accented characters in the path name. Then restart the compilation.
因为我放在了有中文的路径下,后来挪到没中文的路径下就不会报这个错误了;
- 错误二
Traceback (most recent call last): File "/media/stefan/A050780B5077E706/_raspi/opencv/sources/modules/java/generator/gen_java.py",line 1559,in <module> generator.gen(srcfiles,module,dstdir) File "/media/stefan/A050780B5077E706/_raspi/opencv/sources/modules/java/generator/gen_java.py",line 1061,in gen self.gen_class(ci) File "/media/stefan/A050780B5077E706/_raspi/opencv/sources/modules/java/generator/gen_java.py",line 1460,in gen_class for fi in ci.getAllMethods(): File "/media/stefan/A050780B5077E706/_raspi/opencv/sources/modules/java/generator/gen_java.py",line 824,in getAllMethods result.extend([fi for fi in sorted(self.methods) if fi.isconstructor]) TypeError: unorderable types: FuncInfo() < FuncInfo() modules/java/CMakeFiles/opencv_java.dir/build.make:86: recipe for target 'modules/java/photo+CalibrateCRF.java' Failed make[2]: *** [modules/java/photo+CalibrateCRF.java] Error 1 CMakeFiles/Makefile2:6282: recipe for target 'modules/java/CMakeFiles/opencv_java.dir/all' Failed make[1]: *** [modules/java/CMakeFiles/opencv_java.dir/all] Error 2 Makefile:137: recipe for target 'all' Failed make: *** [all] Error 2
解决办法:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -D BUILD_EXAMPLES=ON -D PYTHON_EXECUTABLE=$(which python3) -D BUILD_opencv_java=OFF BUILD_opencv_test_java=OFF ..
8.执行命令
sudo make install
9.sudo make install 执行完毕后OpenCV编译过程就结束了,接下来就需要配置一些OpenCV的编译环境首先将OpenCV的库添加到路径,从而可以让系统找到
sudo gedit /etc/ld.so.conf.d/opencv.conf
执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加
/usr/local/lib
10.执行如下命令使得刚才的配置路径生效
sudo ldconfig
11.配置bash
sudo gedit /etc/bash.bashrc
在最末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
保存,执行如下命令使得配置生效
source /etc/bash.bashrc
更新
sudo updatedb12.至此所有的配置都已经完成
下面用一个小程序测试一下
找到
cd到opencv-3.4.1/samples/cpp/example_cmake目录下
我们可以看到这个目录里官方已经给出了一个cmake的example我们可以拿来测试下
按顺序执行
cmake . make ./opencv_example即可看到打开了摄像头,在左上角有一个hello opencv 即表示配置成功