c++和opencv中向量下标超出范围错误
Vector subscript out of range error in c++ and opencv
我正在尝试编写一个程序,该程序使用 ORB 算法来检测和计算图像的关键点,并使用 FLANN 匹配器匹配描述符向量。
我面临的问题是,每次我 运行 Visual C++ 上的程序时,我都会收到一个错误,告诉 "vector subscript out of range"(我还附上了错误的图片)。
问题似乎出在 for 的某个地方,因为当我启动调试器时它停在那里,我得到了错误。当我评论第一个 for 看看其余是否正常时,我在第二个 for 上遇到了同样的错误。
请帮我找出问题。
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2\core\types.hpp>
#include <opencv2\highgui.hpp>
#include <opencv2\core.hpp>
#include <opencv2\opencv_modules.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat img1 = imread("C:\Users\patri\Desktop\test.bmp");
Mat img2 = imread("C:\Users\patri\Desktop\test3.bmp");
/*
if (!img1.data || !img2.data)
{
printf(" --(!) Error reading images \n"); return -1;
}
*/
std::vector<KeyPoint> keypoints_1, keypoints_2;
Mat descriptors_1, descriptors_2;
Ptr<ORB> orb = ORB::create(100, 2, 8, 31, 0, 2, ORB::HARRIS_SCORE, 31, 20);
orb->detectAndCompute(img1, Mat(), keypoints_1, descriptors_1);
orb->detectAndCompute(img2, Mat(), keypoints_2, descriptors_2);
std::cout << "Found " << keypoints_1.size() << " Keypoints " << std::endl;
std::cout << "Found " << keypoints_2.size() << " Keypoints " << std::endl;
Mat out1, out2;
drawKeypoints(img1, keypoints_1, out1, Scalar::all(255));
drawKeypoints(img2, keypoints_2, out2, Scalar::all(255));
imshow("Kpts1", out1);
imshow("Kpts2", out2);
//////////////////////////////////////////////////////////////////////
// Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
//matcher.match(descriptors_1, descriptors_2, matches);
double max_dist = 0; double min_dist = 100;
//calculation of max and min distances between keypoints
for (int i = 0; i < descriptors_1.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);
std::vector< DMatch > good_matches;
for (int i = 0; i < descriptors_1.rows; i++)
{
if (matches[i].distance <= max(2 * min_dist, 0.02))
{
good_matches.push_back(matches[i]);
}
}
//-- Draw only "good" matches
Mat img_matches;
drawMatches(img1, keypoints_1, img2, keypoints_2,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
//-- Show detected matches
imshow("Good Matches", img_matches);
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);
}
waitKey(0);
return 0;
}
我遇到的错误
std::vector< DMatch > matches;
为空,但您正试图在 for 循环中访问它的元素。
std::vector< DMatch > matches;//this creates an empty vector
//you need to push_back some elements in matches before trying to access it in your loops
......
//calculation of max and min distances between keypoints
for (int i = 0; i < descriptors_1.rows; i++)
{
double dist = matches[i].distance;//this is trying to access the empty vector
......
}
我认为向量变量 good_matches
可能有 0 个元素,问题代码可能隐藏:
for (int i = 0; i < descriptors_1.rows; i++)
{
double dist = matches[i].distance;
if (dist < min_dist) min_dist = dist;
if (dist > max_dist) max_dist = dist;
}
我正在尝试编写一个程序,该程序使用 ORB 算法来检测和计算图像的关键点,并使用 FLANN 匹配器匹配描述符向量。 我面临的问题是,每次我 运行 Visual C++ 上的程序时,我都会收到一个错误,告诉 "vector subscript out of range"(我还附上了错误的图片)。
问题似乎出在 for 的某个地方,因为当我启动调试器时它停在那里,我得到了错误。当我评论第一个 for 看看其余是否正常时,我在第二个 for 上遇到了同样的错误。
请帮我找出问题。
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2\core\types.hpp>
#include <opencv2\highgui.hpp>
#include <opencv2\core.hpp>
#include <opencv2\opencv_modules.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat img1 = imread("C:\Users\patri\Desktop\test.bmp");
Mat img2 = imread("C:\Users\patri\Desktop\test3.bmp");
/*
if (!img1.data || !img2.data)
{
printf(" --(!) Error reading images \n"); return -1;
}
*/
std::vector<KeyPoint> keypoints_1, keypoints_2;
Mat descriptors_1, descriptors_2;
Ptr<ORB> orb = ORB::create(100, 2, 8, 31, 0, 2, ORB::HARRIS_SCORE, 31, 20);
orb->detectAndCompute(img1, Mat(), keypoints_1, descriptors_1);
orb->detectAndCompute(img2, Mat(), keypoints_2, descriptors_2);
std::cout << "Found " << keypoints_1.size() << " Keypoints " << std::endl;
std::cout << "Found " << keypoints_2.size() << " Keypoints " << std::endl;
Mat out1, out2;
drawKeypoints(img1, keypoints_1, out1, Scalar::all(255));
drawKeypoints(img2, keypoints_2, out2, Scalar::all(255));
imshow("Kpts1", out1);
imshow("Kpts2", out2);
//////////////////////////////////////////////////////////////////////
// Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
//matcher.match(descriptors_1, descriptors_2, matches);
double max_dist = 0; double min_dist = 100;
//calculation of max and min distances between keypoints
for (int i = 0; i < descriptors_1.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);
std::vector< DMatch > good_matches;
for (int i = 0; i < descriptors_1.rows; i++)
{
if (matches[i].distance <= max(2 * min_dist, 0.02))
{
good_matches.push_back(matches[i]);
}
}
//-- Draw only "good" matches
Mat img_matches;
drawMatches(img1, keypoints_1, img2, keypoints_2,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
//-- Show detected matches
imshow("Good Matches", img_matches);
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);
}
waitKey(0);
return 0;
}
我遇到的错误
std::vector< DMatch > matches;
为空,但您正试图在 for 循环中访问它的元素。
std::vector< DMatch > matches;//this creates an empty vector
//you need to push_back some elements in matches before trying to access it in your loops
......
//calculation of max and min distances between keypoints
for (int i = 0; i < descriptors_1.rows; i++)
{
double dist = matches[i].distance;//this is trying to access the empty vector
......
}
我认为向量变量 good_matches
可能有 0 个元素,问题代码可能隐藏:
for (int i = 0; i < descriptors_1.rows; i++)
{
double dist = matches[i].distance;
if (dist < min_dist) min_dist = dist;
if (dist > max_dist) max_dist = dist;
}