比较 descriptor.type 和 descriptor.cols 后的 OpenCV 分段错误
OpenCV segmentation fault after comparing descriptor.type and descriptor.cols
在我的程序中,每当我用导致无法检测到 ORB 功能的东西覆盖相机时,程序就会崩溃并出现错误:
OpenCV Error: Assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)) in batchDistance, file /bin/opencv-2.4.7/modules/core/src/stat.cpp, line 2473
terminate called after throwing an instance of 'cv::Exception'
what(): /bin/opencv-2.4.7/modules/core/src/stat.cpp:2473: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) in function batchDistance
Aborted
为了尝试修复此错误,我遵循了此 post here 中的建议,其中涉及在行 matcher->match(descriptorsObject, descriptors, matches);
[=23] 周围添加以下 if
=]
if(descriptorsObject.type() == descriptors.type() && descriptorsObject.cols == descriptors.cols) {
matcher->match(descriptorsObject, descriptors, matches);
}
然而,当相机无法检测到任何特征点时,程序仍然崩溃,但我只是得到错误:
Segmentation fault
我不知道为什么现在会这样,如果 if
的条件不满足,我是否需要有 else
的条件才能获得下一帧?
编辑:
当代码在调试模式下为 运行 时,出现以下错误:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000402d9e in main (argc=2, argv=0x7fffffffe428) at ORB.cpp:57
57 double dist = matches[i].distance;
在编译时启用所有警告时,我得到以下信息:
ORB.cpp:87:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
附加代码:
if(descriptorsObject.type() == descriptors.type() && descriptorsObject.cols == descriptors.cols) {
matcher->match(descriptorsObject, descriptors, matches);
}
double max_dist = 0;
double min_dist = 100;
for( int i = 0; i < descriptorsObject.rows; i++ ) {
double dist = matches[i].distance; //LINE 57
if( dist < min_dist )
min_dist = dist;
if( dist > max_dist )
max_dist = dist;
}
std::vector<DMatch> goodMatches;
for( int i = 0; i < descriptorsObject.rows; i++ ) {
if( matches[i].distance < 3*min_dist ) {
goodMatches.push_back( matches[i]);
}
}
if(goodMatches.size() >= 4) {
Mat outputImage;
Scalar keypointColor = Scalar(255, 0, 0);
drawMatches( img_object, keypointsObject, gray, keypoints, goodMatches, imgMatches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
std::vector<Point2f> obj;
std::vector<Point2f> scene;
for( int i = 0; i < goodMatches.size(); i++ ) { //LINE 87
//-- Get the keypoints from the good matches
obj.push_back( keypointsObject[ goodMatches[i].queryIdx ].pt );
scene.push_back( keypoints[ goodMatches[i].trainIdx ].pt );
}
std::vector<uchar> mask;
Mat H = findHomography( obj, scene, CV_RANSAC, 3, mask );
std::vector<Point2f> obj_corners(4);
obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_object.cols, 0 );
obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.rows );
std::vector<Point2f> scene_corners(4);
perspectiveTransform( obj_corners, scene_corners, H);
line( imgMatches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
line( imgMatches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
line( imgMatches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
line( imgMatches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
if (mask.size() < 100) {
std::cout << fileNamePostCut << std::endl;
}
}
namedWindow("Output");
imshow("Output", imgMatches);
你试过调试吗?
由于使用 if
语句修改代码导致了不同的错误,最可能的原因可能是您可能试图从 matches
或 descriptors
中选择一些值在 if 条件之后。由于图像中没有匹配项或特征,这可能会导致分割错误。
尝试调试代码并向我们提供更多详细信息,包括代码片段,以便进一步缩小可能性范围。
我扩展了 if
语句的范围,因此它不再在 matcher->match(descriptorsObject, descriptors, matches);
行之后关闭,而是在 namedWindow("Output");
行上方的末尾关闭
在我的程序中,每当我用导致无法检测到 ORB 功能的东西覆盖相机时,程序就会崩溃并出现错误:
OpenCV Error: Assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)) in batchDistance, file /bin/opencv-2.4.7/modules/core/src/stat.cpp, line 2473
terminate called after throwing an instance of 'cv::Exception'
what(): /bin/opencv-2.4.7/modules/core/src/stat.cpp:2473: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) in function batchDistance
Aborted
为了尝试修复此错误,我遵循了此 post here 中的建议,其中涉及在行 matcher->match(descriptorsObject, descriptors, matches);
[=23] 周围添加以下 if
=]
if(descriptorsObject.type() == descriptors.type() && descriptorsObject.cols == descriptors.cols) {
matcher->match(descriptorsObject, descriptors, matches);
}
然而,当相机无法检测到任何特征点时,程序仍然崩溃,但我只是得到错误:
Segmentation fault
我不知道为什么现在会这样,如果 if
的条件不满足,我是否需要有 else
的条件才能获得下一帧?
编辑: 当代码在调试模式下为 运行 时,出现以下错误:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000402d9e in main (argc=2, argv=0x7fffffffe428) at ORB.cpp:57
57 double dist = matches[i].distance;
在编译时启用所有警告时,我得到以下信息:
ORB.cpp:87:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
附加代码:
if(descriptorsObject.type() == descriptors.type() && descriptorsObject.cols == descriptors.cols) {
matcher->match(descriptorsObject, descriptors, matches);
}
double max_dist = 0;
double min_dist = 100;
for( int i = 0; i < descriptorsObject.rows; i++ ) {
double dist = matches[i].distance; //LINE 57
if( dist < min_dist )
min_dist = dist;
if( dist > max_dist )
max_dist = dist;
}
std::vector<DMatch> goodMatches;
for( int i = 0; i < descriptorsObject.rows; i++ ) {
if( matches[i].distance < 3*min_dist ) {
goodMatches.push_back( matches[i]);
}
}
if(goodMatches.size() >= 4) {
Mat outputImage;
Scalar keypointColor = Scalar(255, 0, 0);
drawMatches( img_object, keypointsObject, gray, keypoints, goodMatches, imgMatches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
std::vector<Point2f> obj;
std::vector<Point2f> scene;
for( int i = 0; i < goodMatches.size(); i++ ) { //LINE 87
//-- Get the keypoints from the good matches
obj.push_back( keypointsObject[ goodMatches[i].queryIdx ].pt );
scene.push_back( keypoints[ goodMatches[i].trainIdx ].pt );
}
std::vector<uchar> mask;
Mat H = findHomography( obj, scene, CV_RANSAC, 3, mask );
std::vector<Point2f> obj_corners(4);
obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_object.cols, 0 );
obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.rows );
std::vector<Point2f> scene_corners(4);
perspectiveTransform( obj_corners, scene_corners, H);
line( imgMatches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
line( imgMatches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
line( imgMatches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
line( imgMatches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
if (mask.size() < 100) {
std::cout << fileNamePostCut << std::endl;
}
}
namedWindow("Output");
imshow("Output", imgMatches);
你试过调试吗?
由于使用 if
语句修改代码导致了不同的错误,最可能的原因可能是您可能试图从 matches
或 descriptors
中选择一些值在 if 条件之后。由于图像中没有匹配项或特征,这可能会导致分割错误。
尝试调试代码并向我们提供更多详细信息,包括代码片段,以便进一步缩小可能性范围。
我扩展了 if
语句的范围,因此它不再在 matcher->match(descriptorsObject, descriptors, matches);
行之后关闭,而是在 namedWindow("Output");