load hw7image; %break image into 2x2 blocks a1 = ho(1:2:end,1:2:end); a2 = ho(1:2:end,2:2:end); a3 = ho(2:2:end,1:2:end); a4 = ho(2:2:end,2:2:end); % put in array of vectors newho = [a1(:) a2(:) a3(:) a4(:)]; np = size(newho,1); % randomly select 1000 vectors from newho as training data train = zeros([1000 4]); % place holder % run kmeans % L = ??? rinit = zeros([L 4]); options = zeros([1 14]); options(2) = 1; options(3) = 1; options(5) = 1; % pick random point in data to initialize starting points options(14) = 1000; % max number of iterations rfinal = kmeans(rinit,train,options); %final reconstruction locations % now quantize - "code" will be the transmitted values code = ones([np 1]); % place holder % reconstruct image rnewho = rfinal(code,:); rho = zeros(size(ho)); rho(1:2:end,1:2:end) = reshape(rnewho(:,1),size(a1)); rho(1:2:end,2:2:end) = reshape(rnewho(:,2),size(a1)); rho(2:2:end,1:2:end) = reshape(rnewho(:,3),size(a1)); rho(2:2:end,2:2:end) = reshape(rnewho(:,4),size(a1)); % calculate average distortion