Arithmetic coding for image compression
显示 更早的评论
I am working on the topic of image compression I found built in matlab function for arithmetic coding which is arithenco I want to use it in image compression can any give me example on how to this function for image compression.
采纳的回答
reshape() the image into a vector. Apply arithmetic encoding.
11 个评论
I write the following code to convert image to vector
c=dicomread('brain.dcm');
c=im2uint8(c);
imgV = reshape(c.', 1, []); % Convert to vector
counts = [99 1];
arithenco(imgV,counts)
but it gives me
The symbol sequence parameter must be a vector of positive finite integers
arithenco(imgV+1,counts)
Note: your counts vector should be the same length as the number of unique values in imgV
It give me the following error
The symbol sequence parameter can take values only between 1 and the length of the symbol counts parameter.
Reread what I said before,
"Note: your counts vector should be the same length as the number of unique values in imgV"
Is there any technique in matlab to make counts vector equal imgV vector
You have two choices:
1) Use a fixed vector of counts that matches some probability model you have determined. This has the advantage of not needing to store / transmit the dictionary for decoding; or
2) Use the actual counts. This has the advantage of providing the best compression, but does require storing/transmitting the list of symbols used (because, if I recall correctly, it is not permitted to use a count of 0.)
[unique_symbols, ~, symbol_number] = unique(imgV);
symbol_counts = accumarray(symbol_number, 1);
coded_result = arithenco(symbol_number, symbol_counts);
and you need to store/transmit unique_symbols to decode.
Thank you but finally I want to save the coded_result I saved it as .mat and the problem is that the output file size increase Is there any other method to save the output file
The size of a .mat can be larger or smaller than the size of the data being saved. You should save into .mat files for ease of recalling data exactly was it was saved; you should not save into .mat files expecting them to be any particular size.
If you are looking at the file size from a data compression point of view, you should be using something like,
fid = fopen('compressed_data.dat', 'w');
fwrite(fid, uint8(length(unique_symbols)) );
fwrite(fid, uint8(unique_symbols));
coded_as_uint8 = uint8(bin2dec(char(buffer(coded_result,8) + '0').'));
unused_bits_of_last_byte = uint8(length(coded_as_uint8) * 8 - length(coded_result) );
fwrite(fid, unused_bits_of_last_byte);
fwrite(fid, coded_as_uint8);
fclose(fid);
This creates a file with the following structure:
- a single byte that gives the length of the symbol table
- a vector that length of bytes that are the symbols being encoded
- a single byte that gives the number of unused bits in the last byte of the data that follows
- a vector of the arithenco results converted from a vector of double(0) and double(1) into a stream of bytes by packing 8 bits per byte
The "number of unused bits in the last byte" is there because the conversion of a stream of bits into bytes has a problem if the number of bits being converted is not an exact multiple of 8. The buffer() call (from the Signal Processing Toolbox) puts in 0s for the extra bits and when decoding you need a way to know when to stop decoding and ignore the final few 0s.
There are other approaches that can be used instead of coding the number of unused bits, and some of the other approaches can be pretty useful in some situations, but they are more complicated to decode.
Thank you it is great
can you tell how to save this as an image
bytes = uint8([]);
bytes = [bytes, uint8(length(unique_symbols)) ];
bytes = [bytes, uint8(unique_symbols)];
coded_as_uint8 = uint8(bin2dec(char(buffer(coded_result,8) + '0').'));
unused_bits_of_last_byte = uint8(length(coded_as_uint8) * 8 - length(coded_result) );
bytes = [bytes, unused_bits_of_last_byte];
bytes = [bytes, coded_as_uint8];
imwrite(bytes, 'useless_image.tif');
The above will fail if the encoding requires more than 4 gigabytes. You must use .tif to be able to handle that size; if you can be sure that the encoding will be no more than 2 gigabytes (minus one byte) then you can use .png .
.tif and .png files can be used to store arbitrary bytes without loss. (.bmp too, but .bmp has a limit of 30000 bytes for this purpose.)
You will not get any useful output if you ask to display an image created in this way: if the arithematic encoding worked properly, then the output will look pretty much random. Just because you can create an image file does not mean that the image file is understandable to humans.
People keep expecting that compressed images look like... I don't know. A distorted but partly recognizable version of the original, I guess? A smaller and possibly recolored version of the original?
But compression theory says that you can continue to compress until one of two things happens:
- the compressed values become statistically indistinguishable from random; or
- the overhead needed to describe the data transformations to apply more compression starts to take more space than just listing the compressed bytes as they are.
If you could still make out any resemblence between the original image and the data representing the compressed image, then you did not do a good enough job of compression !!
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Denoising and Compression 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
