如果你之前安装过OpenCV,最好先卸干净:
sudo make uninstall #
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
接下来:
sudo apt-get --purge remove opencv-doc opencv-data python-opencv
1、获取opencv安装包:
可以百度云盘搜索,也可以用git clone的方法:
#获取opencv源码
git clone git@github.com:opencv/opencv.git
#获取opencv_contrib源码,根据自己需要,可以不安装
git clone git@github.com:opencv/opencv_contrib.git
2、安装依赖库
sudo apt-get install build-essential libgtk2.0-dev libvtk7-dev libjpeg-dev libtiff5-dev libjasper-dev libopenexr-dev libtbb-dev
报错:E: Unable to locate package libjasper-dev的解决办法
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
3、安装opencv
以opencv-4.1.0为例,将下载后的解压,按以下命令操作:
3.1 cmake
unzip opencv-4.1.0.zip
cd opencv-4.1.0
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DOPENCV_GENERATE_PKGCONFIG=ON -DCMAKE_INSTALL_PREFIX=/usr/local/opencv -DPYTHON3_LIBRARY=../anaconda3/lib/libpython3.8.so .. #设置安装目录,注意后面的两个点不要丢,否则找不到cmakelist文件
make # 编译 这一步会花一点时间,大概20分钟到一个小时,如果你的cpu性能好,可以用 make -j8
更全的安装命令:
cmake -D CMAKE_BUILD_TYPE=RELEASE -DOPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_GTK=ON -D WITH_OPENGL=ON -D BUILD_TIFF=ON -D WITH_IPP=ON -D BUILD_EXAMPLES=ON -D WITH_FFMPEG=ON ..
-DOPENCV_GENERATE_PKGCONFIG=ON opencv4 默认不生产pc文件,需要开启此项
-DCMAKE_INSTALL_PREFIX=/usr/local/opencv opencv的安装路径,可以省略,将安装到默认/usr/local目录
-DOPENCV_EXTRA_MODULES_PATH=…/…/opencv_contrib/modules/ 将 opencv 和 opencv_contrib 一起编译,如果不需要 opencv_contrib 可以将此项去掉
这一步成功后显示如下:
3.2 make install
输入以下命令:
make -j12
接下来:
sudo make install
至此,安装工作已经完了,接下来进行配置。
4、配置
输入以下命令:
cd /etc/ld.so.conf.d/
sudo touch opencv.conf
# 根据安装位置
sudo sh -c 'echo "/usr/local/opencv/lib" > opencv.conf' //注意自己的安装位置
sudo ldconfig
然后,复制.pc文件:
sudo cp -f /usr/local/opencv/lib/pkgconfig/opencv4.pc /usr/lib/pkgconfig/opencv4.pc
注意,我安装的是4.1.0,生成的是opencv4.pc,不是opencv.pc
测试一下:
pkg-config --modversion opencv4
输出:
附:
如果以前安装失败,最好将之前安装残留的文件完全清除,先进入之前opencv的build文件夹下,然后输入以下命令:
sudo make uninstall
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
sudo apt-get --purge remove opencv-doc opencv-data python-opencv
opencv3的安装和opencv4相同,需要注意的是,如果要安装两个版本的opencv,安装路径最好区分一下,便于以后配置环境
下面是分别用到opencv3和opencv4时对应的CmakeList.txt设置案例:
opencv3
cmake_minimum_required(VERSION 2.8)
project(ch8)
set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
#使用时最好将-march=native去掉,防止报内存错误
set(CMAKE_CXX_FLAGS "-std=c++11 ${SSE_FLAGS} -g -O3 -march=native")
find_package(OpenCV 3.4.14 REQUIRED)
find_package(Sophus REQUIRED)
find_package(Pangolin REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
${G2O_INCLUDE_DIRS}
${Sophus_INCLUDE_DIRS}
"/usr/include/eigen3/"
${Pangolin_INCLUDE_DIRS}
)
add_executable(optical_flow optical_flow.cpp)
target_link_libraries(optical_flow ${OpenCV_LIBS})
add_executable(direct_method direct_method.cpp)
target_link_libraries(direct_method ${OpenCV_LIBS} ${Pangolin_LIBRARIES})
opencv4
cmake_minimum_required(VERSION 2.8)
project(ch8)
set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
set(CMAKE_CXX_FLAGS "-std=c++11 ${SSE_FLAGS} -g -O3 -march=native")
set(${OpenCV_INCLUDE_DIRS} "/usr/local/opencv4/")
set(${OpenCV_LIBS} "/usr/local/opencv4/lib/")
find_package(OpenCV 4.2.0 REQUIRED)
find_package(Sophus REQUIRED)
find_package(Pangolin REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
${G2O_INCLUDE_DIRS}
${Sophus_INCLUDE_DIRS}
"/usr/include/eigen3/"
${Pangolin_INCLUDE_DIRS}
)
add_executable(optical_flow optical_flow.cpp)
target_link_libraries(optical_flow ${OpenCV_LIBS})
add_executable(direct_method direct_method.cpp)
target_link_libraries(direct_method ${OpenCV_LIBS} ${Pangolin_LIBRARIES})
报错:/usr/bin/ld: warning: libzstd.so.1.3.7, needed by //home/user/anaconda3/lib/libtiff.so.5
原因使用了其它的tiff编译版本库,cmake的时候加上:
-D BUILD_TIFF=ON -D ENABLE_PRECOMPILED_HEADERS=OFF
not found (try using -rpath or -rpath-link)
报错:libavcodec.a(vc1dsp_mmx.o):针对符号`ff_pw_9‘的重定位R_X86_64_PC32
ffmpeg安装问题,参考:ffmpeg安装过程记录
编译工具的选择
建议用g+±7
参考文献:
【1】在Ubuntu16.04下安装OpenCV环境,详细执行步骤和测试案例,良心填坑帖
【2】linux环境下编译安装opencv
【3】ubuntu安装opencv 解决ippicv下载问题:ippicv_2019_lnx_intel64_general_20180723.tgz
【4】如何在项目中区分使用opencv3和opencv4而不会产生冲突
【5】ffmpeg安装过程记录