Mat保存为txt文件

Mat写入txt文件

void saveMat(Mat I, string path)
{
	ofstream matFile;
	matFile.open(path, ios::out);
	if (!matFile.fail())
	{
		for (int i = 0; i < I.rows; i++)
		{
			for (int j = 0; j < I.cols; j++)
			{
				matFile << I.at<double>(i, j) << "\t";
			}
			matFile << std::endl;
		}
	}
	matFile.close();
}

从txt文件读入Mat

Mat readMat(string path)
{
	ifstream fin(path);
	Mat gray(333, 65, CV_64FC1);
	for (int i = 0; i < gray.rows; i++)
	{
		for (int j = 0; j < gray.cols; j++)
		{
			double a;
			fin >> a;
			gray.at<double>(i, j) = a;//
		}
	}
	return gray;
}

你可能感兴趣的:(c++,opencv)