简体   繁体   中英

Exception under opencv 3.0 and mingw under windows 7 when using AKAZE

I want to use AKAZE, which is integraded in OpenCV 3.0. For this I've tested the following code:

#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();

}

After line akaze->detectAndCompute(img1, noArray(), kpts1, desc1); the following exception was thrown:

 OpenCV Error: Insufficient memory (Failed to allocate 72485160 bytes) in OutOfMemoryError, file C:\opencv\sources\modules\core\src\alloc.cpp, line 52.
 OpenCV Error: Assertion failed (u != 0) in create, file C:\opencv\sources\modules\core\src\matrix.cpp, line 411 terminate called after throwing an instance of 'cv::Exception'
 what():  C:\opencv\sources\modules\core\src\matrix.cpp:411: error: (-215) u != 0

I've compiled OpenCV mit mingw 4.92 under Windows 7.

Has somebody an answer?

thank you

More of a comment, than an answer, but I am unable to comment.

As the error states, you seem to be running out of memory while processing the A-KAZE detection. In one of my tests, (although my images were 4160x2340), processing three detection modules one after the other easily took around 7-8 GB of memory. What resolution are your images at, and how much RAM do you have?

Also, if you compile this application as 32-bit, it will not be able to allocate more than 4 GB (2 if you yourself are on a 32-bit OS). Are you on 32-bit or 64-bit, and if the latter, are you compiling it as a 64-bit application? One possible solution would be to just resize your image so that it has lesser pixels and requires lesser memory:

cv::resize(sourceImage, destinationImage, Size(), 0.5, 0.5, interpolation); // Halves the resolution  

But this is a last resort, because higher resolution means more features and precision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM