algorithm - Vector subscript out of range error in c++ and opencv -


i'm trying write program uses orb algorithm detect , compute keypoints of image , matches descriptor vectors using flann matcher. issue facing is, every time run program on visual c++, getting error tells "vector subscript out of range"(i've attached image of error).

the problem seems somewhere in because when start debugger stops there , error. when commented first see if rest ok, i've got same error on second for.

please me find problem.

#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 , min distances between keypoints (int = 0; < 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;  (int = 0; < descriptors_1.rows; i++) {     if (matches[i].distance <= max(2 * min_dist, 0.02))     {         good_matches.push_back(matches[i]);     } }  //-- draw "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);  (int = 0; < (int)good_matches.size(); i++) {     printf("-- match [%d] keypoint 1: %d  -- keypoint 2: %d  \n", i, good_matches[i].queryidx, good_matches[i].trainidx); }  waitkey(0);  return 0; }    

the error i'm getting

std::vector< dmatch > matches; empty trying access elements in loop.

std::vector< dmatch > matches;//this creates empty vector //you need push_back elements in matches before trying access in loops ......  //calculation of max , min distances between keypoints (int = 0; < descriptors_1.rows; i++) {     double dist = matches[i].distance;//this trying access empty vector     ...... } 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -