[英]I am reading a bin file using MATLAB and python. They are reading header differently
I am trying to read a binary file using MATLAB and python.我正在尝试使用 MATLAB 和 python 读取二进制文件。 MATLAB is producing header
MATLAB 正在生产 header
[80 0 8 0 1780 400 357 22441] whereas python is reading the header [80 0 8 0 1780 400 357 22441] 而 python 正在读取 header
[ 80 8 1780 357 258 22440 2040 2048]. [80 8 1780 357 258 22440 2040 2048]。
What is the wrong I am doing???我在做什么错??? I am running both on windows.
我都在 windows 上运行。
**MATLAB code:**
fid = fopen(file_name);
fread(fid,isTherePos(1)-1,'uint32','l');
header = fread(fid,8,'uint16','l')
**Python code:**
with open(filename, mode='rb') as f:
b = f.read()
np_data = np.frombuffer(b, dtype='<I')
srtOfLine = LidarMRC.findStartOfLine(filename)
header = np_data[srtOfLine[0][0]:srtOfLine[0][0] +8].astype('<H')
print(header)
In the MATLAB code, you read the first "isTherePos(1)-1" bytes as 32-bit values.在 MATLAB 代码中,您将第一个“isTherePos(1)-1”字节读取为 32 位值。 Then you read the header as 8x 16-bit values.
然后您将 header 读取为 8x 16 位值。
In the Python code, you read the whole file into np_data as 32-bit values.在 Python 代码中,您将整个文件作为 32 位值读入 np_data。 The "astype" function does not re-interpret those bytes.
“astype” function 不会重新解释这些字节。 It takes each 32-bit entity and truncates it to 16 bits, which is exactly what you see.
它采用每个 32 位实体并将其截断为 16 位,这正是您所看到的。
You should read np_data as dtype=np.uint16
, then adjust your srtOfLine number to multiply by 2.您应该将 np_data 读取为 dtype
dtype=np.uint16
,然后将 srtOfLine 编号调整为乘以 2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.