天天看點

OpenCV-4-儲存mat資料為一個檔案

#include <opencv2/opencv.hpp>  
#include <iostream>  
#include <stdio.h>
using namespace std;
using namespace cv;

static void saveXYZ(const char* filename, const Mat& mat)
{
	FILE* fp = fopen(filename,"wt"); //"wt" 隻寫打開或建立一個文本檔案,隻讀寫資料 (t為文本檔案)
	printf("%d %d \n", mat.rows, mat.cols);
	for (int y = 0; y < mat.rows; y++)
	{
		for (int x = 0; x < mat.cols; x++)
		{
			int point = mat.at<int>(y, x);
			cout << point << endl;

			fprintf(fp, "%d\n", point);
		}
	}
	fclose(fp);
}
int main()
{
	Mat a = (Mat_<int>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
	//cout << a << "\n"<< endl;
	saveXYZ("a.txt", a);
	
	system("pause");
	return 0;
}
           

FILE* fp = fopen(filename,“wt”); //建立檔案

  fprintf(fp, “%d\n”, point); //檔案資料輸入

  fclose(fp); //關閉檔案

“rt” 隻讀打開一個文本檔案,隻讀資料

“wt” 隻寫打開或建立一個文本檔案,隻讀寫資料

"at"追加打開一個檔案檔案,并在檔案未層寫資料

"rb"隻讀打開一個二進制檔案,隻讀資料

"wb"隻寫打開或建一個二制檔案,隻寫資料

"ab"追加打開一個二進制檔案,并在示尾寫資料

"rt+"讀寫打開或建立一個文本檔案,允許讀寫

"wt+"讀寫打開或建立一個文本檔案,允許讀寫

"at+"讀寫打開一個文本檔案,允許讀,或在檔案未追加資料

"rb+"讀寫打開一個二進制檔案,允許讀和寫

“wb+” 讀寫打開或建立一個二進制檔案,允許讀和寫

"ab+"讀寫打開一個二進制檔案,允許讀,或在檔案未追加資料

檔案使用方式由—r,w,a,t,b,+,六個字元拼成,分别是:

 r(read)讀

 w(write)寫

 a(append)追加

 t(text)文本檔案,可省略不寫

 b(banary) 二進制檔案

 + 讀和寫