首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向量下标超出范围错误- C++向量和OpenCV4

向量下标超出范围错误- C++向量和OpenCV4
EN

Stack Overflow用户
提问于 2020-08-24 12:00:06
回答 1查看 25关注 0票数 0

我正在写一个程序,将图像与SURF合并,但遇到了一个未解决的错误,关于矢量超出范围。(我不能说英语很好,我很抱歉);

我的程序

代码语言:javascript
复制
int main(int argc, char **argv) {
Mat image01, image02;
image01 = imread("D:/work/Temp/拼接A1.jpg");
image02 = imread("D:/work/Temp/拼接A2.jpg");

Ptr<xfeatures2d::SURF> surf = xfeatures2d::SURF::create(1000);
vector<KeyPoint> keypoint01, keypoint02;
Mat dsp01, dsp02;
surf->detect(image01, keypoint01);
surf->detect(image02, keypoint02);
surf->compute(image01, keypoint01, dsp01);
surf->compute(image02, keypoint02, dsp02);


vector<DMatch> dmatch;
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
matcher -> match(dsp01, dsp02, dmatch);

double maxDist = 0;
for (int i = 0; i < dsp01.rows; i++) {
    double dist = dmatch[i].distance;
    if (dist > maxDist)
        maxDist = dist;
}

vector<DMatch> good_matches;

for (int k = 0; k < dsp01.rows; k++)
{
    if (dmatch[k].distance < 0.4*maxDist) {
        good_matches.push_back(dmatch[k]);
    }
}


Mat imageout;
drawMatches(image01, keypoint01, image02, keypoint02, good_matches, imageout);
imshow("11", imageout);

vector<Point2f> imagePoint2;
vector<Point2f> imagePoint1;
imagePoint1.resize(good_matches.size());
imagePoint2.resize(good_matches.size());
for (size_t i = 0; i < good_matches.size(); i++) {
    if (good_matches.empty())
    {
        cout << "no good matches" << endl;
        break;
    }
        imagePoint2.push_back(keypoint02[good_matches[i].queryIdx].pt);
        imagePoint1.push_back(keypoint01[good_matches[i].trainIdx].pt);
}

和problem enter image description here

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-24 13:21:46

问题是,您正在尝试到达keypoint向量的一个元素,该元素重载了大小。为了安全起见,您可以尝试这样做:

代码语言:javascript
复制
for (size_t i = 0; i < good_matches.size(); i++) {
    if (good_matches.empty())
    {
        cout << "no good matches" << endl;
        break;
    }
        if((int)good_matches[i].queryIdx<keypoint02.size() && (int)good_matches[i].trainIdx<keypoint01.size)
        {
           imagePoint2.push_back(keypoint02[good_matches[i].queryIdx].pt);
           imagePoint1.push_back(keypoint01[good_matches[i].trainIdx].pt);
        }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63554556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档