0

Iam getting access violation error while I am performing debugging in stereo calibration

code is as follows

  for(int j=0;j<n;j++){    
                       cornersR[j]=cvPoint2D32f(cornersRp[j].x,cornersRp[j].y);
                       cornersL[j]=cvPoint2D32f(cornersLp[j].x,cornersLp[j].y);
                       }

these are initialized as follows,n is a fixed integer

           CvPoint2D32f* cornersRp = new CvPoint2D32f[ n ];
           CvPoint2D32f* cornersLp = new CvPoint2D32f[ n ];
           vector<CvPoint2D32f> cornersR;
           vector<CvPoint2D32f> cornersL;

please help me out.....

hmjd
  • 120,187
  • 20
  • 207
  • 252
nbsrujan
  • 1,179
  • 1
  • 12
  • 26

1 Answers1

6

In order to use vector[] you have to ensure that the vector has an element at that index. In this case, both the vectors are empty resulting in the access violation.

Change the declarations to:

vector<CvPoint2D32f> cornersR(n);
vector<CvPoint2D32f> cornersL(n);

which will populate the vectors with n default constructed instances of CvPoint2D32f.

If there is no default constructor for CvPoint2D32f you could either:

vector<CvPoint2D32f> cornersR(n, CvPoint2D32f(1,1));
vector<CvPoint2D32f> cornersL(n, CvPoint2D32f(1,1));

which would populate the vectors with copies of the second argument, or use vector::push_back() instead of vector::operator[] and without specifying an initial size for the vector:

vector<CvPoint2D32f> cornersR;
vector<CvPoint2D32f> cornersL;

cornersR.push_back(cvPoint2D32f(cornersRp[j].x,cornersRp[j].y));
hmjd
  • 120,187
  • 20
  • 207
  • 252