When I try combining channels for RGB compressed image, the Image coloring if "off" and somewhat "yellow-ish"

2 views (last 30 days)
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;
end
end
%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;
end
end
%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');
  2 Comments
Stephen23
Stephen23 on 28 Apr 2017
@Kayla Kloster: please do not keep adding the same question repeatedly. You can simply edit the question instead.

Sign in to comment.

Accepted Answer

Jan
Jan on 28 Apr 2017
Edited: Jan on 28 Apr 2017
Your code contain 3 almost equal sections. Such redundancies are prone to bugs, because it is confusing to edit them. Prefer to use a loop over the 3 channels and call the same code section.
This confusing repeating of code sections decrease the readability and typos like this are almost invisible:
%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);
...
%Apply DCT
YG=DCT*B*DCT';
% ^
You do not want "B" here, but BG.
The names for the different channels are not smart: B for red, BG for green, BB for blue.
  1 Comment
Kayla Kloster
Kayla Kloster on 28 Apr 2017
Thanks so so much!! That was the issue. Yes I have noticed it have been extremely difficult to debug, so I will work on using a for loop instead of repeated sections.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!