Ask Your Question

Revision history [back]

OpenCV Error: Image step is wrong (The matrix is not continuous, thus its number of rows can not be changed

Hello everyone, I have a error when I run a code that reads about 4,500 images and the error occurred when reading the 455th image. The error is

OpenCV Error: Image step is wrong (The matrix is not continuous, thus its number of rows can not be changed) in reshape, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/core/src/matrix.cpp, line 1102 terminate called after throwing an instance of 'cv::Exception' what(): /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/core/src/matrix.cpp:1102: error: (-13) The matrix is not continuous, thus its number of rows can not be changed in function reshape

Could you give me some help,thanks a lot! And the code are as follows

#include "vo_features.h"
using namespace cv;
using namespace std;

#define MAX_FRAME 1000
#define MIN_NUM_FEAT 4540

int main( int argc, char** argv )   {

 // we work with grayscale images
  Mat img_1, img_2;
  Mat R_f, t_f; //the final rotation and tranlation vectors containing the 

  ofstream myfile;
  myfile.open ("results1_1.txt");

  double scale = 1.00;
  char filename1[200];
  char filename2[200];
  sprintf(filename1, "/home/zxf/vo/00/image_0/%06d.png", 0);
  sprintf(filename2, "/home/zxf/vo/00/image_0/%06d.png", 1);

  char text[100];
  int fontFace = FONT_HERSHEY_PLAIN;
  double fontScale = 1;
  int thickness = 1;  
  Point textOrg(10, 50);

  // we work with grayscale images
  //read the first two frames from the dataset
  img_1 = imread(filename1);
  img_2 = imread(filename2);

  if ( !img_1.data || !img_2.data ) { 
    std::cout<< " --(!) Error reading images " << std::endl; return -1;
  }

  // feature detection, tracking
  vector<Point2f> points1, points2;        //vectors to store the coordinates of the feature points
  featureDetection(img_1, points1);        //detect features in img_1
  vector<uchar> status;
  featureTracking(img_1,img_2,points1,points2, status); //track those features to img_2
  double focal = 718.8560;
  Point2d pp(607.1928, 185.2157);
  //recovering the pose and the essential matrix
  Mat E, R, t, mask;
  E = findEssentialMat(points2, points1, focal, pp, RANSAC, 0.999, 1.0, mask);
  recoverPose(E, points2, points1, R, t, focal, pp, mask);

  Mat prevImage = img_2;
  Mat currImage;
  vector<Point2f> prevFeatures = points2;
  vector<Point2f> currFeatures;
  char filename[100];
  R_f = R.clone();
  t_f = t.clone();

  clock_t begin = clock();
  namedWindow( "Road facing camera", WINDOW_AUTOSIZE );// Create a window for display.
  namedWindow( "Trajectory", WINDOW_AUTOSIZE );// Create a window for display.
  Mat traj = Mat::zeros(600, 600, CV_8UC3);

  for(int numFrame=2; numFrame < MAX_FRAME; numFrame++) {
    sprintf(filename, "/home/zxf/vo/00/image_0/%06d.png", numFrame);
        cout << numFrame << endl;
    Mat currImage_= imread(filename);
        Mat patchMatTmp3;

        currImage_.copyTo(patchMatTmp3);
        currImage= patchMatTmp3.reshape(0,0);
    vector<uchar> status;
    featureTracking(prevImage, currImage, prevFeatures, currFeatures, status);
    E = findEssentialMat(currFeatures, prevFeatures, focal, pp, RANSAC, 0.999, 1.0, mask);
    recoverPose(E, currFeatures, prevFeatures, R, t, focal, pp, mask);

        Mat prevPts(2,prevFeatures.size(), CV_64F), currPts(2,currFeatures.size(), CV_64F);
        for(int i=0;i<prevFeatures.size();i++)  {  
        prevPts.at<double>(0,i) = prevFeatures.at(i).x;
        prevPts.at<double>(1,i) = prevFeatures.at(i).y;

        currPts.at<double>(0,i) = currFeatures.at(i).x;
        currPts.at<double>(1,i) = currFeatures.at(i).y;
        }
    scale = 1;//getAbsoluteScale(numFrame, 0, t.at<double>(2));

        if ((scale>0.1)&&(t.at<double>(2) > t.at<double>(0)) && (t.at<double>(2) > t.at<double>(1))) {

            t_f = t_f + scale*(R_f*t);
            R_f = R*R_f;

        }      
        myfile << t_f.at<double>(0) << " " << t_f.at<double>(1) << " " << t_f.at<double>(2) << endl;
        if (prevFeatures.size() < MIN_NUM_FEAT) {

        featureDetection(prevImage, prevFeatures);
            featureTracking(prevImage,currImage,prevFeatures,currFeatures, status);
        }

        prevImage = currImage.clone();
        prevFeatures = currFeatures;
        int x = int(t_f.at<double>(0)) + 300;
        int y = int(t_f.at<double>(2)) + 100;
        circle(traj, Point(x, y) ,1, CV_RGB(255,0,0), 2);
        rectangle( traj, Point(10, 30), Point(550, 50), CV_RGB(0,0,0), CV_FILLED);
        sprintf(text, "Coordinates: x = %02fm y = %02fm z = %02fm", t_f.at<double>(0), t_f.at<double>(1), t_f.at<double>(2));
        putText(traj, text, textOrg, fontFace, fontScale, Scalar::all(255), thickness, 8);  

        imshow( "Road facing camera", currImage);
        imshow( "Trajectory", traj );
        waitKey(1);
  }
  clock_t end = clock();
  double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  cout << "Total time taken: " << elapsed_secs << "s" << endl;
  return 0;
}