linux 下opencv编译安装
安装所需要的工具和包:
sudo apt-get install build-essential pkgconfig
sudo apt-get install cmake git gcc
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
安装依赖库ffmpeg
cd ffmpeg/
./configure --disable-yasm --enable-shared --enable-pic --prefix=/usr/local/ffmpeg 生成可连接库,--prefix设置安装路径
make
make install 开始安装
vim /etc/profile 打开环境变量文档
在文尾输入:
export FFMPEG_HOME=/usr/local/ffmpeg
export PATH=$FFMPEG_HOME/bin:$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib #添加动态库路径
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/ffmpeg/lib/pkgconfig:/usr/loal/lib
ffmpeg -version 如果显示版本信息,就证明ffmpeg已经成功安装了
首先检测你的环境是否配置成功。
pkg-config ffmpeg --libs --cflags查看ffmpeg链接库是否配置好了如果没配置好也别着急,继续往下看)
pkg-config opencv --libs --cflags查看opencv链接库和头文件配置
如果没有打印程序的链接库路径,说明链接库没有完整配置好
这个时候首进入ffmpeg文件目录(不是源码目录,是软件安装目录/usr/local/ffmpeg/)/lib的目录的
所有文件复制到/usr/local/lib目录下;然后打开ffmpeg/lib/的里有个pkgconfig,
把里头的文件全部复制到/usr/local/lib/pkgconfig里头
编译源码:
1.cd opencv-3.3.0
2.mkdir build
3.cd build
4.cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. 编译通过
4.1.cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/firstuser/depend/opencv-3.3.0/opencv_contrib-3.3.0/modules/ -D WITH_TBB=ON -D BUILD_SHARED_LIBS=OFF -D WITH_OPENMP=ON -D ENABLE_PRECOMPILED_HEADERS=OFF ..
4.2.cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_SHARED_LIBS=OFF -D WITH_OPENMP=ON -D ENABLE_PRECOMPILED_HEADERS=OFF ..
5.make -j4 多线程
6.sudo make install
修改ippicv下载路径:
vim /home/firstuser/depend/opencv-3.3.0/3rdparty/ippicv/ippicv.cmake #根据自己的路径填写
将47行的
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
改为手动下载的文件的本地路径:
"file:///home/firstuser/下载/" (根据自己的路径填写)
到了下载ippicv那一步时会自动从本地下载。
错误及解决方法:
0.error: ‘CODEC_FLAG_GLOBAL_HEADER’ was not declared in this scope
error: ‘AVFMT_RAWPICTURE’ was not declared in this scope
在/opt/opencv/opencv-3.3.0/modules/videoio/src/cap_ffmpeg_impl.hpp 里最顶端添加
#define AV_CODEC_FLAG_GLOBAL_HEADER (1 << 22)
#define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER
#define AVFMT_RAWPICTURE 0x0020
1.-- No package 'gtk+-3.0' found
sudo apt-get install libgtk-3-dev
2.-- No package 'gstreamer-base-1.0' found
-- No package 'gstreamer-video-1.0' found
-- No package 'gstreamer-app-1.0' found
-- No package 'gstreamer-riff-1.0' found
-- No package 'gstreamer-pbutils-1.0' found
sudo apt-get -y install libgstreamer-plugins-base1.0-dev
sudo apt-get -y install libgstreamer1.0-dev
3.-- No package 'libavresample' found
-- No package 'libgphoto2' found
sudo apt-get -y install libavresample-dev
sudo apt-get -y install libgphoto2-dev
4.-- Could not find OpenBLAS include. Turning OpenBLAS_FOUND off
-- Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
-- Could NOT find Atlas (missing: Atlas_CBLAS_INCLUDE_DIR Atlas_CLAPACK_INCLUDE_DIR Atlas_CBLAS_LIBRARY Atlas_BLAS_LIBRARY Atlas_LAPACK_LIBRARY)
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Could NOT find JNI (missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
-- Could NOT find Matlab (missing: MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIRS MATLAB_ROOT_DIR MATLAB_LIBRARIES MATLAB_LIBRARY_DIRS MATLAB_MEXEXT MATLAB_ARCH MATLAB_BIN)
-- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
sudo apt-get install libopenblas-dev
5.-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
sudo apt-get install doxygen
6.-- Could NOT find JNI (missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
sudo mkdir /usr/local/java
sudo tar zxvf jdk-8u151-linux-x64.tar.gz -C /usr/local/java
sudo ln -s /usr/local/java/jdk1.8.0_151/ /usr/local/java/latest
sudo vim /etc/profile
export JAVA_HOME=/usr/local/java/latest
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
cd ~/opencv-3.3.0/build
make clean
重新编译:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. 编译通过
make -j8
make install
附3.4版本以上有该问题:
/usr/bin/ld: warning: libswresample.so.3, needed by //usr/local/ffmpeg/lib/libavcodec.so.58, not found (try using -rpath or -rpath-link)
//usr/local/ffmpeg/lib/libavcodec.so.58:对‘swr_close@LIBSWRESAMPLE_3’未定义的引用
解决方法:
https://blog.csdn.net/guo_lei_lamant/article/details/82561312
vim /etc/ld.so.conf.d
/usr/local/ffmpeg/lib
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
重装新版本:先卸载旧版本
1.删除安装文件
cd /home/***/opencv/build
sudo make uninstall
cd ..
sudo rm -r build
2.删除所有和opencv相关文件
sudo rm -r /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2
cd /usr
find . -name "*opencv*" | xargs sudo rm -rf
3.删除代码包
cd /home/***
chmod a+x /home/***/opencv
rm -r /home/***/opencv
4.
cd ~/opencv-3.4.6
error while loading shared libraries: libopencv_core.so.3.4: cannot open shared object file: No such file or directory
1.打开路径:/etc/ld.so.conf.d
2.创建文件:OpenCV.conf文件
3.添加自己opencv的lib路径 ldconfig (通常为/usr/local/lib)
error: ./TopCamDetDL.so: undefined symbol: _ZN2cv3dnn23experimental_dnn_34_v143NetC1Ev
opencv版本问题
重新编译:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. 编译通过
make -j8
make install