Ho we can Implement Unary compression method and create Unary encoder decoder in matlab?

3 views (last 30 days)
Hi, I want to Implement the Unary compression method for compressing Grayscale images how I can do that?
My main goal is to create a function that the output must be the compression rate, which is calculated based on the size of the original image file and the size of the compressed file.
one function for the unary encoder and another one for the unary decoder.
Thank you for your participation

Accepted Answer

Walter Roberson
Walter Roberson on 20 Nov 2021
Mathematically, you can consider a file of bytes to be a Base 256 encoding of an integer. For example if the file contained just the bytes 241 217 (decimal) then that could be considered to mathematically be a representation of 241*256+217... whatever that number happens to work out as.
The unary representation of a number requires taking that many copies of a symbol in a row. For example if the value is 19 then the unary would be 19 copies of a symbol. Like ::::::::::::::::::: or 1111111111111111111. The end of the number is marked by the sequence stopping. The value represented is determined by counting the number of symbols.
Pure unary is only able to represent one number per universe. To represent two different numbers you would need to mark the boundary between the unary sequences, which requires a second symbol.
The smallest input image file you can have is one byte, which could represent a single grayscale pixel with intensity 0 to 255. (Or could represent a single pixel rgb image with 3 bits of red information, three green, and two bits of blue.)
You cannot write out less than 8 bits to a file, so input bytes in the range 1 to 8 require at least a single byte of output, and input bytes of value 9 and up require more output bytes when represented in unary. Therefore you cannot compress anything using a unary encoder.
Exception: if your input image consisted of a single pixel with value 0, then the unary encoding of it would be the empty file, which is indeed smaller. This is the only case that you can compress using unary.
  5 Comments
Walter Roberson
Walter Roberson on 21 Nov 2021
V was a typing mistake, should have been v
filename = 'cameraman.tif';
k = imread(filename);
k = imresize(k, [8 8]); %very small!
UnaryImage = arrayfun(@(v)repmat('#',1,v), k, 'UniformOutput', false);
k(1:2,1:2)
ans = 2×2
160 170 164 180
UnaryImage{1,1}
ans = '################################################################################################################################################################'
size(UnaryImage{1,1})
ans = 1×2
1 160
size(UnaryImage{1,2})
ans = 1×2
1 170
size(UnaryImage{2,1})
ans = 1×2
1 164
size(UnaryImage{2,2})
ans = 1×2
1 180
You can see from this that each cell is the unary representation of one pixel, repeated occurances of a single symbol, with each pixel intensity being repeated by that many occurances of the symbol.
For Unary, the choice of symbol does not matter: only the number of symbols is important.
If you approach counting as an ancient practical task, such as making one mark for each sheep, then making a simple solid mark such as "|" or "/" is most consistent with established history. It is common for "1" to be used, in analogy for the counting numbers starting with 1, but that is not really historic: "1" is a numeral, something that has history and theory attached to it, but if you are taking the "it is just counting" approach then using "|" or "/" would be more to be expected.
If, on the other hand, you approach Unary as a numeric base, a matter of positional notation representation of an abstract concept, then in Western mathematics, it would be more correct to use "0" as the symbol for the digit used in Unary. Positional notation representation does not start each digit with 1: it starts it with 0 -- for positional notation, the numeric sequence is not 1, 2, 3 and so on: for positional notion, the numeric sequence is 0, 1, 2, 3, and so on. 0 is the kernel left behind when you have reduced down to a single possibility per position in a numeric positional notation system.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!