64,643
社区成员
发帖
与我相关
我的任务
分享
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\nonfree\features2d.hpp>
#include<opencv2\features2d\features2d.hpp>
#include<iostream>
#include <fstream>//为了调用“.csv”文件
#include<string>
using namespace std;
using namespace cv;
string outputPath="E:\\graduate_work\\audio_process\\FrameORB\\FrameORB\\Megamind.csv";
bool checkFile(string name) {
if(FILE *file = fopen(name.c_str(),"r")) {
fclose(file);
return true;
}
return false;
}
bool checkOutputFile(string name) {
if(FILE *file = fopen(name.c_str(),"w")) {
fclose(file);
return true;
}
return false;
}
void writeOutputFile(string outFile, vector< int > goodMatchsKeypointsNum) {
ofstream file (outFile.c_str());
for(int i = 0; i < goodMatchsKeypointsNum.size(); i++) {
file <<goodMatchsKeypointsNum[i]<< endl;
}
file.close();
}
int main()
{
if(checkFile(outputPath)) {
while(true) {
string in;
cout << "File '" << outputPath << "' already exists. Overwrite [y/N]? ";
getline(std::cin,in);
if(in == "Y" || in == "y") {
break;
}
if(in == "" || in == "N" || in == "n") {
return 0;
}
}
}else {
if(!checkOutputFile(outputPath)) {
cout << "The outputFilePath seems to be invalid or cannot be written" << endl;
return 1;
}
}
//VideoCapture capture("F:\\movie_clip\\Mr.Nobody.mkv");
VideoCapture capture("C:\\Users\\801\\Desktop\\Megamind.avi");
if (!capture.isOpened())
{
cout<<"fail to open!"<<endl;
}
//计算视频的总帧数
long totalFrameNumber=capture.get(CV_CAP_PROP_FRAME_COUNT);
cout<<"整个视频"<<totalFrameNumber<<"帧"<<endl;
//定义一个用来控制读取视频循环结束的变量
bool stop = false;
int flag=1;
//承载每一帧的图像
Mat frame;
//设置开始帧
long currentFrame=0;
Mat tempImage;
vector<int> goodMatchsKeypointsNum;//用于存储所有的关键点
while(!stop)
{
//读取一帧
if(!capture.read(frame))
{
cout<<"读取视频失败"<<endl;
return -1;
}
cout<<currentFrame<<",";
if (flag==1&¤tFrame==1)
{
tempImage=frame.clone();
flag=-1;
}else if(currentFrame>1)
{
//【1】对载入的图片太大得要进行尺寸的缩小
Mat srcImage1;
Mat srcImage2;
resize(tempImage,srcImage1,Size(tempImage.cols/6,tempImage.rows/6),(0,0),(0,0),3);
resize(frame,srcImage2,Size(frame.cols/6,frame.rows/6),(0,0),(0,0),3);
Mat grayImage1;
Mat grayImage2;
cvtColor(srcImage1,grayImage1,CV_BGR2GRAY);
cvtColor(srcImage2,grayImage2,CV_BGR2GRAY);
//参数定义
OrbFeatureDetector featureDetector;
vector<KeyPoint>keyPoints1;
vector<KeyPoint>keyPoints2;
Mat descriptors1;
Mat descriptors2;
//调用detect函数检测出特征关键点,保存在vector容器中
featureDetector.detect(grayImage1,keyPoints1);
featureDetector.detect(grayImage2,keyPoints2);
//计算描述符
OrbDescriptorExtractor featureExtractor;
featureExtractor.compute(grayImage1,keyPoints1,descriptors1);
featureExtractor.compute(grayImage2,keyPoints2,descriptors2);
//基于FLANN的描述符对象匹配
flann::Index flannIndex(descriptors1,flann::LshIndexParams(12,20,2),cvflann::FLANN_DIST_HAMMING);
//匹配和测试描述符,获取两个最邻近的描述符
Mat matchIndex(descriptors2.rows,2,CV_32SC1),
matchDistance(descriptors2.rows,2,CV_32FC1);
flannIndex.knnSearch(descriptors2,matchIndex,matchDistance,2,flann::SearchParams());//调用K邻近算法
//根据劳氏算法选出优秀匹配
//vector<DMatch> goodMatchs;
int goodMatchsNum=0;
for(int i=0;i<matchDistance.rows;i++)
{
if (matchDistance.at<float>(i,0)<0.6*matchDistance.at<float>(i,1))
{
DMatch dmatches(i,matchIndex.at<int>(i,0),
matchDistance.at<float>(i,0));
//goodMatchs.push_back(dmatches);
goodMatchsNum++;
}
}
cout<<goodMatchsNum<<endl;
goodMatchsKeypointsNum.push_back(goodMatchsNum);
tempImage=frame.clone();
}
//按下ESC或者到达指定的结束帧后退出读取视频
if( currentFrame == totalFrameNumber-1)
{
stop = true;
}
++currentFrame;
}
writeOutputFile(outputPath,goodMatchsKeypointsNum);
return 0;
}