嗨,我已经能够将 Mat 对象写入文本文件。如下,
std::fstream outputFile;
outputFile.open( "myFile.txt", std::ios::out ) ;
outputFile << des_object.rows << std::endl;
outputFile << des_object.cols << std::endl;
for(int i=0; i<des_object.rows; i++)
{
for(int j=0; j<des_object.cols; j++)
{
outputFile << des_object.at<float>(i,j) << std::endl;
}
}
outputFile.close( );
在我的前两行代码中,我打印了行数和列数,以便在我读回它时使用。但我无法读取文本文件并再次创建 Mat 对象。
以下是我尝试过的代码。不确定我的代码是否正确。
Mat des_object1;
std::ifstream file("myFile.txt");
std::string str;
int rows;
int cols;
int a = 0;
while (std::getline(file, str))
{
int i = 0;
int j = 0;
if(a == 0){
rows = std::stoi( str );
}else if(a == 1){
cols = std::stoi( str );
}else{
for(i; i< rows; i++)
{
for(j; j<cols; j++)
{
des_object1.at<float>(i,j) = ::atof(str.c_str());
break;
}
}
}
++a;
}