matlab存TXT数据
clear,clc
close all
im = imread('sd.png');
%figure,imshow(im)
data = single(im);
%% 将数据存为.txt
% img = single(im);
img = permute(data,[2,1,3]);
fid5=fopen('data.txt','w+');
fwrite(fid5,img,'float');
fclose(fid5);
使用C++读取,为了验证数据读进来是一样,我在上面的代码中是将一张图片存储为.txt文件,在C++中读入后,我将使用opencv对其进行显示。
C++读取数据
#include <iostream>
#include <fstream>
#include"opencv2/core.hpp"
#include"opencv2/highgui.hpp"
#include"opencv_lib.h"
using namespace std;
using namespace cv;
#define ROW2 718
#define COL2 640
#define depth 1
template <class T>
bool Input_3D_data(T P[], const char* s1)
{
FILE* fid0 = fopen(s1, "rb");
if (fid0 == NULL)
{
cout << "Open error !!!" << endl;
fclose(fid0);
return 0;
}
int shft = 0;
fseek(fid0, shft, SEEK_CUR);
memset(P, 0, sizeof(float));
fread(P, ROW2 * COL2 * depth * sizeof(float), 1, fid0);
fclose(fid0);
}
int main()
{
//float(*blurImg) = new float[depth * ROW2 * COL2](); // 录入原图
float blurImg[depth * ROW2 * COL2] = { 0 };
const char* f0 = "E:/C_project/data.txt";
int cheak = 0;
Input_3D_data<float>(blurImg, f0);
Mat img(ROW2, COL2, CV_32F, blurImg);
imshow("img", img);
waitKey(0);
return 0;
}