我的配置:
ubuntu18.04
gcc7.5.0(版本大于4.9即可)
RTX2060显卡(不影响)
大部分人需要安装不同版本的opencv,所以最好一开始就做好版本管理:
各个版本编译安装,来回切换
一、安装各种依赖:
sudo apt-get install libopencv-dev build-essential cmake git libprotobuf-dev
sudo apt-get install libgtk2.0-dev pkg-config python-dev python-numpy
sudo apt-get install libsnappy-dev gtk+-3.0 yasm Doxygen
sudo apt-get install libdc1394-22 libopenblas-dev liblmdb-dev
sudo apt-get install libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev
sudo apt-get install libjasper-dev libhdf5-serial-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libxine-dev
sudo apt-get install libgstreamer0.10-dev libgflags-dev
sudo apt-get install libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev
sudo apt-get install libqt4-dev libfaac-dev libgoogle-glog-dev
sudo apt-get install libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install libtheora-dev libvorbis-dev protobuf-compiler liblapack-dev
sudo apt-get install libxvidcore-dev x264 v4l-utils unzip liblapacke-dev
sudo apt-get install libavresample-dev libgphoto2-dev libavutil-dev
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev
sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next
sudo apt-get update
sudo apt-get install ffmpeg
二、opencv编译
下载opencv代码,在opencv目录下建立build文件夹
mkdir build
cd build
重点来了!!!!!!!!!!!!!!!!!!!!!!
cmake代码
cmake -D ENABLE_PRECOMPILED_HEADERS=OFF -D CMAKE_BUILD_TYPE=Release -D BUIILD_opencv_python=OFF -D WITH_CUDA=OFF -D CMAKE_INSTALL_PREFIX=/usr/local/opencv2410 -D WITH_FFMPEG=OFF -D ENABLE_PRECOMPILED_HEADERS=OFF ..
其中CMAKE_INSTALL_PREFIX是自定义的最终库文件位置,按需修改,方便版本管理
cmake配置是非常重要的设置,会排除很多bug,实践经验!!!
/usr/local/opencv2410
是我自定义的位置,按需修改
然后编译,安装
make -j16
sudo make install
make的时候会出现报错
error: the compiler can assume that the address of ‘annotate_img’ will never be NULL [-Werror=address]
打开opencv-2.4.10/build/modules/contrib/CMakeFiles/opencv_contrib.dir/flags.make文件,删掉 -Werror=address
。然后重新make
三、更改~/.bashrc
sudo gedit ~/.bashrc
在最后添加
export PATH=/home/install/opencv3.4.0/build-install/bin:$PATH
export LD_LIBRARY_PATH=/home/install/opencv3.4.0/build-install/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/home/install/opencv3.4.0/build-install/lib/pkgconfig
保存退出后,source ~/.bashrc
查看opencv版本号
pkg-config --modversion opencv
结语:
如果提示缺少videodev.h,缺少videoio.h,不用管,这个报错不会导致编译失败,是别的原因
如果有其他报错的话,在这基本能找到:
OpenCV-3编译安装方法及常见错误解决(Ubuntu平台)
ubuntu18编译opencv2.4.10源码