How to save variables in matlab in most optimal way

34 次查看(过去 30 天)
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 个评论
dpb
dpb 2022-5-14
编辑:dpb 2022-5-15
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 2022-5-15
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

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2022-5-15
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 个评论
Adel Hafri
Adel Hafri 2022-6-3
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 2022-6-3
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Denoising and Compression 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by