0

I am trying to read in data from a text file but am not getting the right values. Only the values of width0 and height0 and width1 height 1 are right. All the values after the 2nd line are wrong. Can anyone tell me why ?

string line, thrash;    
std::istringstream in;
ifstream myfile("camera_parameters.txt");

float width0, height0, width1, height1; // Initial and Final image sizes
float fx1, fy1, cx1, cy1;   // L camera matrix
float fx2, fy2, cx2, cy2;   // R camera matrix
float a1, b1, c1, d1, e1;   // L camera distortion matrix
float a2, b2, c2, d2, e2;   // R camera distortion matrix
float tx, ty, tz;           // Trans from L to R camera
float r00, r01, r02,        // Rotat from L to R camera
      r10, r11, r12, 
      r20, r21, r22;

std::getline(myfile, line); in.str(line);
in >> thrash;
in >> width0 >> height0;
in.str(""); in.clear();

std::getline(myfile, line); in.str(line);
in >> thrash; 
in >> width1 >> height1;
in.str(""); in.clear();

std::getline(myfile, line); in.str(line);
in >> thrash;
in >> tx >> ty >> tz;
in.str(""); in.clear();
cout << tx << " " << ty << endl;

std::getline(myfile, line); in.str(line);
in >> thrash;
in >> r00 >> r01 >> r02 >> r10 >> r11 >> r12 >> r20 >> r21 >> r22;
in.clear(); in.str(std::string());

std::getline(myfile, line); in.str(line);
in >> thrash;
in >> fx1 >> thrash >> cx1 >> thrash >> fy1 >> cy1;
in.clear(); in.str(std::string());

std::getline(myfile, line); in.str(line);
in >> thrash;
in >> a1 >> b1 >> c1 >> d1 >> e1;
in.clear(); in.str(std::string());

std::getline(myfile, line); in.str(line);
in >> thrash;
in >> fx2 >> thrash >> cx2 >> thrash >> fy2 >> cy2;
in.clear(); in.str(std::string());

std::getline(myfile, line); in.str(line);
in >> thrash;
in >> a2 >> b2 >> c2 >> d2 >> e2;
in.clear(); in.str(std::string());

Below are my data in a text file

S_00: 1.392000e+03 5.120000e+02
S_rect_00: 1.242000e+03 3.750000e+02    
T_01: -5.370000e-01 4.822061e-03 -1.252488e-02    
R_01: 9.993513e-01 1.860866e-02 -3.083487e-02 -1.887662e-02 9.997863e-01 -8.421873e-03 3.067156e-02 8.998467e-03 9.994890e-01    
K_00: 9.842439e+02 0.000000e+00 6.900000e+02 0.000000e+00 9.808141e+02 2.331966e+02 0.000000e+00 0.000000e+00 1.000000e+00    
D_00: -3.728755e-01 2.037299e-01 2.219027e-03 1.383707e-03 -7.233722e-02    
K_01: 9.895267e+02 0.000000e+00 7.020000e+02 0.000000e+00 9.878386e+02 2.455590e+02 0.000000e+00 0.000000e+00 1.000000e+00    
D_01: -3.644661e-01 1.790019e-01 1.148107e-03 -6.298563e-04 -5.314062e-02
Kong
  • 2,202
  • 8
  • 28
  • 56
  • 1
    Well, debug it. Print out each value (including the entire line stored in the `line` variable) as you read it, and see what goes wrong. Also check failbit. – Ben Voigt Oct 08 '16 at 17:41
  • It seems like at the 2nd line, getline reads in the correct line (S_rect_00: 1.242000e+03 3.750000e+02 ) but in >> thrash reads in S_00... – Kong Oct 08 '16 at 17:46
  • Closely related to http://stackoverflow.com/q/16443153/103167 – Ben Voigt Oct 08 '16 at 17:47
  • You have serious code smells 1) Declaring variables at the beginning of a function (instead of declaring each right at usage) 2) Dealing with too many variables in a single function (split that function) –  Oct 08 '16 at 17:55
  • Seems like im not able to get it right even after http://stackoverflow.com/questions/2848087/how-to-clear-stringstream what a pos function – Kong Oct 08 '16 at 18:06
  • So no one has any idea how to get this function working ? is it no longer in use ? is there a different function to read things from txt files? – Kong Oct 09 '16 at 03:53

1 Answers1

1

After in >> width0 >> height0;, the EOF flag is set on in, which will prevent any further usage.

Associating the stream with new data via in.str(line) doesn't reset the flags, you will have to do that yourself via in.clear().

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • @kong: You're still not telling us how far it gets before it goes wrong. – Ben Voigt Oct 08 '16 at 18:16
  • Hi thanks ! The values of tx ty tz (3rd line onwards) are all wrong. Maybe it'll be easier to just create a new stringstream everytime. This task is so much easier in matlab – Kong Oct 08 '16 at 18:19
  • Hi its working now. I don't know why but I just had to restart visual studio I think... Thanks :) – Kong Oct 09 '16 at 04:47