QUANTIZATION OF 512x512 image WITHOUT USING THE USUAL QUANTIZATION MATRIX-IMAGE COMPRESSION

1 次查看(过去 30 天)
I have been battling with the method or the idea I can use in quantizing 512x512 image in whcih I am not allowed to use the usual quantization matrix table/predefined quantization matrix.
I need to select my frequency of interest during the qunatization process and turn the frequencies I am not intereted in to 0 especially the high frequency DCT coefficients.
Here is my code, I was told my code has no XY symetry and I do not now how to make it have XY symmetry as well.
tic
%% Reading the file
FileName = convertStringsToChars(FileName);
O_Image = imread(FileName);
% imshow(O_Image);
% Threshold=10^(-5); % The stopping condition to discard quantized DCT coeffiecint below 0.000001
Threshold=10^(-6);
%% Parameters
nBit = 8; % Number of bit 8 bits=1 byte
Index = strfind(FileName, '.'); % Search for files that contains dot
FileName_TXT = append(FileName(1:Index(end)-1), "_Quality", num2str(Quality), "_", Transform , ".txt");
FileName_JPEG = append(FileName(1:Index(end)), "jpg");
O_Size = size(O_Image);
% Factor encoded on 1 byte to determine the size of the original image
O_k1 = ceil(O_Size(1)/(2^(nBit)-1)); %This gives 3 bits which is 1 byte for the row and the column of the image dim
O_k2 = ceil(O_Size(2)/(2^(nBit)-1));
%% Transform
switch Transform
case 'DCT'
O_Value = dct2(O_Image); %DCT matrix of the original Image
S_Transform = '0';
% Make a quantization matrix Z
x=linspace(1,2,prod(O_Size)); % prod(O_Size)=512*512
y=reshape(x,length(O_Image),length(O_Image)); %reshape(x,[512,512]) returns the 512-by-512 matrix
% whose elements are taken columnwise from x
for i=1:length(O_Image)
for j=1:length(O_Image)
Z(i,j)=[length(O_Image)^y(i,j)]; % This matrix depends on the location of
end % each element , so every element is treated
end % differently .
% % % Make the Quantization
O_Value = O_Value./(Z*Fact); %Fact=x which is enables us to treat DCT coeefients differently
A=O_Value;
% figure; imshow(log(abs(O_Value)),[]); colormap parula; colorbar;
case 'DFT'
O_Value = [real((fftshift(fft(O_Image)))) imag((fftshift(fft(O_Image))))]/(2*prod(O_Size));
O_Size(2) = 2*O_Size(2);
O_k2 = 2*O_k2;
S_Transform = '1';
% figure; imshow(log(abs(fftshift((fft2(O_Image)))/prod(O_Size))),[]); colormap parula; colorbar;
end
Here is my main code
FileName = "I_lenagray.tif";
Transform = "DCT";
ReductionFactor = 1; % reduction rate either 1 or 2 for a good image compression and reconstrcution
Quality = 1:0.1:10; % Quality range
%%% Initialization
Flag = 1;
Q_Rate = zeros(1, size(Quality,2)); % A vector for rate of compression
Q_PSNR_OR = zeros(1, size(Quality,2)); % A vector for PSNR of original and reconstructed image
Q_PSNR_OJ = zeros(1, size(Quality,2));
Q_PSNR_RJ = zeros(1, size(Quality,2));
Q_MSE_OR = zeros(1, size(Quality,2));
Q_MSE_OJ = zeros(1, size(Quality,2));
Q_MSE_RJ = zeros(1, size(Quality,2));
%%% Loop
Pos_q = 0; %Indix for interation on the position depending on the quality selected
r=linspace(100000,100,91); % Remake a quality vector for matrix of quantization
for q = Quality
Pos_q = Pos_q + 1; %
Fact=r(Pos_q); %Choose the corresponding quality
% [R_Image, C_Rate, PSNR, MSE, ~,Threshold,A,B,Z] = MainFunction(FileName, Transform, q,Fact, ReductionFactor);
% Image_Saving(FileName, R_Image, q, 'tif')
[R_Image, C_Rate, PSNR, MSE, ~,Threshold] = MainFunction(FileName, Transform, q,Fact, ReductionFactor);
if Flag
Dim = size(R_Image); %Size of the reconstrcuted image as compared to 512x512of the original image
Q_ImageR = uint8(zeros(Dim(1), Dim(2), size(Quality,2))); % Conversion of zeros of the recontsructed image with respect to the size of the quality in uint8 format
Flag = 0;
I will really appreciate any idea I can use to achieve this image compression.
Is there anything I can do different to have this code achieve the aim ?
Thanks for your help.

回答(1 个)

prabhat kumar sharma
Hello Olusola
The quantization matrix Z defined by your current code lacks XY symmetry. Your matrix's quantization is not the same along the diagonal as required by XY symmetry because it was created by reshaping a linearly spaced vector into a square matrix.
You can use a matrix where the value grows as you move further from the top-left corner (which represents the DC coefficient and low-frequency components in the DCT domain) to produce an XY symmetric quantization matrix that focuses on low frequencies.
You can accomplish this by changing your code as follows:
1. Replace the current quantization matrix generation code with a symmetric matrix where the quantization factor increases with the distance from the top-left corner.
2. Apply the quantization matrix to the DCT coefficients.
3. Set the high-frequency DCT coefficients to zero based on the threshold you're interested in.
switch Transform
case 'DCT'
O_Value = dct2(O_Image); % DCT matrix of the original Image
S_Transform = '0';
% Create a symmetric quantization matrix Z with increasing values from the top-left corner
[X, Y] = meshgrid(1:length(O_Image), 1:length(O_Image));
Z = sqrt((X-1).^2 + (Y-1).^2) + 1; % Add 1 to avoid division by zero
% Apply the quantization matrix to the DCT coefficients
O_Value = O_Value ./ (Z * Fact);
% Threshold high frequencies
O_Value(abs(O_Value) < Threshold) = 0;
% Your Further code...
end
In the modified code: A symmetric quantization matrix Z is created using meshgrid. It's based on Euclidean distance from the top-left corner, with 1 added to avoid division by zero. The DCT coefficients are divided by Z scaled by Fact, applying quantization. DCT coefficients below the Threshold are zeroed out, removing high-frequency details. This method retains low-frequency information and adjusts compression via Fact and Threshold.
I hope it helps you with your problem!

Community Treasure Hunt

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

Start Hunting!

Translated by