matlab存txt & c++读取

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;
}

发布了8 篇原创文章 · 获赞 18 · 访问量 4564

猜你喜欢

转载自blog.csdn.net/weixin_42435145/article/details/104163286