目录
1.下载opencv
如果需要 xfeatures2D的话下载opencv_contribute,3.4
mirrors / opencv / opencv · GitCode
vs新建,打开此界面配置debug 64
配置链接,vs2017 debug 用带d的lib和vc15
系统环境变量path新增
重启vs
测试代码
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = imread("D:/.imageregpy/pic/peach/1.jpg", 1); //0为灰度图
imshow("input", src);
waitKey(0);
destroyAllWindows();
return 0;
}
2.SITFT特征提取
转载
SIFT特征提取算法理解与实现 | Granvallen;Nest
代码
GitHub - Granvallen/SIFT: SIFT特征提取算法C++与Matlab实现
3.各种特征提取的方法 opencv
1.使用FLANN进行特征点匹配
转载(9条消息) OpenCV角点检测—FLANN快速最近邻进行特征点匹配(9)_flannbasedmatcher_思考之路的博客-CSDN博客
# include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
Mat imgage1= imread("../1.png", 1);
Mat imgage2 = imread("../2.png", 1);
if(!imgage1.data && !imgage2.data) {
printf("图像路径错误!\n");
return false;
}
int minHessian = 300;
Ptr<SIFT > detector = SIFT::create(minHessian);
std::vector<KeyPoint> keypoints1, keypoints2;
detector->detect(imgage1, keypoints1);
detector->detect(imgage2, keypoints2);
Ptr<SIFT> extractor = SIFT::create() ;
Mat descriptors1, descriptors2;
extractor->compute(imgage1, keypoints1, descriptors1);
extractor->compute(imgage2, keypoints2, descriptors2);
//采用FLANN算法匹配描述符向量
Ptr<FlannBasedMatcher> matcher = FlannBasedMatcher::create();
std::vector<DMatch> matches;
matcher->match(descriptors1, descriptors2, matches);
double max_dist = 0;
double min_dist = 100;
//快速计算关键点之间的最大和最小距离
for(int i = 0; i < descriptors1.rows; i++)
{
double dist = matches[i].distance;
if(dist < min_dist) min_dist = dist ;
if(dist > max_dist) max_dist = dist ;
}
//输出距离信息
printf("最大距离:%f\n", max_dist );
printf("最小距离:%f\n", min_dist);
//使用符合条件的匹配结果(距离小于2 * min_dist ),
std::vector<DMatch> good_matches;
for(int i = 0; i < descriptors1.rows; i++)
{
if(matches[i].distance < 2 * min_dist)
{
good_matches.push_back(matches[i]);
}
}
//绘制出符合条件的匹配点
Mat img_matches;
drawMatches(imgage1, keypoints1, imgage2, keypoints2, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
//输出相关点信息
for(int i = 0; i < good_matches.size(); i++)
{
printf("符合条件的匹配点[%d] 特征点1:%d------- 特征点2:%d\n", i, good_matches[i].queryIdx, good_matches[i].trainIdx);
}
//显示效果图
imshow("匹配效果图", img_matches);
// waitKey(0);
while (char (waitKey(1)) != 'q'){}
return 0;
}
2.FLANN结合SIFT进行关键点的描述和匹配
# include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
//改变console字体颜色
// system("color 6F");
//[1]载入图片,显示并转化为灰图
Mat trainImage = imread("../1.png"), trainImage_gray;
imshow("原始图", trainImage);
cvtColor(trainImage, trainImage_gray,6);
//【2】检测SIFT关键点,提取训练图像描述符
vector<KeyPoint> train_keypoints;
Mat trainDescriptor;
int minHessian = 80;
Ptr<SIFT > detector = SIFT::create(minHessian);
detector->detect(trainImage_gray, train_keypoints);
Ptr<SIFT> extractor = SIFT::create() ;
extractor->compute(trainImage_gray, train_keypoints, trainDescriptor);
//[3]创建基于FLANN的描述符匹配对象;
FlannBasedMatcher matcher;
vector<Mat> train_desc_collection(1, trainDescriptor); //?????????????
matcher.add(train_desc_collection);
matcher.train();
//[4]创建视频对象,定义帧率
VideoCapture cap(0);
unsigned int frameCount = 0; //帧数
//【5】不断循环直到按下q
while (char (waitKey(1)) != 'q')
{
//<1>参数设置
int64 time0 = getTickCount();
Mat captureImage, captureImage_gray;
cap >> captureImage; //采集视频到captureImage中
if(captureImage.empty()) {
continue;
}
//<2>转化图像到灰度图
cvtColor(captureImage, captureImage_gray, 6);
//[3]检测S关键点,提取测试图像描述符
vector<KeyPoint> test_keypoint;
Mat testDescriptor;
detector->detect(captureImage_gray, test_keypoint);
extractor->compute(captureImage_gray, test_keypoint, testDescriptor);
//<4>匹配训练和测试描述符
vector<vector<DMatch>> matches ;
matcher.knnMatch(testDescriptor, matches, 2);
//[5]根据劳式算法(Low s alogrithm)得到好的匹配
vector<DMatch> goodMatches;
for(unsigned int i = 0; i < matches.size(); i++)
{
if(matches[i][0].distance < 0.6 * matches[i][1].distance)
goodMatches.push_back(matches[i][0]);
}
//[6]绘制匹配点并显示窗口
Mat dstImage;
drawMatches(captureImage, test_keypoint, trainImage,train_keypoints, goodMatches, dstImage);
imshow("匹配窗口", dstImage);
//[7]输出帧率信息
cout << "当前帧率为:" << getTickFrequency() / (getTickCount() - time0) << endl;
}
return 0;
}