Home > Software engineering >  Error: the Debug an assertion failed! Expression: vector subscript out of range
Error: the Debug an assertion failed! Expression: vector subscript out of range

Time:10-01

The source code is as follows:
 # include 
# include
# include
# include
# include
using namespace cv;
using namespace std;
Const unsigned char FORE_GROUD=255;
Int thresh=7;
Mat SRC, DST, dst1;
//ofstream fout.

Int main ()
{
String outfilename (" test. TXT ");
Ofstream fout (outfilename c_str ());
NamedWindow (" result ", 1);
Mat SRC=https://bbs.csdn.net/topics/imread (" 1. JPG ");
Mat DST, src1;
CvtColor (SRC, src1, COLOR_BGR2GRAY);
Point root_points [1] [8].
Root_points [0] [0]=Point (0, 0);
Root_points [0] [1]=Point (SRC. The cols, 0);
Root_points [0] [2]=Point (SRC) cols, 100);
Root_points [0] [3]=Point (140100);
Root_points [0] [4]=Point (0205);
Root_points [0] [5]=Point (SRC) cols, 205);
Root_points [0] [6]=Point (SRC) cols, SRC, rows);
Root_points [0] [7]=Point (0, SRC. Rows);
Const Point * PPT [1]={root_points [0]}.
Int the NPT []={8};
Polylines (src1, PPT, NPT, 1, 1, Scalar (0), 1,8,0);
FillPoly (src1, PPT, NPT, 1, Scalar (255));
MedianBlur (src1, src1, 3);
EqualizeHist (src1, src1);
Threshold (src1, DST, double (thresh), double (FORE_GROUD), THRESH_BINARY);
Vector Contours.
VectorVector Contours_poly (contours. The size ());
Vector BoundRect (contours. The size ());
FindContours (DST, contours, hierarchy, CV_RETR_TREE CV_CHAIN_APPROX_SIMPLE);//for each contour of each pixel
for(int i=0; I & lt; Contours. The size (); I++)
{
ApproxPolyDP (Mat (contours [I]), contours_poly [I], 3, true);
BoundRect [I]=boundingRect (Mat (contours_poly [I]));
//the Rect the Rect=boundingRect (Mat (contours_poly [index]));
DrawContours (SRC, contours_poly, index, Scalar (255255255), 1, 8, hierarchy, 0, Point ());
A rectangle (SRC, boundRect [I]. Tl (), boundRect [I]. Br (), Scalar (255255255), 2);
}


//fout. Close ();
Imshow (" result ", DST);
Imshow (" show ", SRC);
waitKey(0);
Return (0);
}

Breakpoint moves approxPolyDP (Mat (contours [I]), contours_poly [I], 3, true); An error occurred while
Ask the great spirit show

CodePudding user response:

The subscript overflow?
The Debug under open the call stack and see the function call stack, find the error function, the first

CodePudding user response:

Collapsed in the pop-up dialog box, press the corresponding button to enter debugging press Alt + 7 key to view the Call Stack, namely "the Call Stack" from the inside to the following out of from the inner to outer function Call history, double-click a row to the cursor to the Call of the source code or assembly instruction, don't understand when double click on the next line, until we can read ,

CodePudding user response:

When you visit the vector, the subscript have cross-border operation, pay attention to your little target range
  • Related