C++比较冲浪描述符算法匹配有时会中断
我编写了一个 C++ 应用程序,用于比较图像中的点匹配(OpenSurf C++),但有时,从数千个“getUniqueMatches”中,应用程序会在“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 内存 已使用 20% 高清 使用g++(性能模式)编译,IDE使用Netbeans。 OpenCV、libcurl。
I wrote a c++ application that compares points matches from images (OpenSurf C++) but some times, 1 from some thousands of "getUniqueMatches", the application breaks in some point inside "getUniqueMatches". I has this log:
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 --
And this is the code:
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;
}
}
Only some times breaks here. I don't know what is happening, maybe something wrong? The application uses only 1 thread when executing this function. When extracting points uses 10 threads.
Using it on Centos5 (VPS).
2 Gb RAm
20% hd used
Compiled using g++ (Performance mode), IDE used Netbeans.
OpenCV, libcurl.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑两件事:
g++ 编译器优化。使用
-O2
或更少;-O3
及其他有时会产生奇怪的行为,特别是在浮点运算方面。如果减少优化不能解决问题,我建议更改
if (d1 / d2 < 比率)
至
if (d1 < double(ratio)*d2)
为了避免被零除(我认为不太可能发生)并获得更好的精度结果。
I suspect 2 things:
g++ compiler optimization. Use
-O2
or less;-O3
and beyond sometimes generate weird behavior especially concerning floating point operation.If reducing optimization does not fix the problem, I suggest changing
if (d1 / d2 < ratio)
to
if (d1 < double(ratio)*d2)
for avoiding division by zero (unlikely happen I think) and for better precision result.
更改
为
我认为问题是除以零,但没有引发异常。 :(
Change
for
I think that the problem was a division by zero, but was not throwing exceptions. :(