1.准备好与vs版本对应的OpenCV,下载网址:https://opencv.org/release/
2.opencv 配置环境变量
①. 在环境变量中添加:OPENCV_DIR,它的值指向OpenCV安装目录:opencv/build/x64/vc15
②. 在path环境变量添加:%OPENCV_DIR%\bin
3.配置visual studio
①.添加依赖库头文件
vs中右键项目进入属性->C/C+±>General->Addition Include Directories添加opencv的库目录:(OPENCV_DIR)…\include,如下图
②. 添加依赖库
在选项:Linker->General->Addition Library Directories,添加:$(OPENCV_DIR)\lib
③. 动态链接库
在选项:Linker->Input->Additional Dependencies, 添加项目需要的库。
其中Release版本需要:opencv_world340.lib
Debug版本需要:opencv_world340d.lib
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: " << argv[0] << " ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if( image.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}