Quantization on an RGB Image
Show older comments
I'm having issues with this code on RGB images. I'm trying to use 2D-DCT and quantization to compress an image. My code works on gray images and works if I just transpose the RGB image from one matrix to another. The image comes out dark, and I am at a loss of what the problem is. See examples and code below
Original

Processed

Code
clc
close all
image = imread('picture.jpg');
p=1;
%check if matrix is divisible by 8
if (rem(size(image,1),8) ~= 0)
disp("width not divisible by 8 croping image");
imagew = size(image,1) - rem(size(image,1),8);
image = image(1:imagew,1:size(image,2),1:size(image,3));
end
if (rem(size(image,2),8) ~= 0)
disp("heigth not divisible by 8 croping image");
imageh = size(image,2) - rem(size(image,2),8);
image = image(1:size(image,1),1:imageh,1:size(image,3));
end
%gray scale
% r=image(:,:,1);
% g=image(:,:,2);
% b=image(:,:,3);
% original = 0.2126*r+0.7152*g+0.0722*b;
% original = uint8(original);
%color
original = uint8(image);
figure(1)
imshow(original, []);
rows = (size(original,1));
columns = (size(original,2));
bRow = ceil(size(original,1)/(size(original,1)/8));
bCol = ceil(size(original,2)/(size(original,2)/8));
Q = p*8./hilb(8);
processed = uint8(zeros(rows,columns,(size(original,3))));
for z = 1:size(original,3)
for y = 0:(size(original,1)/8) - 1
for x = 0:(size(original,2)/8) - 1
x0 = x*bCol+1;
y0 = y*bRow+1;
x1 = min(x0+bCol-1, columns);
y1 = min(y0+bRow-1, rows);
xb = original(y0:y1,x0:x1,z);
dct2xb = round(dct2(xb));
Xd = double(xb);
Xc = Xd - 128;
Yq = round(dct2xb ./ Q);
idct2xb = idct2(Yq);
processed(y0:y1,x0:x1,z) = uint8(idct2xb);
% processed(y0:y1,x0:x1,z) = uint8(xb);
end
end
end
figure(2)
imshow(processed, []);
Full disclosure, yes this is homework.
Answers (0)
Categories
Find more on Denoising and Compression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!