The cv::Mat function in OpenCV writes the data to the txt file
- 2020-06-03 07:58:04
- OfStack
Image processing using opencv often involves reading data from a file into cv::Mat or writing data from cv::Mat to an txt file.
Here is a common method I use to write data from cv::Mat to an txt file, as shown in the code:
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();
}