% You need to change the following two lines to indicate where you put the Utility Toolbox and Machine Learning Toolbox addpath /users/jang/matlab/toolbox/utility addpath /users/jang/matlab/toolbox/machineLearning close all; % Close all windows clear all; % Cear all variables in the workspace inImage = imread('vivian_orig.png'); % Read image file [m, n]=size(inImage); % For the size of the input image blockSide=8; % For computing compression ratio centerCount=64; % No. of centers/clusters outImage=myImageCompress(inImage, centerCount); % Perform image compression using k-means clustering absDiff=abs(double(inImage)-outImage); % You need to use double() to convert the image data back to double precision distortion=sum(absDiff(:).^2); % Total distortion fprintf('distortion = %f\n', distortion); % Print total distortion fprintf('Average deviation = %f\n', mean(absDiff(:))); % Print average deviation for each pixel component %% Plot the images figure; subplot(211); image(inImage); axis image; title('Original image'); subplot(212); image(uint8(outImage)); axis image; % Display the output image. You need to use uint8() to convert double precision back to unsigned 8-bit integers compressionRatio=8*3*m*n/(log2(centerCount)*(m/blockSide)*(n/blockSide)+8*centerCount*3*blockSide*blockSide); % Compute the compression ratio titleStr=sprintf('Codebook size=%g, block=%dx%d, compression ratio=%g', centerCount, blockSide, blockSide, compressionRatio); % Note that codebook size is equal to the number of centers/clusters title(titleStr);