第一步:下载OpenCV source
https://github.com/opencv/opencv/archive/4.5.0.zip
第二步:
解压下载的文件,然后在解压后的文件夹下新建build文件夹,执行
cmake ..
make
第三步:
随便找个地方新建example文件夹
写测试代码DisplayImage.cpp如下
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
cout<<"usage: DisplayImage.out <Image_Path>\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;
}
然后写CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
set(OpenCV_DIR /ffmpeg/opencv/opencv-4.5.0/build/)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
其中/ffmpeg/opencv/opencv-4.5.0/build/为OpencvConfig.cmake的路径,这个里面包含了OpenCV_INCLUDE_DIRS和OpenCV_LIBS等路径
然后在当前文件夹下创建build文件夹,使用
cmake ..
make
即能生成可执行文件DisplayImage,然后在当前build文件夹下执行
./DisplayImage to//path/image
即可看到图像显示,表示OpenCV已正常设置