How to save variables in matlab in most optimal way

조회 수: 20 (최근 30일)
Adel Hafri
Adel Hafri 2022년 5월 14일
댓글: Adel Hafri 2022년 6월 3일
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에서 Large Files and Big Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by