gpt4 book ai didi

c++ - OpenCV 筛选/冲浪/Orb : drawMatch function is not working well

转载 作者:太空狗 更新时间:2023-10-29 21:20:25 27 4
gpt4 key购买 nike

我使用 Sift/Surf 和 ORB,但有时我会遇到 drawMatch 函数的问题。

错误在这里:

OpenCV Error: Assertion failed (i2 >= 0 && i2 < static_cast(keypoints2.size())) in drawMatches, file /home/opencv-2.4.6.1/modules/features2d/src/draw.cpp, line 208 terminate called after throwing an instance of 'cv::Exception' what(): /home/opencv-2.4.6.1/modules/features2d/src/draw.cpp:208: error: (-215) i2 >= 0 && i2 < static_cast(keypoints2.size()) in function drawMatche

代码:

drawMatchPoints(img1,keypoints_img1,img2,keypoints_img2,matches);

我试着像这样用 img2 和 keypoints_img2 反转 img 1,keypoints_img1 :

drawMatchPoints(img2,keypoints_img2,img1,keypoints_img1,matches);

对应我做单应性的函数:

void drawMatchPoints(cv::Mat image1,std::vector<KeyPoint> keypoints_img1,
cv::Mat image2,std::vector<KeyPoint> keypoints_img2,std::vector<cv::DMatch> matches){

cv::Mat img_matches;
drawMatches( image1, keypoints_img1, image2, keypoints_img2,
matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
std::cout << "Number of good matching " << (int)matches.size() << "\n" << endl;



//-- Localize the object
std::vector<Point2f> obj;
std::vector<Point2f> scene;

for( int i = 0; i < matches.size(); i++ )
{
//-- Get the keypoints from the good matches
obj.push_back( keypoints_img1[ matches[i].queryIdx ].pt );
scene.push_back( keypoints_img2[matches[i].trainIdx ].pt );
}

Mat H = findHomography( obj, scene, CV_RANSAC );
std::cout << "Size of homography " << *H.size << std::endl ;

//-- Get the corners from the image_1 ( the object to be "detected" )
std::vector<Point2f> obj_corners(4);
obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( image1.cols, 0 );
obj_corners[2] = cvPoint( image1.cols, image1.rows ); obj_corners[3] = cvPoint( 0, image1.rows );
std::vector<Point2f> scene_corners(4);


perspectiveTransform( obj_corners, scene_corners, H);


//-- Draw lines between the corners (the mapped object in the scene - image_2 )
line( img_matches, scene_corners[0] + Point2f( image1.cols, 0), scene_corners[1] + Point2f( image1.cols, 0), Scalar(0, 255, 0), 4 );
line( img_matches, scene_corners[1] + Point2f( image1.cols, 0), scene_corners[2] + Point2f( image1.cols, 0), Scalar( 0, 255, 0), 4 );
line( img_matches, scene_corners[2] + Point2f( image1.cols, 0), scene_corners[3] + Point2f( image1.cols, 0), Scalar( 0, 255, 0), 4 );
line( img_matches, scene_corners[3] + Point2f( image1.cols, 0), scene_corners[0] + Point2f( image1.cols, 0), Scalar( 0, 255, 0), 4 );

//-- Show detected matches
cv::imshow( "Good Matches & Object detection", img_matches );
cv::waitKey(5000);

但我仍然有错误!

我注意到当我的 keypoints_img1 的大小小于我的 keypoints_img2 的大小时发生错误:

Size keyPoint1 : 244 - Size keyPoint2 : 400

因此,如果我颠倒我的两张图片的加载,那是可行的,但如果我的第一张图片比我的第二张图片有更多的关键点,我现在不能提前......

为了创建功能,我的代码(最重要的一步):

init_Sift(400,5,0.04,25,1.6);
void init_Sift(int nf,int nOctaveL,double contrastThresh, double edgeThresh,double sigma){
this->nfeatureSift=nf;
this->nOctaveLayerSift=nOctaveL;
this->contrastThresholdSift=contrastThresh;
this->edgeThresholdSift=edgeThresh;
this->sigmaSift=sigma;}



cv::FeatureDetector* detector=new SiftFeatureDetector(nfeatureSift,nOctaveLayerSift,contrastThresholdSift,edgeThresholdSift,sigmaSift);
cv::DescriptorExtractor* extractor=new SiftDescriptorExtractor

extractor->compute( image, keypoints, descriptors );

匹配部分:

    std::cout << "Type of matcher : " << type_of_matcher << std::endl;
if (type_of_matcher=="FLANN" || type_of_matcher=="BF"){
std::vector<KeyPoint> keypoints_img1 = keyfeatures.compute_Keypoints(img1);
std::vector<KeyPoint> keypoints_img2 = keyfeatures.compute_Keypoints(img2);

cv::Mat descriptor_img1 = keyfeatures.compute_Descriptors(img1);
cv::Mat descriptor_img2 = keyfeatures.compute_Descriptors(img2);

std::cout << "Size keyPoint1 " << keypoints_img1.size() << "\n" << std::endl;
std::cout << "Size keyPoint2 " << keypoints_img2.size() << "\n" << std::endl;

//Flann with sift or surf
if (type_of_matcher=="FLANN"){
Debug::info("USING Matcher FLANN");
fLmatcher.match(descriptor_img1,descriptor_img2,matches);

double max_dist = 0; double min_dist = 100;

//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptor_img1.rows; i++ ){
double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}

std::vector< DMatch > good_matches;

for( int i = 0; i < descriptor_img1.rows; i++ )
{ if( matches[i].distance <= max(2*min_dist, 0.02) )
{ good_matches.push_back( matches[i]); }
}

std::cout << "Size of good match : " << (int)good_matches.size() << std::endl;
//-- Draw only "good" matches
if (!good_matches.empty()){
drawMatchPoints(img1,keypoints_img1,img2,keypoints_img2,good_matches);

}
else {
Debug::error("Flann Matcher : Pas de match");
cv::Mat img_matches;
drawMatches( img1, keypoints_img1, img2, keypoints_img2,
matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
cv::imshow( "No match", img_matches );
cv::waitKey(5000);
}

}
//BruteForce with sift or surf
else if (type_of_matcher=="BF"){
Debug::info("USING Matcher Brute Force");

bFmatcher.match(descriptor_img1,descriptor_img2,matches);
if (!matches.empty()){
std::nth_element(matches.begin(),//Initial position
matches.begin()+24, //Position of the sorted element
matches.end());//End position
matches.erase(matches.begin()+25,matches.end());

drawMatchPoints(img1,keypoints_img1,img2,keypoints_img2,matches);
//drawMatchPoints(img2,keypoints_img2,img1,keypoints_img1,matches);
}
else {
Debug::error("Brute Force matcher : Pas de match");
cv::Mat img_matches;
drawMatches( img1, keypoints_img1, img2, keypoints_img2,
matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
cv::imshow( "No match", img_matches );
cv::waitKey(5000);

}

}

您有什么建议或建议吗?

编辑: 我解决了我的问题。我有一个 C++ 问题,因为我有两个类。一个关于匹配,另一个关于寻找关键特征。我已经在我的 .h std::vector 上写过,对于描述符也是如此。

class keyFeatures{

public:
...
std::vector<keyPoint> keypoints;
...

我删除了这个属性并做了一个接受参数 std::vector keypoints 的函数

cv::Mat descriptor_img1 = keyfeatures.compute_Descriptors(img1,keypoints_img1);

代替

cv::Mat descriptor_img1 = keyfeatures.compute_Descriptors(img1);

我觉得我做匹配的时候有冲突...但我不知道为什么我不应该把它写在我的 .h 上并在我的函数上做一个本地参数。

谢谢!

最佳答案

对于像我这样搜索过但找不到解决方案的人。

Assertion failed (i2 >= 0 && i2 < static_cast(keypoints2.size()))

这意味着断言由于 i2 小于 0 或 i2 小于 keypoints2 大小而失败。但什么是 i2?

来自 rbaleksandar 在评论中提供的链接

int i2 = matches1to2[m].trainIdx;

这里的 trainIdx 是 keypoints2 中的一个索引。检查 i2 < static_cast(keypoints2.size()) 确保索引小于 keypoints2.size()。

对我来说,这是因为我在调用 drawMatches 之前丢弃了一些关键点,但在计算描述符之后,即调用了 DescriptorExtractor#compute。这意味着当我更改那些关键点时,drawMatches 通过描述符引用旧的关键点。最终结果是一些关键点的 idx 很大,但关键点的大小很小,因此出现错误。

关于c++ - OpenCV 筛选/冲浪/Orb : drawMatch function is not working well,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24552590/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com