在使用opencv進行圖像處理的過程中,經常會涉及到將文件中的數據讀入到cv::Mat中,或者將cv::Mat中的數據寫入到txt文件中。
下面就介紹一種我常用的將cv::Mat中的數據寫入到txt文件中的方法,具體見代碼:
void writeMatToFile(cv::Mat& m, const char* filename)
{
std::ofstream fout(filename);
if (!fout)
{
std::cout << "File Not Opened" << std::endl;
return;
}
for (int i = 0; i<m.rows; i++)
{
for (int j = 0; j<m.cols; j++)
{
fout << m.at<float>(i, j) << "\t";
}
fout << std::endl;
}
fout.close();
}
2017.05.11