2

我正在尝试将数据文件(1000 x 5 doubles)读入 Mat 对象。Mat 对象被初始化为 Mat::zeros(1000,5,CV_32F)。在读取每一行数据时,将相应的行分配给新值。代码如下所示:

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;
    }
}

如您所见,我尝试在分配 Mat(矩阵)元素时打印出它们的值,以便确保存储了正确的值。奇怪的是分配一切顺利,但是一些先前分配的元素会突然改变它们的值。示例输出如下所示,在最后一行,在分配元素 [1,2] 后,[0,4] 处的值从其正确值 18.1901 更改为 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

有人可以帮我弄清楚发生了什么吗?非常感谢你!

4

1 回答 1

1

32F 代表 OpenCV 中的浮点数。navdata.at<double>(0,4)是不正确的。您应该使用navdata.at<float>(0,4)或将您的数据类型更改为 64F。此外,OpenCV 应该为您的代码抛出异常。你确定没有?

于 2012-07-24T06:16:10.647 回答