下载地址:https://www.raoyunsoft.com/wordpress/index.php/2020/03/09/opencvdownload/
pkg-config --modversion opencv
环境:Ubuntu 20.04
使用g++和cmake编译opencv 4.5.0
① 安装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支持(首次安装了python的库,但make报错了,之后删了这两个库,若不使用python,建议不安装)
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
① 进入到下载好的opencv目录中,新建并进入目录build:
cd opencv
mkdir build
cd build
② 使用cmake配置opencv:
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES ..
说明:
-D OPENCV_GENERATE_PKGCONFIG=YES
OpenCV4以上默认不使用pkg-config,该编译选项开启生成opencv4.pc文件,支持pkg-config功能
③ 用make进行编译(大约需要35分钟)
make -j4
说明:
-j4中的4指同时使用4个进程,可以根据电脑的进程数调整此值
④ 用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)
opencv4.pc文件的默认路径:/usr/local/lib/pkgconfig/opencv4.pc
若此目录下没有,可以使用以下命令搜索:
sudo find / -iname opencv4.pc
sudo cp opencv4.pc opencv.pc
可以看到的确在这个目录下
将路径加入到PKG_CONFIG_PATH(用vim打开):
sudo vim /etc/profile.d/pkgconfig.sh
在文件后面加入下面一行:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
保存并退出后激活:
vim保存退出的方式->输入:wq 然后回车
# 激活
source /etc/profile
用以下命令验证是否成功:
pkg-config --libs opencv4
① 打开文件(可能为空文件):
sudo vim /etc/ld.so.conf.d/opencv4.conf
② 在该文件末尾加上OpenCV的lib路径,保存退出:
/usr/local/lib
③ 使配置的路径生效:
sudo ldconfig
————————————————
版权声明:本文为CSDN博主「tiffiny10」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44796670/article/details/115900538
如果出现:
/home/lyp/opencv-4.5.0/samples/cpp/example_cmake/example.cpp:1:10: fatal error: opencv2/core/core.hpp: No such file or directory
#include "opencv2/core/core.hpp"
^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/opencv_example.dir/build.make:81: recipe for target 'CMakeFiles/opencv_example.dir/example.cpp.o' failed
make[2]: *** [CMakeFiles/opencv_example.dir/example.cpp.o] Error 1
CMakeFiles/Makefile2:94: recipe for target 'CMakeFiles/opencv_example.dir/all' failed
make[1]: *** [CMakeFiles/opencv_example.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
make: *** [all] Error 2
可以考虑是不是链接库路径出现问题。
opencv4.5.0安装后,opencv2的路径在/usr/local/include/opencv4/opencv2/
所以需要将opencv2链接到include路径下
sudo ln -s /usr/local/include/opencv4/opencv2/ /usr/local/include/opencv2
cd 到/opencv/samples/cpp/example_cmake目录下,依次执行以下命令:
cmake .
make
./opencv_example
该测试需要电脑有摄像头,若启动摄像头看到了画面,说明安装成功。
下面简单构建一个显示图片的C++程序
① 在桌面新建文件夹ImageShow
② 进入该文件夹,打开终端,输入:
touch main.cpp
③ 用文本编辑器打开main.cpp,输入如下代码:
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
cout<<"usage: DisplayImage.out \n" ;
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
cout<<"No image data \n";
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
④ 终端输入:
touch CMakeLists.txt
⑤ 用文本编辑器打开CMakeLists.txt,输入如下代码:
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Define project name
project(ImageShow)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Declare the executable target built from your sources
add_executable(ImageShow main.cpp)
# Link your application with OpenCV libraries
target_link_libraries(ImageShow PRIVATE ${OpenCV_LIBS})
⑥ 在该文件夹下加入一张名为flower.jpg的图片,用于测试
⑦ 在终端依次输入如下代码,构建并运行程序:
cmake .
make
./ImageShow flower.jpg