Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab writing image data to a binary file

Tags:

matlab

I am not very good at Matlab and I needed help with the following code.

I have the image data from a set of images and I want to save these to a binary file along with a file signature showing how the data has been saved in the file.

For example, say I have an image that is divided such that it is said to consist of 4 rows and 4 columns so that the image is made up of 16 smaller images. The image data for these smaller images is saved in a structure as follows:

data = struct('imageTitleFinal', {}, 'imageDataFinal', {});

for rows = 1:numberOfRows
        for columns = 1:numberOfColumns
            data(rows,columns).imageTitleFinal = currentTile;
            data(rows,columns).imageDataFinal = currentStructure(rows,columns).imageData;
        end
end

I want to be able to write this image data for each of the smaller images to a binary file and be able to distinguish between the sets of image data within the file. Any help would be much appreciated.

like image 274
kushaldsouza Avatar asked Nov 28 '25 03:11

kushaldsouza


1 Answers

If you HAD to write to a binary file for whatever reasons, you could potentially create your own 'header' for this data.

Write a fixed amount of meta-data for each 'smaller image', this data should have all the information to help you parse your image data. A simple way would be:

-I assume your 'title' is a char array -I assume your data file is a double array -The '|' is just a separator.

|A uint32 with the number of chars in your title|Your title data written as chars| A uint32 with the number of doubles in your data|All your data written as doubles|

Air code:

fopen in append mode
fwrite(fid, numel(title), 'uint32');
fwrite(fid, title,'char'); %assuming ASCII char set
fwrite(fid, numel(data), 'uint32');
fwrite(fid, data, 'double);

You can consider adding more 'meta-data', for example, the size of the data, if your 'smaller images' will not have uniform size.

like image 108
Ashish Uthama Avatar answered Nov 30 '25 19:11

Ashish Uthama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!