I want to get binary data from a bin file.

1 Comment
Answers (2)
0 votes
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.
Hi @Walter Robertson,
I do agree with your comments. Now let see what is OP’s response
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 @大地,
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.

8 Comments
Hi @Walter Robertson,
I completely agree with your comments.
Categories
Find more on Standard File Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

