I’m trying to read a data file (1000 x 5 doubles) into a Mat object. The Mat object was initialized as Mat::zeros(1000,5,CV_32F). While each line of data were read, the corresponding row were assigned to the new values. The code is shown below:
void readNavFile(const char* filename, Mat& navdata) {
ifstream infile(filename);
string line;
int x=0, y=0;
if (infile.is_open()) {
while (getline(infile, line)) {
double value;
stringstream converter(&line[0]);
double* p = navdata.ptr<double>(x);
while (converter >> value) {
p[y++] = value;
//DEBUGING
cout << "Pointer value:" << p[y-1] << endl;
cout << "Matrix value at:" << "row:" << x << " col:" << y-1 <<" is: "<< navdata.at<double>(x,y-1)<< endl;
cout << navdata.at<double>(0,4) << endl;
}
y = 0;
x++;
}
infile.close();
//DEBUGING
cout << "Final matrix first row, last element:" << navdata.at<double>(0,4) << endl;
}
}
As you can see, I try to print out the value of the Mat (matrix) element as they are assigned so I can be sure the correct values are stored. The weird thing is the assignment all went well however some of the previously assigned elements would all of suddent change their value. A sample output looks like the followings, at the last line, after assigning element [1,2], the value at [0,4] changed from its correct value 18.1901 to 3.31757e-190:
Pointer value:-35.1236
Matrix value at:row:0 col:0 is: -35.1236
Matrix element [0,4]: 0
Pointer value:150.735
Matrix value at:row:0 col:1 is: 150.735
Matrix element [0,4]: 0
Pointer value:360.094
Matrix value at:row:0 col:2 is: 360.094
Matrix element [0,4]: 0
Pointer value:3.46045
Matrix value at:row:0 col:3 is: 3.46045
Matrix element [0,4]: 0
Pointer value:18.1901
Matrix value at:row:0 col:4 is: 18.1901
Matrix element [0,4]: 18.1901
Pointer value:-35.1236
Matrix value at:row:1 col:0 is: -35.1236
Matrix element [0,4]: 18.1901
Pointer value:150.735
Matrix value at:row:1 col:1 is: 150.735
Matrix element [0,4]: 18.1901
Pointer value:360.096
Matrix value at:row:1 col:2 is: 360.096
Matrix element [0,4]: 3.31757e-190
Could someone help me to figure out what’s going on? Thank you very much!
32F stands for float in OpenCV.
navdata.at<double>(0,4)is incorrect. You should usenavdata.at<float>(0,4)or, change your datatype to 64F. Also, OpenCV should have thrown an exception for your code. Are you sure it didn’t?