Why is my image decoder not producing the image i encoded?
7 views (last 30 days)
Show older comments
I want to process an image following the JPEG standard, encode it and then reverse the steps to get the original image back. However, it seems that my decoder is far from the original image, putting out a pink and white image that resembles the original slightly.
function [out, dict] = imageEncoder(image)
YCbCr = double(rgb2ycbcr(image));
encoded = zeros(size(YCbCr));
quantizationTable = [16 11 10 16 24 40 51 61 ;
12 12 14 19 26 28 60 55 ;
14 13 16 24 40 57 69 56 ;
14 17 22 29 51 87 80 62 ;
18 22 37 56 68 109 103 77 ;
24 35 55 64 81 104 113 92 ;
49 64 78 87 103 121 120 101;
72 92 95 98 112 100 103 99];
for channel = 1:3
for height = 1 : 8 : size(YCbCr, 1) - 7
for width = 1 : 8 : size(YCbCr, 1) - 7
% divide image in 8x8 blocks, for each channel individually
pixelBlock = YCbCr(height : height + 7, width : width + 7, channel);
% center pixel block values around zero by subtracting 128
pixelBlock = pixelBlock - 128;
% perform DCT type 2 for each block
dctPixelBlock = dct2(pixelBlock);
% block quantization
quantizedBlock = round(dctPixelBlock./quantizationTable);
encoded(height : height + 7, width : width + 7, channel) = quantizedBlock;
end
end
end
encoded2 = encoded(:);
symbols = transpose(unique(encoded2));
probabilities = histcounts(encoded2,length(symbols))/length(encoded2);
dict = huffmandict(symbols, probabilities);
out = huffmanenco(encoded2, dict);
end
For decoding the image, i employ the reverse algorithm:
function reconstructedImage = imageDecoder(encoded, dict)
decoded = huffmandeco(encoded, dict);
decoded = reshape(decoded, [128 128 3]);
reconstructedImage = zeros(size(decoded));
quantizationTable = [16 11 10 16 24 40 51 61 ;
12 12 14 19 26 28 60 55 ;
14 13 16 24 40 57 69 56 ;
14 17 22 29 51 87 80 62 ;
18 22 37 56 68 109 103 77 ;
24 35 55 64 81 104 113 92 ;
49 64 78 87 103 121 120 101;
72 92 95 98 112 100 103 99];
for channel = 1:3
for height = 1 : 8 : size(decoded, 1) - 7
for width = 1 : 8 : size(decoded, 1) - 7
dctValues = decoded(height : height + 7, width : width + 7, channel).*quantizationTable;
pixelBlock = idct2(dctValues) + 128;
reconstructedImage(height : height + 7, width : width + 7, channel) = pixelBlock;
end
end
end
reconstructedImage = ycbcr2rgb(reconstructedImage);
end
I get an image with all ones when using ycbcr2rgb, but even i f i comment that line out and leave reconstructedImage as is, it still is quite different from YCbCr from the first function. I expect some losses due to the rounding of the quantized block, but the variation seems too high. Also, how can i properly transform the reconstructedImage from YCbCr color-space to RGB such that i can see the restored image?
Thanks
0 Comments
Answers (0)
See Also
Categories
Find more on Source Coding 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!