function [out_image] = entropy_image(in_image) % entropy_image replaces each pixel % with the (estimated) number of bits % necessary to encode that pixel. % Usage: % cameraman = imread('cameraman.tif'); % c = entropy_image(cameraman); % figure; colormap(gray(256)); % c2 = 255-(c-1)*(255/9); % c2 = imclip( c, 10, 2, 256 ); % image(double(c2)+1); drawnow; % % translate c=1 bit to white (255), % % translate c=10 bits or more to black (0). % % row_diff = ... % [ double(cameraman(:,1)) diff(double(cameraman'))' ]; % r = entropy_image( row_diff ); % figure; colormap(gray(256)); % r2 = 255-(r-1)*(255/9); % image(double(r2)+1) % dd = entropy_image( diff2( cameraman ) ); % figure; colormap(gray(256)); % d3 = 255-(dd-1)*(255/9); % image(double(d3)+1) % Change log: % 1999-07-08:DAV: David Cary started. % See also ENTROPY, ENTROPY_GRAPH. if( isa( in_image, 'uint8' ) ) x = in_image; else, minimum = min(in_image(:)); x = round(in_image - minimum); end; pixels = prod(size(x)); histogram = histo(x(:)); % find all values that occurred at least once occurred = find(histogram); % For each value that actually occurred, % estimate how many bits it takes to % encode that value. P = histogram(occurred) ./ pixels; bits = inf*ones(size(histogram)); bits(occurred)=-log2(P); %bits(occured)=ceil( -log2(P) ); % bits(1) indicates how many bits each pixel with value "0" requires, % were one to compress it with entropy coding. % This is generally a good estimate of Huffman compression, % except when bits(1) is very small. % (If there were negative values in the original image, % bits(1) indicates how many bits pixels with the most negative value requires). % Create the output image via look-up table out_image = zeros(size(x)); out_image(:) = bits(1+double(x)); % We're done ! % Consistency checking; % ASSERT( never looked up pixels that we thought didn't exist ) if( isequal( [], find( inf == out_image ) ) ), else, error( 'Sorry, internal programming error in entropy_map.' ) end; % ASSERT( It's impossible for a pixel to be represented in zero bits, unless ... % the entire image is the same color. ) if( isequal( [], find( 0 == out_image ) ) ), else, % All pixels are the same color as the first pixel, right ? if( isequal( [], find( x(1) ~= x ) ) ), disp(' the entire image is the same color ') else, error( 'Sorry, internal math error in entropy_map.' ) end; end; % end entropy_image.m