33,311
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
//#include "opencv2/core/core.hpp"
//#include "opencv2/features2d/features2d.hpp"
//#include "opencv2/highgui/highgui.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/xfeatures2d/nonfree.hpp"
#include <opencv2/imgproc.hpp> // Gaussian Blur
#include <opencv2/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp> // OpenCV window I/O
#include <opencv2/features2d.hpp>
#include <opencv2/objdetect.hpp>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using namespace cv;
vector<string> paths =
{ "d:\\images\\fbl.jpg",
"d:\\images\\fpl.jpg" ,
"d:\\images\\lsa.jpg",
"d:\\images\\mjh.jpg",
"d:\\images\\mls.jpg",
"d:\\images\\xsy.jpg",
"d:\\images\\ysy.jpg",
"d:\\images\\ysw.jpg",
"d:\\images\\bbf.jpg",
"d:\\images\\gsn.jpg",
"d:\\images\\ysk.jpg",
"d:\\images\\yss.jpg",
"d:\\images\\ysq.jpg"
};
const char *windowvideoName = "videoCap window";
const char *windowmatchName = "match window";
const int CLIP_WIDTH = 360;
const int CLIP_HEIGHT = 200;
const int TOP_MATCH_COUNT = 25;
const float NOT_SURE_RATE_THRESHOD = 0.5f; //0---1
const float SURE_RATE_THRESHOD = 0.8f; //0---1
const float DISTANCE_THRESHOD = 0.16f;
const bool BREAK_ON_SUCCESS = false;
const bool USE_SIFT = false;
int IMGS = 0;
vector<vector<KeyPoint>> keypoints;
vector<Mat> descriptors;
vector<Mat> srcs;
vector<double> rates;
Rect CLIP_EREA;
Ptr<BFMatcher> matcher = BFMatcher::create(NORM_L2, false);
Ptr<FeatureDetector> detector;
Ptr<FeatureDetector> extractor;
void init()
{
if (USE_SIFT) {
detector = xfeatures2d::SiftFeatureDetector::create();
extractor = xfeatures2d::SiftFeatureDetector::create();
}
else
{
detector = xfeatures2d::SurfFeatureDetector::create(400);
extractor = xfeatures2d::SurfDescriptorExtractor::create(400);
}
IMGS = paths.size();
keypoints.resize(IMGS);
descriptors.resize(IMGS);
srcs.resize(IMGS);
rates.resize(IMGS);
for (int i = 0; i < IMGS; i++)
{
srcs[i] = imread(paths[i]);
detector->detect(srcs[i], keypoints[i]);
extractor->compute(srcs[i], keypoints[i], descriptors[i]);
}
}
CvScalar rankCoror(float rate)
{
if (rate < 0.1) { return CV_RGB(150, 0, 0); }
if (rate < 0.25&&rate >= 0.1) { return CV_RGB(0, 0, 255); }
if (rate < NOT_SURE_RATE_THRESHOD&&rate >= 0.25) { return CV_RGB(0, 100, 255); }
if (rate < SURE_RATE_THRESHOD&&rate >= NOT_SURE_RATE_THRESHOD) { return CV_RGB(0, 255, 100); }
if (rate >= NOT_SURE_RATE_THRESHOD) { return CV_RGB(0, 255, 0); }
return CV_RGB(150, 0, 0);
}
void main2()
{
init();
namedWindow(windowvideoName);
namedWindow(windowmatchName);
VideoCapture VideoStream(0);
Mat videocapture;
Mat clip;
do
{
VideoStream >> videocapture;
CLIP_EREA.x = 0;
CLIP_EREA.y = 0;
CLIP_EREA.width = videocapture.cols;
CLIP_EREA.height = videocapture.rows;
clip = Mat(videocapture);
int rows = clip.rows;
int cols = clip.cols;
if (cols > CLIP_WIDTH)
{
clip = clip.colRange((cols - CLIP_WIDTH) / 2, (cols + CLIP_WIDTH) / 2);
CLIP_EREA.x = (cols - CLIP_WIDTH) / 2;
CLIP_EREA.width = CLIP_WIDTH;
}
if (rows > CLIP_HEIGHT)
{
clip = clip.rowRange(0, CLIP_HEIGHT);
CLIP_EREA.height = CLIP_HEIGHT;
}
vector<KeyPoint> keypoint2;
Mat descriptor2;
vector<DMatch>matches;
detector->detect(clip, keypoint2);
if (keypoint2.size() == 0) { continue; }
extractor->compute(clip, keypoint2, descriptor2);
for (int i = 0; i < IMGS; i++)
{
matcher->match(descriptors[i], descriptor2, matches);
//std::sort(matches.begin(), matches.end());
nth_element(matches.begin(), matches.begin() + TOP_MATCH_COUNT, matches.end());
matches.erase(matches.begin() + TOP_MATCH_COUNT, matches.end());
double dist = 0;
int count = 0;
for each (DMatch m in matches)
{
if (m.distance <= DISTANCE_THRESHOD)
{
count++;
}
dist += m.distance;
}
float rate1 = count*1.0f / matches.size();
rates[i] = rate1;
if (rate1 >= NOT_SURE_RATE_THRESHOD)
{
Mat output;
drawMatches(srcs[i], keypoints[i], clip, keypoint2, matches, output);
imshow(windowmatchName, output);
cout << "MATCH: " << paths[i] << "rate1: " << rate1 << " rate2: " << dist / matches.size() << endl;
if (rate1 >= SURE_RATE_THRESHOD&&BREAK_ON_SUCCESS)
{
break;
}
}
}
for (int x = 0; x < IMGS; x++)
{
char temp[150];
sprintf(temp, "%s : RATE: %.2f", paths[x].c_str(), (float)rates[x]);
putText(videocapture, temp, Point(10 + (x % 2) * 300, 10 + x * 10), FONT_HERSHEY_SIMPLEX, 0.5, rankCoror(rates[x]), 2);
}
rectangle(videocapture, CLIP_EREA, CV_RGB(0, 0, 255));
imshow(windowvideoName, videocapture);
} while (waitKey(30) < 0);
}