Converting Binary files to ASCII

8 Ansichten (letzte 30 Tage)
Marcos Mora
Marcos Mora am 21 Mär. 2019
Kommentiert: Edwin Cortez am 1 Mai 2020
Hello everyone,
I am not sure how to convert a Binary file into ASCII file by using matlab. I have seen other similar questions about this in this page but I am not sure about the format of my binary file. It contains points from a disparity map of an image. I have also tried using the tutorials from Matlab, but still I get a wrong result.I attach some binary files so that someone can help
This is the matlab tutorial i was talking about:
A = fread(fileID)
A = fread(fileID,sizeA)
A = fread(fileID,sizeA,precision)
A = fread(fileID,sizeA,precision,skip)
A = fread(fileID,sizeA,precision,skip,machinefmt)
[A,count] = fread(___)
Please, any help would be appreciated.
Thanks in advance
  3 Kommentare
dpb
dpb am 21 Mär. 2019
[MM Answer moved to comment...dpb]
This is the only thing the creator said:
Additionally we provide the velodyne point clouds for point-cloud-based methods. To save space, all scans have been stored as Nx4 float matrix into a binary file using the following code:
stream = fopen (dst_file.c_str(),"wb");
fwrite(data,sizeof(float),4*num,stream);
fclose(stream);
Here, data contains 4*num values, where the first 3 values correspond to x,y and z, and the last value is the reflectance information. All scans are stored row-aligned, meaning that the first 4 values correspond to the first measurement. Since each scan might potentially have a different number of points, this must be determined from the file size when reading the file, where 1e6 is a good enough upper bound on the number of values:
// allocate 4 MB buffer (only ~130*4*4 KB are needed)
int32_t num = 1000000;
float *data = (float*)malloc(num*sizeof(float));
// pointers
float *px = data+0;
float *py = data+1;
float *pz = data+2;
float *pr = data+3;
// load point cloud
FILE *stream;
stream = fopen (currFilenameBinary.c_str(),"rb");
num = fread(data,sizeof(float),num,stream)/4;
for (int32_t i=0; i<num; i++) {
point_cloud.points.push_back(tPoint(*px,*py,*pz,*pr));
px+=4; py+=4; pz+=4; pr+=4;
}
fclose(stream);
x,y and y are stored in metric (m) Velodyne coordinates.
dpb
dpb am 21 Mär. 2019
Well, that's quite a lot of "only"... :)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

dpb
dpb am 21 Mär. 2019
OK, that's all you should need...
fid=fopen('yourfile.ext','r');
data=fread(fid,[inf,4],'single');
fid=fclose(fid);
should return you a Nx4 data array of the x,y,z and reflectance values for each of N measurements.
  3 Kommentare
Marcos Mora
Marcos Mora am 21 Mär. 2019
Yeah, it worked with Walter Roberson's code, but thanks to both of you!
You both were really helpful!
Edwin Cortez
Edwin Cortez am 1 Mai 2020
Walter Roberson's code also worked for me. Thank you.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by