4) Take the cameraman image (standard test image of MATLAB). a) Determine the sa
ID: 3731969 • Letter: 4
Question
4) Take the cameraman image (standard test image of MATLAB). a) Determine the sample entropy of this image b) Get a Huffman coder (from internet). Apply to cameraman image, and determine the compression ratio. c) Now, apply the ZIP operation, find the compression ratio. d) Finally, Find the JPEG-LS (LOCO) coder from the internet. This is a lossless coder tuned for images. Apply it to the cameraman image, determine the compression ratio. Comment on your observations. Why are the first three methods inefficient? e)Explanation / Answer
Try the code below: (download cameraman.jpg (512*512 grayscale))
function camera
img = imread('cameraman.jpg');
disp('Part (a)');
img_entropy = entropy(img)
disp('Part (b)');
[M N]=size(img);
A = img(:);
counter = [0:1:255]; % Distinct data symbols appearing in sig
tot=sum(counter);
for i=1:1:size((counter)');
p(i)=counter(i)/tot;
end
[dict,average_len]=huffmandict(counter,p); % building the Huffman dictionary
comp= huffmanenco(A,dict); %encode your original image with the dictionary built
huffman_compression_ratio= (M*N*8)/length(comp) %computing the compression ratio
disp('Part (c)');
zipper = zip('compress','cameraman.jpg');
initial = dir('cameraman.jpg');
size_initial = initial.bytes;
final = dir('compress.zip');
size_final = final.bytes;
zip_compression_ratio = size_initial/size_final
disp('Part (d)');
% perform DCT in 2 dimension over blocks of 8x8 in the given picture
dct_8x8_image_of_128x128 = image_8x8_block_dct( img );
jpeg_compression_ratio = (M*N)/(size(dct_8x8_image_of_128x128,1)*size(dct_8x8_image_of_128x128,2))
function out = pdip_dct2( in )
% get input matrix size
N = size(in,1);
% build the matrix
n = 0:N-1;
for k = 0:N-1
if (k>0)
C(k+1,n+1) = cos(pi*(2*n+1)*k/2/N)/sqrt(N)*sqrt(2);
else
C(k+1,n+1) = cos(pi*(2*n+1)*k/2/N)/sqrt(N);
end
end
in = im2double(in);
out = C.*in.*(C');
function transform_image = image_8x8_block_dct( input_image )
transform_image = zeros( size( input_image,1 ),size( input_image,2 ) );
for m = 0:15
for n = 0:15
transform_image( m*8+[1:8],n*8+[1:8] ) = ...
pdip_dct2( input_image( m*8+[1:8],n*8+[1:8] ) );
end
end
Note: Huffman encoder usually works better when the frequency of some greyscale values are high.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.