Ho we can Implement Unary compression method and create Unary encoder decoder in matlab?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
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
mohammad nemat
on 20 Nov 2021
Thanks a lot. First, I want to implement the Unary method in Matlab for a grayscale image, Second I want to find geometric distribution given the geometric necessity of data distribution for better performance of the Unary method, a solution to simulate the distribution of images to geometrically reversible distribution.
For the first step could you please help me?
How I can convert a grayscale image to unary format for each pixel?
My main problem is how I can save Unary codeword and after encode unary codeword.
UnaryImage = arrayfun(@(v)repmat('0',1,V), YourImage, 'UniformOutput', false);
The rest such as geometric distribution looks like nonsense in the context of unary representation.
mohammad nemat
on 21 Nov 2021
What is the V parameter here?
How do this work for create Unary codeword for each pixel?
k=imread('./test.png');
V=5;
UnaryImage = arrayfun(@(v)repmat('0',1,V), k, 'UniformOutput', false);
I run it but I got UnaryImage matrix with whole of zero value each cells 00000 value
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.
mohammad nemat
on 25 Nov 2021
Ok,thanks
More Answers (0)
Categories
Find more on Denoising and Compression in Help Center and File Exchange
See Also
on 20 Nov 2021
on 25 Nov 2021
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)