C++比较冲浪描述符算法匹配中断几次



我写了一个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 千兆以太网使用 20% 高清使用 g++(性能模式)编译,IDE 使用 Netbeans。OpenCV, libcurl.

我怀疑有两件事:

  • G++ 编译器优化。使用-O2或更少; -O3及以后有时会产生奇怪的行为,尤其是关于浮点运算的行为。

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

    if (d1 / d2 < ratio)

    if (d1 < double(ratio)*d2)

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

更改

if (d1 / d2 < ratio) {

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

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

最新更新