1

我编写了一个 c++ 应用程序,它比较来自图像的点匹配(OpenSurf C++),但有时,来自数千个“getUniqueMatches”的 1 个,应用程序在“getUniqueMatches”内的某个点中断。我有这个日志:

05/13/11 10:17:16: this->pointsA = 227
05/13/11 10:17:16: this->pointsB = 226
05/13/11 10:17:16: this->matches before = 0
05/13/11 10:17:16: 227 226 0 0.650000
05/13/11 10:17:16: Starting in getUniqueMatches

-- And here breaks, inside getUniqueMatches --

这是代码:

inline bool findInVector(std::vector<int> v, int value) {
    int size = v.size();
    for(int i=0; i<size; i++) {
        if(v[i] == value) {
            return true;
        }
    }

    return false;
}

void getUniqueMatches(IpVec &ipts1, IpVec &ipts2, IpPairVec &matches, float ratio) {
    try {
        wLog(f("%d %d %d %f", ipts1.size(), ipts2.size(), matches.size(), ratio));

        float dist, d1, d2;
        Ipoint *match;

        matches.clear();

        std::vector<int> matched;

        wLog("Starting in getUniqueMatches");

        // Breaks after here

        int size = ipts1.size();
        int size2 =  ipts2.size();

        for (int i = 0; i < size; i++) {
            d1 = d2 = FLT_MAX;

            int foundJ = -1;

            for (unsigned int j = 0; j < size2; j++) {
                dist = ipts1[i] - ipts2[j];

                if (dist < d1 && !findInVector(matched, j)) {
                    d2 = d1;
                    d1 = dist;
                    match = &ipts2[j];
                    foundJ = j;
                } else if (dist < d2) {
                    d2 = dist;
                }
            }

            if (d1 / d2 < ratio) {
                ipts1[i].dx = match->x - ipts1[i].x;
                ipts1[i].dy = match->y - ipts1[i].y;

                matches.push_back(std::make_pair(ipts1[i], *match));
                matched.push_back(foundJ);
            }
        }
    } catch(std::exception ex) {
        wLog(f("Exception in getUniqueMatches: ", ex.what()));
        return;
    }
}

只有几次在这里休息。我不知道发生了什么,也许有问题?执行此函数时,应用程序仅使用 1 个线程。提取点时使用 10 个线程。

在 Centos5 (VPS) 上使用它。2 Gb RAM 使用 20% hd 使用 g++(性能模式)编译,IDE 使用 Netbeans。OpenCV,库库尔。

4

2 回答 2

1

我怀疑两件事:

  • g++ 编译器优化。使用-O2或更少;-O3有时会产生奇怪的行为,尤其是在浮点运算方面。

  • 如果减少优化不能解决问题,我建议更改

    if (d1 / d2 < ratio)

    if (d1 < double(ratio)*d2)

    为了避免被零除(我认为不太可能发生)并获得更好的精度结果。

于 2011-05-28T12:32:32.100 回答
0

改变

if (d1 / d2 < ratio) {

为了

if(d2 > 0 && ...) {

我认为问题是除以零,但没有抛出异常。:(

于 2011-05-14T15:45:00.017 回答