opencv官方教程请参考https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
参考博客https://blog.csdn.net/new_delete_/article/details/84797041
此教程使用g++和cmake编译opencv 4.5.0
请确保ubuntu已经安装了g++, cmake, make, wget, unzip,若没有安装,则使用以下命令安装:
sudo apt install -y g++
sudo apt install -y cmake
sudo apt install -y make
sudo apt install -y wget unzip
安装opencv依赖的库:
sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
支持python3:
此步为可选,不设置也可以(亲测可行),如果设置了,在下一步cmake中下载ippicv可能会很慢
# python3支持
sudo apt install python3-dev python3-numpy
# streamer支持
sudo apt install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
# 可选的依赖
sudo apt install libpng-dev libopenexr-dev libtiff-dev libwebp-dev
安装4.5.0版本:
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.0.zip
或安装最新版本:
wget -O opencv.zip https://github.com/opencv/opencv/archive/master.zip
其他版本可从opencv官网下载
下载完后解压缩并重命名为opencv(源码包大小大概90M):
unzip opencv.zip
mv opencv-master opencv
1)进入到下载好的opencv目录中,新建目录build并进入build目录:
cd opencv
mkdir build
cd build
2)使用cmake配置opencv
$ cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES ..
命令说明:
-D OPENCV_GENERATE_PKGCONFIG=YES
OpenCV4以上默认不使用pkg-config
,该编译选项开启生成opencv4.pc
文件
3)使用make编译
make -j4
说明:
-j4
中的4指同时使用4个进程,可以根据电脑的进程数调整此值
4)使用make安装
sudo make install
默认安装路径为:opencv4.pc
文件的默认路径为/usr/local/lib/pkgconfig/opencv4.pc
,如果此目录下没有,可以使用以下命令搜索:
find / -iname opencv4.pc
将路径加入到PKG_CONFIG_PATH
:
sudo vim /etc/profile.d/pkgconfig.sh
在文件后面加入下面一行:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
保存并退出后激活:
source /etc/profile
验证是否成功:
$ pkg-config --libs opencv4
-L/usr/local/lib -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
打开文件(可能为空文件):
sudo vim /etc/ld.so.conf.d/opencv4.conf
在该文件末尾加上OpenCV的lib路径:
/usr/local/lib
使配置的路径生效:
sudo ldconfig
cd 到源码opencv/samples/cpp/example_cmake目录下,依次执行以下命令:
cmake .
make
./opencv_example
该测试需要电脑有摄像头,如果启动摄像头看到了画面,说明安装成功。
1)找到源码路径,在终端中进入到build目录,依次执行:
sudo make uninstall
cd ..
sudo rm -r build
2)在/usr/local下搜索opencv4
sudo find ./ -name opencv4*
将找到的opencv相关项删除即可,大概删除以下项:
sudo rm -r /usr/local/include/opencv4
sudo rm -r /usr/local/lib/pkgconfig/opencv4.pc
sudo rm -r /usr/local/lib/cmake/opencv4
sudo rm -r /usr/local/share/licenses/opencv4
sudo rm -r /usr/local/share/opencv4
在project(test_cpp)
后面加入下面一句:
find_package(OpenCV REQUIRED)
在add_executable(test_cpp main.cpp)
后面加入下面一句:
target_link_libraries(test_cpp PRIVATE ${OpenCV_LIBS})
一个简单的参考示例如下:
cmake_minimum_required(VERSION 3.15)
project(test_cpp)
find_package(OpenCV REQUIRED)
set(CMAKE_CXX_STANDARD 11)
# 设置可执行文件的存放路径,当读取项目下的图片时可正常的使用相对路径,而不需要在前面加上../
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
add_executable(test_cpp main.cpp)
target_link_libraries(test_cpp PRIVATE ${OpenCV_LIBS})
说明:
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
将可执行文件生成在项目的根目录下(clion默认生成在cmake-build-debug中),这样做,当读取本项目下存放的一张图片时,使用相对路径可直接写成’./image/test.jpg’,而不需要写成’…/image/test.jpg’