I want to get binary data from a bin file.

10 次查看(过去 30 天)
How can I get the binary data from the bin file, obtained by Labview?
In Labview, we obtained image data (bin file and bmp file) of 256x256pixel atoms.
Attached below is the atomic image of the bmp file obtained by Labview.
I would like to retrieve the binary data of a 256x256 pixel atomic image from a bin file in matlab and convert the binary data into an image.
By doing so, I would like to confirm that the atomic image in the above bmp file and the atomic image drawn from the acquired binary data are the same.
  1 个评论
Walter Roberson
Walter Roberson 2025-2-6
However, it appears that LabView .bin files have arbitrary binary formats, and do not typically have headers indicating the data format. Perhaps you might get lucky and there will be a header indicating the array sizes.

请先登录,再进行评论。

回答(2 个)

Umar
Umar 2025-2-7

Hi @大地 ,

After going through your comments, in order to retrieve binary data from a LabVIEW-generated bin file in MATLAB and convert it into an image, you can follow these steps:

1. Read the Binary File: Use the fopen and fread functions to read the binary data from the bin file.

2. Reshape the Data: Since the image is 256x256 pixels, reshape the data into a 2D matrix.

3. Convert to Image: Use the imshow function to display the image.

Here is a sample code snippet to achieve this:

% Specify the path to the bin file
filePath = 'path_to_your_file.bin';
% Open the binary file
fileID = fopen(filePath, 'r');
% Read the binary data
data = fread(fileID, [256, 256], 'uint8'); % Adjust data type as necessary
% Close the file
fclose(fileID);
% Display the image
imshow(data, []);
title('Image from Binary Data');
% Optionally, read and display the BMP file for comparison
bmpImage = imread('path_to_your_file.bmp');
figure;
imshow(bmpImage);
title('Original BMP Image');

This code will help you visualize the binary data as an image and allow you to compare it with the BMP file to confirm their similarity. Ensure that the data type in fread matches the format used in LabVIEW.

Hope this helps.

  11 个评论
Umar
Umar 2025-2-9

Hi @大地,

Please see attached. I did comparison check and both images are not equal. I did find out that there is array size are not compatible and images are different dimensions.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2025-2-9
编辑:Walter Roberson 2025-2-9
I was right to be concerned about the file format.
The actual file format involves an 8 byte header that gives array sizes. Then the data is in double precision. Data values range from 0 to 2.77
filenames = unzip('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1825621/20250120%20111341_xyfreq450_xamp30_yamp30.zip');
[fid, msg] = fopen(filenames{1});
if fid < 0; error('cannot open file "%s" because "%s"', filenames{1}, msg); end
size1 = fread(fid, 1, '*uint32', 'ieee-be');
size2 = fread(fid, 1, '*uint32', 'ieee-be');
data = fread(fid, [size1 size2], '*double', 'ieee-be');
fclose(fid);
pcolor(data, 'edgecolor', 'none');
colormap(copper)
  8 个评论
Walter Roberson
Walter Roberson 2025-2-12
The image is created in double precision, so 53 bits of precision.
That 53 bits of precision is being mapped through your color table. The size of color tables typically defaults to 256 entries, but you could, for example,
colormap(copper(65536))
to use 65536 color table entries.
The data itself appears to be numbers such as 2.14, with a range from 0 to 2.77 . All of the values appear to be rounded to the nearest 1/100, so in theory there are 278 of them (0 to 277, divided by 100). log2(278) is marginally over 8. So the data source is theoretically 9 bits
薫
2025-3-9
Thank you very much for your interest.
I created a program based on what you said and it worked.
Thank you very much.

请先登录,再进行评论。

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by