我正在写一个程序,将图像与SURF合并,但遇到了一个未解决的错误,关于矢量超出范围。(我不能说英语很好,我很抱歉);
我的程序
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
发布于 2020-08-24 13:21:46
问题是,您正在尝试到达keypoint向量的一个元素,该元素重载了大小。为了安全起见,您可以尝试这样做:
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);
}
}https://stackoverflow.com/questions/63554556
复制相似问题