参考:
https://docs.opencv.org/4.x/d7/d9f/tutorial_linux_install.html
Build with opencv_contrib
# 1. Install minimal prerequisites, libgtk2.0-dev pkg-config 用来显示图像
sudo apt update && sudo apt install -y cmake g++ wget unzip libgtk2.0-dev pkg-config
# 2. Download and unpack sources
# cd 到自己要放 opencv 的路径下,比如我们的是:/home/h/programs/cv
cd /home/h/programs/cv
sudo wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip --no-check-certificate
sudo wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.2.0.zip --no-check-certificate
unzip opencv.zip && unzip opencv_contrib.zip
# 3. Create build directory and switch into it
mkdir build && cd build
# 4. Configure, build 目录内
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.2.0/modules ../opencv-4.2.0
# 5. build 目录内
make -j 8
# 删除压缩包
cd .. && sudo rm opencv.zip opencv_contrib.zip
编译时候报错:opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:673:20: fatal error: boostdesc_bgm.i: No such file or directory
opencv_contrib-4.2.0/modules/xfeatures2d/src/boostdesc.cpp:654:20: fatal error: boostdesc_bgm.i: 没有那个文件或目录
解决:网络问题,添加代理,然后重复 4,5 步
参考:https://www.cnblogs.com/arxive/p/11778731.html
Using OpenCV with gcc and CMake
参考:https://docs.opencv.org/4.x/db/df5/tutorial_linux_gcc_cmake.html
// DisplayImage.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], IMREAD_COLOR );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
set(OpenCV_DIR /home/h/programs/cv/build)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
Create build directory and switch into it
mkdir build && cd build
目录:
.
├── build
├── CMakeLists.txt
└── DisplayImage.cpp
cmake ..
报错: By not providing “FindOpenCV.cmake” in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by “OpenCV”, but
CMake did not find one.
Could not find a package configuration file provided by “OpenCV” with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of “OpenCV” to CMAKE_PREFIX_PATH or set
“OpenCV_DIR” to a directory containing one of the above files. If “OpenCV”
provides a separate development package or SDK, be sure it has been
installed.
解决:
方式1:
# CMakeLists.txt:
set(OpenCV_DIR /home/h/programs/cv/build)
方式2:
cmake -DOpenCV_DIR=/home/h/programs/cv/build ..
然后:
# 在build 文件夹下
make
./DisplayImage lena.jpg
报错:
Gtk-Message: 23:48:54.655: Failed to load module “canberra-gtk-module”
解决:
sudo apt-get install libcanberra-gtk-module
其他:
为python3构建opencv:https://stackoverflow.com/a/39409570