I am trying to read data from a file, store it in a double array and then use the Mat constructor to create an object containing the data. However the output is not as expected.

ifstream in;
in.open("data");
double d[500][2];
for(int i=0;i<500;i++)
{
    for(int j=0;j<2;j++)
    {
        in>>d[i][j];

    }
}

Mat samples = Mat(500,2,CV_32FC1,&d);

for(int i=0;i<5;i++)
{
    cout<<"\n";
    for(int j=0;j<2;j++)
    {
        cout<<d[i][j]<<" ";
    }
}
cout<<"\nSamples";

for(int i=0;i<5;i++)
{
    cout<<"\n";
    for(int j=0;j<2;j++)
    {
        cout<<samples.at<double>(i,j)<<" ";
    }
}
有帮助吗?

解决方案

Mat samples = Mat(500,2,CV_32FC1,&d); // that's a *float* Mat !

you want;

Mat samples = Mat(500,2,CV_64FC1,&d);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top