Ubuntu 18.04 下安装opencv 3.4.10,以及多版本安装和编写CmakeLists.txt问题,最后介绍如何彻底卸载opencv。
帮高师妹搭环境,希望她能送我一篇毕业论文 [doge]
一、源码下载
OpenCV官网
github地址
或者直接下载
二、安装依赖
-
安装依赖
sudo apt install build-essential sudo apt install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
error:无法定位软件包 libjasper-dev:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
再次运行第3步。
三、编译安装
-
解压源码
tar xvzf opencv-3.4.10.tar.gz
-
将解压后的包移动到/opt
sudo mv opencv-3.4.10 /opt
-
创建编译文件夹
cd /opt/opencv-3.4.10 mkdir build && cd build/
-
编译
# cmake -DWITH_GTK_2_X=ON -DCMAKE_INSTALL_PREFIX=/usr/local .. cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_GTK=ON .. # CMAKE_BUILD_TYPE=RELEASE:表示编译发布版本 # CMAKE_INSTALL_PREFIX:表示生成动态库的安装路径,可以自定义 # WITH_GTK=ON:这个配置是为了防止GTK配置失败:即安装了libgtk2.0-dev依赖,还是报错未安装 # OPENCV_GENERATE_PKGCONFIG=YES:表示自动生成OpenCV的pkgconfig文件,否则需要自己手动生成。 # 线程数最好大点,加快速度,查看cpu线程: # grep 'processor' /proc/cpuinfo | sort -u | wc -l make -j12 sudo make install
-
查看安装结果路径
pkg-config --cflags --libs opencv sudo find / -iname "*opencv*"
四、配置
- 第一种将opencv库添加到系统路经方法
sudo gedit /etc/ld.so.conf.d/opencv.conf 添加: /usr/local/lib sudo ldconfig
- 第二种将opencv库添加到系统路经方法
sudo gedit /etc/ld.so.conf.d 添加: include /etc/ld.so.conf.d/*.conf include /usr/local/lib # cmake编译时填的动态库安装路径加上/lib sudo ldconfig
- 添加环境变量
sudo gedit /etc/bash.bashrc 添加:export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig source /etc/bash.bashrc
五、测试
-
查看opencv版本
pkg-config opencv --modversion
-
运行源码示例
cd /opt/opencv-3.4.10/smaples/cpp/example_cmake cmake . make -j12 ./opencv_example
报错:Gtk-Message: Failed to load module "canberra-gtk-module"
解决方法:sudo apt-get install libcanberra-gtk-module -
demo测试
// main.cpp #include <stdio.h> #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat image; image = imread("../1.png"); if(!image.data) { printf("No image data \n"); return -1; } namedWindow("Display Image",WINDOW_AUTOSIZE); imshow("Display Image",image); cvWaitKey(0); return 0; }
# CMakeLists.txt cmake_minimum_required(VERSION 3.10) project(test_opencv) find_package(OpenCV REQUIRED) INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS}) add_executable(${PROJECT_NAME} main.cpp) target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
六、安装多版本opencv
- OpenCV多版本共存(3.4.10 和 4.1.5):
# 调用4.1.5时: sudo gedit ~/.bashrc 在文件末尾加上: export PKG_CONFIG_PATH=/opt/opencv-4.1.5/build/lib/pkgconfig export LD_LIBRARY_PATH=/opt/opencv-4.1.5/build/lib source ~/.bashrc # 想用之前的(3.4.10),注释这两行就行 # 查询OpenCV的版本 pkg-config --modversion opencv # 3.4.10 pkg-config --modversion opencv4 # 4.1.5
- CMakeLists.txt 编写
# 只有一个版本的OpenCV: find_package(OpenCV REQUIRED) # 会自动生成OpenCVConfig.cmake文件,其指定了CMake找OpenCV的路径,其.h文件在哪等 # 多版本OpenCV时,需要找到所需版本对应的OpenCVConfig.cmake文件, # 并将其路径添加到工程的CMakeLists.txt中: set(OpenCV_DIR "/opt/opencv-4.1.5/build") find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS})
七、彻底卸载opencv
- 第一种自动卸载
# 如果删掉了build,再安装上面编译生成一个 cd /opt/opencv-3.4.10/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*
- 第三种卸载手动卸载
# 会删除python的opencv环境 sudo apt autoremove opencv-doc opencv-data libopencv-dev libopencv2.4-java libopencv2.4-jni python-opencv libopencv-core2.4 libopencv-gpu2.4 libopencv-ts2.4 libopencv-photo2.4 libopencv-contrib2.4 libopencv-imgproc2.4 libopencv-superres2.4 libopencv-stitching2.4 libopencv-ocl2.4 libopencv-legacy2.4 libopencv-ml2.4 libopencv-video2.4 libopencv-videostab2.4 libopencv-objdetect2.4 libopencv-calib3d2.4
- 手动确定删除哪些
sudo find / -name "*opencv*" -exec rm -i {} \
- 直接删除
cd /usr find . -name "*opencv*" | xargs sudo rm -rf
- 检查是否卸载完成
pkg-config opencv --libs pkg-config opencv --modversion