操作系统:Ubuntu 20.04
OpenCV版本:OpenCV 4.5.1
执行下面命令:
#更新源
sudo apt-get update
#更新已安装的包
sudo apt-get upgrade
打开终端,安装以下软件包
sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
接下来获取 OpenCV 的 source 文件,直接到官网下载:https://opencv.org/releases/
点击对应版本的 Source 即可下载
下载成功后解压,解压到哪个文件夹都可以
下载完后解压缩并重命名为opencv
(源码包大小大概90M):
unzip opencv.zip
mv opencv-master opencv
1)进入到下载好的opencv
目录中,新建目录build
并进入build
目录:
cd opencv
mkdir build
cd build
因为OpenCV在debug和release两种模式下生产的库文件不尽相同,debug模式因为添加了很多调试信息,文件大小相对会大一些,所以这里分别编译debug和release两种模式下的OpenCV。 对于debug模式,执行如下命令:
cmake -D CMAKE_BUILD_TYPE=Debug -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_INSTALL_PREFIX=/usr/local/OpenCV/Debug -D WITH_FFMPEG=ON ..
这里有个地方需要注意,命令最后是两个点,是上一层目录的意思,因为Cmakelist.txt在build
的上层目录中。安装路径通过CMAKE_INSTALL_PREFIX设置为/usr/local/OpenCV/Debug
,默认是/usr/local
。而编译类型通过CMAKE_BUILD_TYPE=DEBUG设为DEBUG,默认是RELEASE。对于Release下的编译,只需要更改这两个部分即可,即:
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_INSTALL_PREFIX=/usr/local/OpenCV/Release -D WITH_FFMPEG=ON ..
使用make编译
make -j8
说明:-j8中的8指同时使用8个进程,可以根据电脑的进程数调整此值
使用make安装
sudo make install
默认安装路径为:
/usr/local/bin - executable files
/usr/local/lib - libraries (.so)
/usr/local/cmake/opencv4 - cmake package
/usr/local/include/opencv4 - headers
/usr/local/share/opencv4 - other files (e.g. trained cascades in XML format)
在/etc/ld.so.conf.d/
加入对于库的.conf
文件,首先新建opencv.conf
sudo gedit /etc/ld.so.conf.d/opencv.conf
在opencv.conf
中加入函数库所在的目录:
/usr/local/OpenCV/Debug/lib
/usr/local/OpenCV/Release/lib
也可以在ld.so.conf
文件中
sudo gedit /etc/ld.so.conf
加入上述目录。其实ld.so.conf
中写着如下代码:
include /etc/ld.so.conf.d/*.conf
说明ld.so.conf
是包含ld.so.conf.d
中所有的.conf
文件的。
接下来利用ldconfig命令,将/etc/ld.so.conf.d
中的数据读入缓存
sudo ldconfig
然后添加pkg-config环境变量,便于pkg-config找到*.pc文件。因为我们生成了两个版本的OpenCV,为了便于区分,分别把Debug和Release下lib/pkgconfig下(/usr/local/OpenCV/Debug/lib/pkgconfig,/usr/local/OpenCV/Release/lib/pkgconfig)对应的*.pc文件改名为opencv-debug和opencv-release。
sudo mv opencv4.pc opencv4_release.pc
sudo mv opencv4.pc opencv4_debug.pc
然后把这两个路径设置在环境变量中,
sudo gedit /etc/profile
把以下代码写入文件的最后并保存。
export PKG_CONFIG_PATH=/usr/local/OpenCV/Debug/lib/pkgconfig:$PKG_CONFIG_PATH
export PKG_CONFIG_PATH=/usr/local/OpenCV/Release/lib/pkgconfig:$PKG_CONFIG_PATH
或者,直接把这两文件放到一起,然后放到某个位置,再把这个路径设置到环境变量中。pkg-config的默认搜索路径是/usr/local/lib/config
,所以可以直接放在这个路径下,无需设置环境变量。
保存并退出后激活:
source /etc/profile
验证是否成功:
pkg-config --libs opencv4_debug
-L/usr/local/OpenCV/Debug/lib -lopencv_gapi -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_calib3d -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
pkg-config --libs opencv4_release
-L/usr/local/OpenCV/Release/lib -lopencv_gapi -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_calib3d -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
这样OpenCV的安装就完成了,可以参考Using OpenCV with gcc and CMake中讲解的OpenCV使用方法尝试测试几个简单的程序试试。
cd 到/opencv/samples/cpp/example_cmake
目录下,依次执行以下命令:
cmake .
make
./opencv_example
cmake有可能会报错,原因是找不到Opencv的配置文件。解决办法:打开CMakeLists.txt
#加入这行代码,指向OpenCVConfig.cmake
set (OpenCV_DIR /home/****/opencv/opencv-4.5.1/build)#opencv的安装路径
find_package(OpenCV REQUIRED)
该测试需要电脑有摄像头,如果启动摄像头看到了画面,说明安装成功。
在终端输入指令 pkg-config --modversion opencv4 即可看到OpenCV版本信息输出
sudo cp -f /usr/local/OpenCV/Debug/lib/pkgconfig/opencv4_debug.pc /usr/lib/pkgconfig/
pkg-config --modversion opencv4_debug
sudo cp -f /usr/local/OpenCV/Release/lib/pkgconfig/opencv4_release.pc /usr/lib/pkgconfig/
pkg-config --modversion opencv4_release
以下部分摘自:https://blog.csdn.net/qq_45915007/article/details/113000097.
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
在Ubuntu下,我们经常使用编写Cmake的方式来编译CV项目,CMakeLists.txt的语法比较简单,网上也有很多例子。
一个简单的参考示例如下:
# Time: xx:xx xx.x.xx
# Author: xxx
# This is a file - "CMakeLists.txt" for OpenCV Project
cmake_minimum_required ( VERSION 3.15 )
set(PROJECT_NAME Lambda)
set(SRC Lambda.cpp)
project(${PROJECT_NAME})
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIRS})
add_executable(${PROJECT_NAME} ${SRC})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
将CMakeLists.txt放置在源文件同级目录下,并执行以下指令
# 不一定要创建build文件夹,但是为了简洁,还是创建比较好
mkdir build
cd build
cmake ..
make
# 这里是你的可执行性文件名的名字
./Lambda
将下载好的opencv_contrib-4.5.1的压缩包解压到opencv下opencv-4.5.1的包中,然后在build文件夹下执行
cd build
cmake -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX=/usr/local/OpenCV/Debug -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -D WITH_TBB=ON -D WITH_EIGEN=ON -D WITH_FFMPEG=ON -D WITH_CUDA=ON -D OPENCV_ENABLE_NONFREE=ON ..
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/OpenCV/Release -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -D WITH_TBB=ON -D WITH_EIGEN=ON -D WITH_FFMPEG=ON -D WITH_CUDA=ON -D OPENCV_ENABLE_NONFREE=ON ..
这时会报错
make[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/build.make:63: modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
make[2]: Leaving directory '/home/pi/projects/opencv/build'
make[1]: *** [CMakeFiles/Makefile2:14259: modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
make[1]: Leaving directory '/home/pi/projects/opencv/build'
我们需要手动执行gen脚本(在我的情况下,从/ build上一级,从/opencv开始)。
因此,如果在/build中:
cd ..
python ./modules/python/src2/gen2.py ./build/modules/python_bindings_generator ./build/modules/python_bindings_generator/headers.txt
cd build
sudo make install
来源:
链接: https://github.com/opencv/opencv/issues/10771#issuecomment-376861139.
链接: https://github.com/opencv/opencv/issues/10771#issuecomment-379949822.
使用make编译安装OpenCV
make -j8
说明:-j8中的8指同时使用8个进程,可以根据电脑的进程数调整此值
使用make安装
sudo make install
sudo mv opencv4.pc opencv4_release.pc
sudo mv opencv4.pc opencv4_debug.pc
偶尔报错的时候,可以先make clean
然后再make -j8