How to save variables in matlab in most optimal way

26 views (last 30 days)
Hello, i have being trying to implement multiple image compression algorithms in matlab (such as RLE) but when encoding an image and trying to save the encoded matrix as a .mat file, it always becomes larger when it should be smaller thoeratically
so i want to know what other ways can you save variables such that it gets the original size it should have
thanks in advace
  4 Comments
dpb
dpb on 14 May 2022
Edited: dpb on 15 May 2022
What's the precison of your raw data? Use the same.
There may be another place where MATLAB is increasing the storage size that you're not expecting -- by default it turns virtually everything numeric into double precision, even image data that may only be 8-bit depth.
We would have to know the details of what you're doing to a much greater depth than you've divulged so far to know anything specific.
Adel Hafri
Adel Hafri on 15 May 2022
okay so i have a 750x750 jpeg pictures with values ranging from 0 to 255 and im supposed to apply losless image compression algorithms to reduce the size of those pictures, lossless image compression algorithms such as huffman work by reducing the length of frequent occuring symbols, for example if 150 was my most occuring then my huffman algorithm gives it the code 0 for example and so i ll be saving 7 bits times the frequncy of that data which means compression, the problem is matlab automatically makes that 1 bit length 0 into a 00000000 so essentialy, my algorithm is pointless since matlab will make all the data 8 bit length again, so i want a way to save data exactly the size i want, whether 1 bit,2bits.3....etc instead of it forcing all data to be 8bits
here is an example of how the algorithm changes the symbols
the picture i used isnt the best example of compression but you can get the idea

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 15 May 2022
fwrite() with the 'bit1' precision.
For compression calculations purposes you should never be using the size of mat files. mat files have overhead for every variable, and mat files have their own compression built in.
  7 Comments
Adel Hafri
Adel Hafri on 3 Jun 2022
yeah man, you are a god send, your method worked perfectly thank you so much
i have just one issue now and it's that when im using bit1 for my bitstream it's saving all values as 0, while if i use 'bit2' it works perfectly, any idea why is that happening ?
it's happening even in the example u gave me
bits = {[1] [0 0] [1] [0 1 1] }
Bitstream = [bits{:}];
fid = fopen('test.bin','w');
fwrite(fid, Bitstream, 'bit1');
fclose(fid);
Adel Hafri
Adel Hafri on 3 Jun 2022
nevermind, i fixed it, i had to use ubit1 instead of bit1 for unsigned binary
again thank you so much man, you are a life saver

Sign in to comment.

More Answers (0)

Categories

Find more on Denoising and Compression in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!