I want to get binary data from a bin file.

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 Comment

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.

Sign in to comment.

Answers (2)

Umar
Umar on 7 Feb 2025

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 Comments

This code is not necessarily wrong, but it is not necessarily right either. We just do not know the binary format used in the bin file. It just might include a header describing the array size and possibly describing the datatype as well.

I am also concerned about the colour of the image. Possibly what we are shown in pseudocolor; if so we would need to know the color table to replicate it. But possibly the image is RGB; if so we would need to know the pixel component order.

Hi @Walter Robertson,

Thank you for your insightful feedback regarding the code and the image format. I appreciate your thorough analysis of the binary file structure, particularly your observations about the potential inclusion of a header that may describe both the array size and datatype. I understand your concerns about the color representation of the image. It is indeed crucial to clarify whether we are dealing with pseudocolor or RGB formats, as this will significantly impact our approach to accurately replicating the image. I will look into obtaining more information about the color table and pixel component order to ensure we address these aspects effectively. If you have any additional suggestions or resources that could assist us in resolving these uncertainties, please feel free to share them. Thank you once again for your valuable input.

The labview documentation for binary write indicates that it just writes whatever information it is given. Unfortunately, we do not have enough information about what information was requested to be written out. Just knowing that a binary file was used is not enough information.
It would help if the user were to attach the file -- by examing the file size we could deduce the existence of headers and whether it is grayscale or RGB. If it turns out to be RGB, then we could make experiments on component orders to see how closely we could recreate the image.

Hi @Walter Robertson,

I do agree with your comments. Now let see what is OP’s response

Thank you all for your cooperation!
The size of the bin file was 512KB.
The bin file format cannot be attached to this comment.
I also created an image from the binary data obtained using the program that @Umar gave me.
% Specify the path to the bin file
filePath = '20250120 111341_xyfreq450_xamp30_yamp30.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('20250120 111341_xyfreq450_xamp30_yamp30.bmp');
figure;
imshow(bmpImage);
title('Original BMP Image');
Attached below are the images made from the binary data obtained and the bmp file images.
If these two images look similar, it means that the binary data has been acquired.
However, it does not look like it!
 
You can zip up the bin file and attach the zip file.

Hi @ 大地,

Is it possible for you to accomplish the task mentioned in @Walter Robertson’s comments. Please let us know.

Hi @ 大地,

Upon reviewing the provided code by you,I can identify the following potential issues.

File Format and Data Type: The binary file is read as uint8, which may not be appropriate depending on the actual data format of the binary file. If the binary data is not in the expected format, the resulting image will not display correctly.

Data Dimensions: The fread function is set to read a 256x256 matrix. If the binary file does not contain exactly 65536 bytes (256 * 256), this could lead to unexpected results or errors.

File Path: If the specified file path is incorrect or the file does not exist, fopen will return -1, leading to subsequent errors when attempting to read from fileID.

Hope this helps.

Hi @ Umar,
I have converted the bin file to a zip file, which is attached.
Could you please check it?

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.

Also, please see attached code.

Sign in to comment.

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 Comments

Hi @Walter Robertson,

I completely agree with your comments.

color is arbitary. The data is not RGB, so it has to be pseudocolored; we do not know what lookup table was used.
Thank you for your response.
At first glance, it appears to be close to the image in the bmp file.
Is there any way to depict the unevenness of the atoms more clearly than this?
(This data represents an image of an atom.)
Also, the above program reads a zip file, but how should I change it to read a bin file?
Change
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
to
filePath = '20250120 111341_xyfreq450_xamp30_yamp30.bin';
[fid, msg] = fopen(filePath);
if fid < 0; error('cannot open file "%s" because "%s"', filePath, msg); end
Is there any way to depict the unevenness of the atoms more clearly than this?
Adjust the colormap from copper() to some custom map.
Thank you very much.
We were able to output a cleaner image.
I have one more question.
Is this image being output in 16bit?
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
Thank you very much for your interest.
I created a program based on what you said and it worked.
Thank you very much.

Sign in to comment.

Products

Release

R2024b

Asked:

薫
on 6 Feb 2025

Commented:

薫
on 9 Mar 2025

Community Treasure Hunt

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

Start Hunting!