flann::Index flannIndex(descriptors1,flann

猫猫与橙子 2016-07-29 02:26:38

#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;
}

这是用ORB找出连续视频帧最佳匹配特征点对的数量的一段代码;
在运行过程中:
到flannIndex.knnSearch(descriptors2,matchIndex,matchDistance,2,flann::SearchParams());//调用K邻近算法进行匹配时
有时会出现这样的错误:
OpenCV Error: Assertion failed (query.isContinuous() && indices.isContinuous() && dists.isContinuous()) in cv::flann::runKnnSearch_, file C:\builds\2_4_PackSlave-win64-vc11-shared\opencv\modules\flann\src\miniflann.cpp, line 470
不知道为什么?
...全文
711 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
SHANDINGQUAN 2018-08-25
  • 打赏
  • 举报
回复
办法之一:捕获异常,然后跳过异常。
try
{
flannIndex.knnSearch(imageDesc2, macthIndex, matchDistance, 2, flann::SearchParams());
}
catch (exception ex)
{
fprintf(stderr, "Exception-knnSearch\n", ex.what());
continue;
}
qq_36701957 2017-04-11
  • 打赏
  • 举报
回复
我把弹出来的对话框允许使用摄像头设置成不再弹出此消息就好了。
VNanyesheshou 2016-10-31
  • 打赏
  • 举报
回复
这个问题解决了吗,我也碰见这个问题了,我的是直接就是这个问题,不管是否是黑色图。
猫猫与橙子 2016-08-01
  • 打赏
  • 举报
回复
您知道原理性信息吗?我在用这个算法的时候,发现当视频帧在渐变(这里指视频变化时慢慢变黑再出现画面)出现连续帧都为黑色图时,就会出现这个问题,但是没有黑色图(出现连续的黑频帧)时,是不会出现这个问题的。
赵4老师 2016-07-29
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧