我使用OrbFeatureDetector和Flann处理来自PC摄像头的视频。当我调用 knnSearch 时,无论是发布还是调试,我都会遇到以下异常:
OpenCV错误:断言失败(query.isContinuous()&& indices.isContinuous()&& dists.isContinuous())in cv :: flann :: runKnnSearch_,file E:\ Library \ opencv2.4.12 \ sources \ modules \ flann \ src \ miniflann.cpp,第487行
通常这不会出现。通常在相机被遮盖时发生。
这是正常的吗?如何避免?
我的代码:
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
int main()
{
// LoadImage
Mat srcImage = imread("1.jpg");
imshow("Image to Find", srcImage);
Mat srcImage_gray;
cvtColor(srcImage, srcImage_gray, CV_BGR2GRAY);
// Detect key points from source image
OrbFeatureDetector featureDetector;
vector<KeyPoint> srcKeyPoints;
featureDetector.detect(srcImage_gray, srcKeyPoints);
// Compute description from source image
OrbDescriptorExtractor featureExtractor;
Mat description;
featureExtractor.compute(srcImage_gray, srcKeyPoints, description);
// FLANN
flann::Index flannIndex(description, flann::LshIndexParams(12, 20, 2), cvflann::FLANN_DIST_HAMMING);
// Initialize camera
VideoCapture cap(0);
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
uint32_t frameCount = 0;
// Start
while (true) {
Mat captureImage, captureImage_gray;
cap >> captureImage;
if (captureImage.empty())
continue;
cvtColor(captureImage, captureImage_gray, CV_BGR2GRAY);
// Detect key points from camera
vector<KeyPoint> captureKeyPoints;
Mat captureDescription;
featureDetector.detect(captureImage_gray, captureKeyPoints);
// Compute description from camera
featureExtractor.compute(captureImage_gray, captureKeyPoints, captureDescription);
// knnSearch
Mat matchIndex(captureDescription.rows, 2, CV_32SC1);
Mat matchDistance(captureDescription.rows, 2, CV_32FC1);
try {
// !!!! Here comes exceptions !!!!
flannIndex.knnSearch(captureDescription, matchIndex, matchDistance, 2, flann::SearchParams());
}
catch (Exception e) {
}
// Good matches
vector<DMatch> good;
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));
good.push_back(dmatches);
}
}
// Show Image
Mat resultImage;
drawMatches(captureImage, captureKeyPoints, srcImage, srcKeyPoints, good, resultImage);
imshow("Matches", resultImage);
if (char(waitKey(1)) == 27)
break;
}
return 0;
}