OpenCV drawMatches error
OpenCV drawMatches error
我的代码由一个部分组成,我在其中对一组匹配进行排序,并根据距离定义好的匹配。当我尝试提取Matches时,我收到一个错误:
OpenCV Error: Assertion failed (i1 >= 0 && i1 < static_cast<int>(keypoints1.size())) in drawMatches, file /home/user/OpenCV/opencv-2.4.10/modules/features2d/src/draw.cpp, line 207
terminate called after throwing an instance of 'cv::Exception'
what(): /home/user/OpenCV/opencv-2.4.10/modules/features2d/src/draw.cpp:207: error: (-215) i1 >= 0 && i1 < static_cast<int>(keypoints1.size()) in function drawMatches
draw.cpp文件显示:
// draw matches
for( size_t m = 0; m < matches1to2.size(); m++ )
{
if( matchesMask.empty() || matchesMask[m] )
{
int i1 = matches1to2[m].queryIdx;
int i2 = matches1to2[m].trainIdx;
CV_Assert(i1 >= 0 && i1 < static_cast<int>(keypoints1.size()));
CV_Assert(i2 >= 0 && i2 < static_cast<int>(keypoints2.size()));
const KeyPoint &kp1 = keypoints1[i1], &kp2 = keypoints2[i2];
_drawMatch( outImg, outImg1, outImg2, kp1, kp2, matchColor, flags );
}
}
我的drawMatches呼叫如下:
Mat matchesImage;
drawMatches( im1, keypoints1, im2, keypoints2,
good_matches, matchesImage, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
有人能帮我解释一下这个错误吗?
更新:
这是我的good_matches计算代码
double min_dist = 10000;
double max_dist = 0;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors1.rows; i++ ) {
double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
printf("-- Max dist : %f n", max_dist );
printf("-- Min dist : %f n", min_dist );
//-- Draw only "good" matches
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors1.rows; i++ ) {
if( matches[i].distance <= max(2*min_dist, 0.02) ) {
good_matches.push_back( matches[i]);
}
}
for( int i = 0; i < (int)good_matches.size(); i++ ) {
printf( "-- Good Match [%d] Keypoint 1: %d -- Keypoint 2: %d n",
i, good_matches[i].queryIdx, good_matches[i].trainIdx );
}
cout << "number of good matches: " << (int)good_matches.size() << endl;;
//Draw matches and save file
Mat matchesImage;
drawMatches( im1, keypoints1, im2, keypoints2,
good_matches, matchesImage, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
更新2:
BFMatcher matcher(NORM_L2, true);
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);
问题与匹配点的顺序有关。如果你这样做了,例如:
match(right_desc, left_desc)
函数CCD_ 1将必须遵循相同的顺序。这将起作用(考虑到我的匹配示例):
drawMatches(right_rgb, right_pts, left_rgb, left_pts, matches)
这将产生错误:
drawMatches(left_rgb, left_pts, right_rgb, right_pts, matches)
当您访问匹配的坐标时,顺序还将影响什么是查询和什么是训练(即queryIdx
和trainIdx
)。注意,right_pts
和left_pts
分别是由right_desc
和left_desc
描述的关键点。
希望它能帮助到别人。
我认为每次迭代都应该使用good_matches.clear()
命令清除good_matches
矢量的内容(如果使用drawMatches
0循环从相机获取帧,则会在while(1)
循环之后写入good_matches.clear()
命令:
while(1) {
good_matches.clear();
// other code ...
}
通常,good_matches是一个数组,它绑定来自keypoints1和keypoints2数组的点。因此,点关键点1[good_matches[m].queryIdx]对应于点keypoints2[good_maches[m].trainIdx]。正如您所看到的,opencv代码中的断言是有意义的。
问题似乎出在匹配数组中。
我不同意上面的答案。
传递到matcher.match(dscp1, dscp2, matches)
和drawMatch(img1, kp1, img2, kp2, good_matches)
中的参数是对应的(它们都与keypoints2
中keypoints1
中的每个描述符相匹配,keypoints1
称为查询集,而keypoints2
称为训练集。)
该错误可能是由传递到drawMatch()
的错误keypoints1
和keypoints2
引起的。检查good_matches
中的索引是否与keypoints1
中的kp对应。(如果在挑选好的匹配项时减少keypoints
的数量,这种情况可能会发生。)
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 从不同目录执行时"symbol lookup error:",否则从构建目录执行时按预期工作
- 对象声明中的模板"error: expected expression"
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- OpenCV 3.4.1 error readNetFromTensorflow 无法在 cv::d nn::ReadProtoFromBinaryFile 中打开 .pb
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- 为什么我会"error: expected expression"?
- CMake Error at modules/videoio/cmake/detect_ffmpeg.cmake:16
- 如果两个静态库使用相同的另一个静态库,如何避免"LNK2005 Already Defined error"?
- OpenCV drawMatches error