clc
close all
im=imread('cameraman.tif');
im = double(im)/255;
subplot(2,2,1)
imshow(im)
title('Original image');
img_dct=dct2(im);
img_pow=(img_dct).^2;
img_pow=img_pow(:);
[B,index]=sort(img_pow);%no zig-zag
B=flipud(B);
index=flipud(index);
compressed_dct=zeros(size(im));
coeff = 20000;% maybe change the value
for k=1:coeff
compressed_dct(index(k))=img_dct(index(k));
end
im_dct=idct2(compressed_dct);
im_bin=im2bw(im_dct);
subplot(2,2,2)
imshow(im_dct)
title('DCT Compress Image');
subplot(2,2,3)
imshow(im_bin)
title('DCT Binary Image');