% downsample4.m dat = imread('test_image.tif'); dat2 = dat(310:327,334:351,:); clear dat; [h,w,dummy] = size(dat2); dat2 = double(dat2); % reduce the data. Average 2x2 blocks for lx=1:w/2, for ly=1:h/2, x = (lx-1)*2+1; y = (ly-1)*2+1; dat_reduced(ly,lx,1) = (dat2(y,x,1) + dat2(y+1,x,1) + dat2(y,x+1,1) + dat2(y+1,x+1,1))/4; dat_reduced(ly,lx,2) = (dat2(y,x,2) + dat2(y+1,x,2) + dat2(y,x+1,2) + dat2(y+1,x+1,2))/4; dat_reduced(ly,lx,3) = (dat2(y,x,3) + dat2(y+1,x,3) + dat2(y,x+1,3) + dat2(y+1,x+1,3))/4; end end % use interpolation methods to reconstruct an image % of the original size dat_reconstructed = zeros(h-1,w-1,3); dat_reconstructed(:,:,1) = interp2([1:2:h],[1:2:w]',dat_reduced(:,:,1), [1:h-1],[1:w-1]','linear'); dat_reconstructed(:,:,2) = interp2([1:2:h],[1:2:w]',dat_reduced(:,:,2), [1:h-1],[1:w-1]','linear'); dat_reconstructed(:,:,3) = interp2([1:2:h],[1:2:w]',dat_reduced(:,:,3), [1:h-1],[1:w-1]','linear'); dat_reconstructed = uint8(dat_reconstructed); figure(); image(dat_reconstructed); title('linear interpolation'); dat_reconstructed(:,:,1) = interp2([1:2:h],[1:2:w]',dat_reduced(:,:,1), [1:h-1],[1:w-1]','spline'); dat_reconstructed(:,:,2) = interp2([1:2:h],[1:2:w]',dat_reduced(:,:,2), [1:h-1],[1:w-1]','spline'); dat_reconstructed(:,:,3) = interp2([1:2:h],[1:2:w]',dat_reduced(:,:,3), [1:h-1],[1:w-1]','spline'); dat_reconstructed = uint8(dat_reconstructed); figure(); image(dat_reconstructed); title('spline interpolation');