I am trying to use DCT image compression on an RGB image by applying DCT to each separate channel and then recombining the channels in my final image. However, when I run my code, the output image is not fully colored, and I don't understand why. This is what the image looks like when using Matlab's DCT functions
This is what the image looks like after implementing my DCT image compression:
The following is my code:
clear all; close all;X_int = imread('wombat.jpg');redChannel = X_int(:, :, 1);greenChannel = X_int(:, :, 2);blueChannel = X_int(:, :, 3);N=8;Z=256; %DCT FOR RED CHANNEL
B=zeros(256,256); for r=1:N:256 for c=1:N:256 B(r:r+N-1,c:c+N-1)=redChannel(r:r+N-1,c:c+N-1); [m n] = size(B); %CREATE DCT
DCT=zeros(n); for i=1:m for j=1:n if i==1 DCT(i,j) = ((1/n)^(1/2)); else DCT(i,j) = ((2/n)^(1/2))*cos((pi*(2*j-1)*(i-1))/(2*n)); end end end %APPLY DCT
Y=DCT*B*DCT'; %Zero out sub-antidiagonal entries
p=.1; for i=1:n for j=1:n if i+j>p*2*n Y(i,j) = 0; end end end ImageR = DCT'*Y*DCT; end end%DCT FOR GREEN CHANNEL
BG=zeros(256,256);for r=1:N:256 for c=1:N:256 BG(r:r+N-1,c:c+N-1)=greenChannel(r:r+N-1,c:c+N-1); [s l] = size(BG); %CREATE DCT DCT=zeros(l); for i=1:s for j=1:l if i==1 DCT(i,j) = ((1/l)^(1/2)); else DCT(i,j) = ((2/l)^(1/2))*cos((pi*(2*j-1)*(i-1))/(2*l)); end end end %Apply DCT
YG=DCT*B*DCT'; %Zero out sub-antidiagonal entries p=.1; for i=1:n for j=1:n if i+j>p*2*n YG(i,j) = 0; end end end ImageG = DCT'*YG*DCT; endend%DCT FOR BLUE CHANNEL
BB = zeros(256,256); for r=1:N:256 for c=1:N:256 BB(r:r+N-1,c:c+N-1)=blueChannel(r:r+N-1,c:c+N-1); [q w] = size(BB); %Function to return DCT matrix of any size
DCT=zeros(w); for i=1:q for j=1:w if i==1 DCT(i,j) = ((1/w)^(1/2)); else DCT(i,j) = ((2/w)^(1/2))*cos((pi*(2*j-1)*(i-1))/(2*w)); end end end %Apply DCT YB=DCT*BB*DCT'; %Zero out sub-antidiagonal entries p=.1; for i=1:n for j=1:n if i+j>p*2*n YB(i,j) = 0; end end end ImageB = DCT'*YB*DCT; endend %CONCATENATE THE 3 RGB CHANNELS
red = uint8(ImageR); green = uint8(ImageG); blue = uint8(ImageB); Isvd = cat(3,red,green,blue); figure imshow(Isvd); imwrite(Isvd,'my_output_image.jpg');
Best Answer