本文链接:blog.csdn.net/Dango_mirac…
【一.准备工作】
下载 OpenCV2.4.13.4 opencv.org/releases.ht… 并解压 我是解压到 C:\opencv24134
下载 Cmake cmake.org/download/ 并安装 我安装的是 cmake-3.10.0-win64-x64.msi
【二.编译配置】
解压后的OpenCV文件夹中有两个自文件夹,一个是sources,即用于编译的源文件;另一个是已经编译好的build,build对我们没有用,可以删掉了。新建一个build\vc15文件夹。 打开新安装的 CMake (cmake-gui) ,设置路径如下(根据自己解压的位置来):
【三.更改系统环境】
在系统变量PATH中添加你编译的OpenCV路径,按照我的路径就是: D:\opencv\build\x86\vc14\install\lib 然后把 D:\opencv\build\x86\vc14\install\bin 下的所有dll文件复制到 C:\Windows\SysWOW64以及C:\Windows\System32
【四.配置VS2017】
继续配置以下内容:
【五.测试】
新建cpp文件,输入以下代码:
测试1:
#include
#include<core/core.hpp>
#include<highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("D:\img.jpg");//图片路径
if (!image.data)
{
cout << "图片读取失败!" << endl;
system("pause");
return -1;
}
namedWindow("Img", 1);
imshow("Lena", image);
waitKey();
return 0;
}
运行,若能成功显示图片则配置成功。
测试2:
// opencvtest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include "pch.h"
#include <stdio.h>
#include
#include <core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
#include<opencv2/legacy/legacy.hpp>
#include "opencv2/nonfree/features2d.hpp" //SurfFeatureDetector实际在该头文件中
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = imread("01.png", 1);
Mat src2 = imread("02.png", 1);//旋转后
if (!src.data || !src2.data)
{
cout << " --(!) Error reading images " << endl;
}
//1--初始化SIFT检测算子
int minHessian = 40;
SiftFeatureDetector detector(minHessian);
//2--使用SIFT算子检测特征点
vector<KeyPoint> kp1, kp2;
detector.detect(src, kp1);
detector.detect(src2, kp2);
//--绘制特征点
Mat keypointImg, keypointImg2;
drawKeypoints(src, kp1, keypointImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
imshow("SIFT kp1", keypointImg);
cout << "keypoint1 number: " << kp1.size() << endl;
drawKeypoints(src, kp2, keypointImg2, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
imshow("SIFT kp2", keypointImg2);
cout << "keypoint2 number: " << kp2.size() << endl;
//3--特征向量提取
SiftDescriptorExtractor extractor;
Mat descriptor1, descriptor2;
extractor.compute(src, kp1, descriptor1);
extractor.compute(src2, kp2, descriptor2);
//imshow("desc", descriptor1);
//cout << endl << descriptor1 << endl;
//4--特征匹配
BruteForceMatcher<L2<float>> matcher;
vector<DMatch> matches;
Mat img_matches;
matcher.match(descriptor1, descriptor2, matches);
//--绘制匹配
drawMatches(src, kp1, src2, kp2, matches, img_matches);
imshow("matches", img_matches);
waitKey(0);
return 0;
}