环境
fedora 29 workstation x86_64
opencv 4.5.3
g++ (GCC) 8.3.1 20190223 (Red Hat 8.3.1-2)
CLion version 2021.2 build 212.4746.93
下载解压,并查看说明和文档教程
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.3.zip
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.3.zip
文档教程
tutorial_config_reference_general_contrib
编译与安装
$ mkdir -p build
$ cd build
$ cmake WITH_GDCM WITH_OPENNI2 WITH_ARAVIS WITH_XIMEA WITH_XINE WITH_LIBREALSENSE WITH_MFX WITH_GPHOTO2 WITH_TBB WITH_OPENMP WITH_QT WITH_OPENGL WITH_NGRAPH BUILD_DOCS -DCMAKE_BUILD_TYPE=Debug -DOPENCV_GENERATE_PKGCONFIG=ON -DOPENCV_DOWNLOAD_PATH=/tmp/opencv-cache -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -DCMAKE_INSTALL_PREFIX=/opt/opencv ../opencv
$ cmake --build .
$
$ sudo make install
编译过程中如果遇到boostdesc_bgm.i
等文件下载失败的问题,可以使用脚本文件的 download_with_curl.sh
或者 download_with_wget.sh
手动下载,参照 issues 1301 on github
opencv 示例程序
上面的步骤安装到了 /opt/opencv
,只需使用tutorial_linux_gcc_cmake配置 CMakeLists.txt
即可
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
opencv 示例代码 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], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
编译运行
$ cmake .
$ make
$
$ ./DisplayImage lena.jpg
图片位于源码的示例目录中 opencv-4.5.3/samples/data/lena.jpg
在 CLion 上运行示例程序
创建项目 c++ 可执行程序项目
可以看到 默认创建了 CMakiLists.txt
和 main.cpp
修改 CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(DisplayImage)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(CMAKE_CXX_STANDARD 11)
add_executable(DisplayImage main.cpp)
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
main.cpp
中修改为 opencv 示例代码。
然后运行配置中设置运行参数为需要打开的图片的路径。