opencv 3.0和mingw在windows 7下使用AKAZE时出现异常
Exception under opencv 3.0 and mingw under windows 7 when using AKAZE
我想使用AKAZE,它集成在OpenCV 3.0中。为此,我测试了以下代码:
#include <opencv2/features2d.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
#include <qcoreapplication.h>
#include <QDebug>
using namespace std;
using namespace cv;
const float inlier_threshold = 2.5f; // Distance threshold to identify inliers
const float nn_match_ratio = 0.8f; // Nearest neighbor matching ratio
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Mat img1 = cv::imread("img1.jpg",IMREAD_GRAYSCALE);
Mat img2 = imread("img2.jpg", IMREAD_GRAYSCALE);
Mat homography;
FileStorage fs("H1to3p.xml", FileStorage::READ);
fs.getFirstTopLevelNode() >> homography;
vector<KeyPoint> kpts1, kpts2;
Mat desc1, desc2;
Ptr<AKAZE> akaze = AKAZE::create();
//ERROR after detectAndCompute(...)
akaze->detectAndCompute(img1, noArray(), kpts1, desc1);
akaze->detectAndCompute(img2, noArray(), kpts2, desc2);
BFMatcher matcher(NORM_HAMMING);
vector< vector<DMatch> > nn_matches;
matcher.knnMatch(desc1, desc2, nn_matches, 2);
vector<KeyPoint> matched1, matched2, inliers1, inliers2;
vector<DMatch> good_matches;
for(size_t i = 0; i < nn_matches.size(); i++) {
DMatch first = nn_matches[i][0];
float dist1 = nn_matches[i][0].distance;
float dist2 = nn_matches[i][1].distance;
if(dist1 < nn_match_ratio * dist2) {
matched1.push_back(kpts1[first.queryIdx]);
matched2.push_back(kpts2[first.trainIdx]);
}
}
for(unsigned i = 0; i < matched1.size(); i++) {
Mat col = Mat::ones(3, 1, CV_64F);
col.at<double>(0) = matched1[i].pt.x;
col.at<double>(1) = matched1[i].pt.y;
col = homography * col;
col /= col.at<double>(2);
double dist = sqrt( pow(col.at<double>(0) - matched2[i].pt.x, 2) +
pow(col.at<double>(1) - matched2[i].pt.y, 2));
if(dist < inlier_threshold) {
int new_i = static_cast<int>(inliers1.size());
inliers1.push_back(matched1[i]);
inliers2.push_back(matched2[i]);
good_matches.push_back(DMatch(new_i, new_i, 0));
}
}
Mat res;
drawMatches(img1, inliers1, img2, inliers2, good_matches, res);
imwrite("res.png", res);
double inlier_ratio = inliers1.size() * 1.0 / matched1.size();
cout << "A-KAZE Matching Results" << endl;
cout << "*******************************" << endl;
cout << "# Keypoints 1: t" << kpts1.size() << endl;
cout << "# Keypoints 2: t" << kpts2.size() << endl;
cout << "# Matches: t" << matched1.size() << endl;
cout << "# Inliers: t" << inliers1.size() << endl;
cout << "# Inliers Ratio: t" << inlier_ratio << endl;
cout << endl;
return a.exec();
}
在akaze->detectAndCompute(img1, noArray(), kpts1, desc1);
行之后抛出了以下异常:
OpenCV Error: Insufficient memory (Failed to allocate 72485160 bytes) in OutOfMemoryError, file C:opencvsourcesmodulescoresrcalloc.cpp, line 52.
OpenCV Error: Assertion failed (u != 0) in create, file C:opencvsourcesmodulescoresrcmatrix.cpp, line 411 terminate called after throwing an instance of 'cv::Exception'
what(): C:opencvsourcesmodulescoresrcmatrix.cpp:411: error: (-215) u != 0
我已经在Windows 7下编译了OpenCV mitmingw4.92。
有人回答吗?
谢谢
更多的是评论,而不是回答,但我无法评论。
作为错误状态,您似乎在处理A-KAZE检测时内存不足。在我的一个测试中(尽管我的图像是4160x2340),一个接一个地处理三个检测模块很容易占用大约7-8 GB的内存。您的图像的分辨率是多少,您有多少RAM ?
同样,如果您将此应用程序编译为32位,它将无法分配超过4 GB(如果您自己在32位操作系统上,则为2 GB)。您使用的是32位还是64位,如果是后者,您是否将其编译为64位应用程序?一种可能的解决方案是调整图像的大小,使其具有更少的像素并需要更少的内存:
cv::resize(sourceImage, destinationImage, Size(), 0.5, 0.5, interpolation); // Halves the resolution
但这是最后的手段,因为更高的分辨率意味着更多的特征和精度。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 尝试使用智能指针时引发异常
- 函数如何通知用户它基于函数原型抛出异常?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- opencv 3.0和mingw在windows 7下使用AKAZE时出现异常