JPEG encoder and decoder

8 views (last 30 days)
Divyashree
Divyashree on 4 Mar 2025
Answered: Hitesh on 8 Apr 2025
I need to understand the flow of jpeg compression and decompression. so, i have taken 8*8 block of pixel values and sending through all inbuilt functions in matlab. please confirm is it correct way to use the inbuilt functions or not. If any mistakes happed in my program please give me the suggesions.
clc; clear; close all;
% Example Usage:
U = double([
156 159 158 155 158 156 159 158;
160 154 157 158 157 159 158 158;
156 159 158 155 158 156 159 158;
160 154 157 158 157 159 158 158;
156 153 155 159 159 155 156 155;
155 155 155 157 156 159 152 158;
156 153 157 156 153 155 154 155;
159 159 156 158 156 159 157 161
]);
Q = [ 16 11 10 16 24 40 51 61;
12 12 14 19 26 58 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];
x = U - 128; % level shift input
disp(x);
[xm, xn] = size(x); % retrieve size of input
Forward_dct = dct2(x);
disp('dct output');
disp(Forward_dct);
quantizeddct=round(Forward_dct./Q);
disp(' after quantization:');
disp(quantizeddct);
zigzag_vector = zigzag(quantizeddct);
disp(' after zigzag');
disp(zigzag_vector);
% Step 1: Get unique symbols and probabilities
symbols = unique(zigzag_vector);
counts = histc(zigzag_vector(:), symbols); % Count occurrences of each symbol
probabilities = counts./64; % Compute probabilities
disp('Unique Symbols:');
disp(symbols);
disp('Symbol Probabilities:');
disp(probabilities);
% Step 2: Create Huffman Dictionary
dict = huffmandict(symbols, probabilities);
% Step 3: Encode Zigzag Data
encoded_data = huffmanenco(zigzag_vector, dict);
disp('Huffman Encoded Data:');
disp(encoded_data);
decoded_data = huffmandeco(encoded_data, dict);
disp('huffman decoded:');
disp(decoded_data);
decoderzigzag_vector = izigzag(decoded_data,8,8);
disp('inversezigzag:');
disp(decoderzigzag_vector);
% Step 1: Perform element-wise multiplication
inv_quantized = decoderzigzag_vector .* Q;
% Step 2: Round the values (optional, depends on implementation)
inv_quantized = round(inv_quantized);
% Display the dequantized matrix
disp('Dequantized DCT coefficients:');
disp(inv_quantized);
% Display the IDCT matrix
inverse_dct = idct2(inv_quantized);
disp('2D IDCT output');
disp(inverse_dct);
inverse_dct = round(inverse_dct + 128);
disp(inverse_dct);
  2 Comments
DGM
DGM on 4 Mar 2025
Frankly, I'm just sniping, but there are a bunch of threads about student JPG encoding/decoding efforts, as well as some submissions on the File Exchange. Without the need, I haven't bothered to bang one out myself, so I really can't comment further.
Divyashree
Divyashree on 5 Mar 2025

I have my own program for the DCT and IDCT parts and for the remaining parts( zigzag, huffman encoding, Huffman decoding and inverse zigzag) i will be using in inbuilt functions. To verify my own program output I have used all inbuilt function. Because my research related to dct and idct only. To verify my own program step by step, I need confirmation about my above program which uses inbuilt functions for all steps.

Sign in to comment.

Answers (1)

Hitesh
Hitesh on 8 Apr 2025
Hi Divyashree,
The quantization matrix 'Q' plays a crucial role in determining the quality of the compressed image. A less aggressive quantization matrix will preserve more detail, reducing MSE but potentially increasing the file size. With your quantization matrix the MSE comes out to be 4.5.
I have revised the quantization matrix 'Q' and now the MSE comes out to be 3.5. Kindly refer to the following revised code and mentioned comments for better understanding.
% Main Script
U = double([
156 159 158 155 158 156 159 158;
160 154 157 158 157 159 158 158;
156 159 158 155 158 156 159 158;
160 154 157 158 157 159 158 158;
156 153 155 159 159 155 156 155;
155 155 155 157 156 159 152 158;
156 153 157 156 153 155 154 155;
159 159 156 158 156 159 157 161
]);
% Less aggressive quantization matrix
Q = [8 6 5 8 12 20 26 31;
6 6 7 9 13 29 30 28;
7 7 8 12 20 29 35 28;
7 9 11 15 26 44 40 31;
9 11 19 28 34 55 52 39;
12 18 28 32 41 52 57 46;
25 32 39 44 52 61 60 51;
36 46 48 49 56 50 52 50];
x = U - 128;
disp('Original Image:');
disp(U);
% Forward DCT
Forward_dct = dct2(x);
quantizeddct = round(Forward_dct ./ Q);
% Zigzag, Huffman Encode, Decode, and Inverse Zigzag
zigzag_vector = zigzag(quantizeddct);
symbols = unique(zigzag_vector);
counts = histc(zigzag_vector(:), symbols);
probabilities = counts ./ 64;
dict = huffmandict(symbols, probabilities);
encoded_data = huffmanenco(zigzag_vector, dict);
decoded_data = huffmandeco(encoded_data, dict);
decoderzigzag_vector = izigzag(decoded_data, 8, 8);
% Dequantization and Inverse DCT
inv_quantized = decoderzigzag_vector .* Q;
inverse_dct = idct2(inv_quantized);
reconstructed_image = round(inverse_dct + 128);
disp('Reconstructed Image:');
disp(reconstructed_image);
% Calculate MSE
mse = mean((U(:) - reconstructed_image(:)).^2);
disp(['Mean Squared Error: ', num2str(mse)]);
% Visual Comparison
figure;
subplot(1,2,1);
imshow(uint8(U));
title('Original Image');
subplot(1,2,2);
imshow(uint8(reconstructed_image));
title('Reconstructed Image');
You can also use the "JPEG Encoder Decoder" file exchange. Kindly refer to the following page:

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!