Skip to content

Commit 16f56d5

Browse files
initial commit
0 parents  commit 16f56d5

10 files changed

+205
-0
lines changed

compression_example.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
% Example Script for Fractal DCT Compression
2+
clear all;
3+
4+
% Load and preprocess the image
5+
inputImage = imread('bridge.pgm');
6+
grayscaleImage = double(inputImage);
7+
imshow(grayscaleImage / 256);
8+
9+
% Perform compression
10+
fractal_dct_compress;

dct_transform.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
% Apply Discrete Cosine Transform
2+
function dctBlock = dct_transform(imageBlock)
3+
dctBlock = dct2(imageBlock - 128);
4+
end

decompression_example.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
% Example Script for Fractal DCT Decompression
2+
clear all;
3+
4+
% Load compressed data
5+
load('compressedData.mat');
6+
7+
% Perform decompression
8+
fractal_dct_decompress;

distortion_calculation.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
% Distortion Calculation between Range and Domain Blocks
2+
distortion = norm(domainBlock - rangeBlock);

fractal_dct_compress.m

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
% Fractal Image Compression using DCT
2+
clear all;
3+
4+
% Define normalization matrices for quantization
5+
normMat = ones(16) * 10^4;
6+
normMat(1:8, 1:8) = [
7+
16 11 10 16 24 40 51 61
8+
12 12 14 19 26 58 60 55
9+
14 13 16 24 40 57 69 56
10+
14 17 22 29 51 87 80 62
11+
18 22 37 56 68 109 103 77
12+
24 35 55 64 81 104 113 92
13+
49 64 78 87 103 121 120 101
14+
72 92 95 98 112 100 103 99
15+
];
16+
17+
% Initialize variables for the compression process
18+
encodingResults = zeros(1024, 10);
19+
qualityLevels = [.1 .25 .4 .5 .6 .7 .8 .95];
20+
currentIteration = 1;
21+
errorThreshold = 7;
22+
quantizationFactor = 30;
23+
24+
% Define image and block sizes
25+
rangeBlockSize = 16;
26+
domainBlockSize = 32;
27+
28+
% Read and display the input image
29+
inputImage = imread('bridge.pgm');
30+
grayscaleImage = double(inputImage);
31+
imshow(grayscaleImage / 256);
32+
33+
% Get image dimensions
34+
imageSize = size(grayscaleImage);
35+
numBlocks = imageSize(1) / rangeBlockSize;
36+
37+
% Divide the image into blocks and apply DCT
38+
blockIndex = 0;
39+
for row = 1:numBlocks
40+
for col = 1:numBlocks
41+
blockIndex = blockIndex + 1;
42+
currentBlock = grayscaleImage(rangeBlockSize*(row-1)+1:rangeBlockSize*row, rangeBlockSize*(col-1)+1:rangeBlockSize*col);
43+
dctBlock = dct2(currentBlock - 128);
44+
normalizedBlock = dctBlock ./ normMat;
45+
blockStorage{blockIndex} = normalizedBlock;
46+
end
47+
end
48+
49+
% Initialize variables for the main loop
50+
totalBlocks = blockIndex;
51+
iterations = 1;
52+
currentError = errorThreshold;
53+
54+
% Main compression loop
55+
while (iterations)
56+
matchedBlocks = 0;
57+
for i = 1:totalBlocks
58+
domainBlock = blockStorage{i};
59+
domainBlock(1, 1) = 0;
60+
diff = 1000;
61+
62+
% Perform distortion calculation
63+
distortion_calculation; % Custom function
64+
65+
if (rangeBlockSize == 2)
66+
encodingResults(i, 1) = distortion;
67+
encodingResults(i, 2) = norm(domainBlock - rangeBlock);
68+
encodingResults(i, 3) = contrast;
69+
encodingResults(i, 4) = round(originalValue);
70+
iterations = 0;
71+
else
72+
if distortion < currentError
73+
matchedBlocks = matchedBlocks + 1;
74+
encodingResults(i, 3) = contrast;
75+
encodingResults(i, 4) = round(originalValue);
76+
else
77+
unmatchedBlocks(1, i-matchedBlocks) = i;
78+
encodingResults(i, 6) = 0;
79+
end
80+
end
81+
end
82+
83+
compressionResults{iterations} = encodingResults;
84+
encodingResults = zeros(5000, 6);
85+
86+
if (iterations ~= 0)
87+
partition_no_search; % Custom function for further partitioning
88+
end
89+
end
90+
91+
% Calculate and display compression metrics
92+
totalTime = etime(clock, timeStart) / 60;
93+
disp(['Total compression time: ', num2str(totalTime), ' minutes']);
94+
95+
% Calculate memory usage and compression ratio
96+
memoryUsage = (sum(encodingResults(:) ~= 0) * 12 + sum(encodingResults(:) == 0) * 13) / 8;
97+
memoryUsageKB = memoryUsage / 1024;
98+
bitsPerPixel = (memoryUsageKB / 256) * 8;
99+
compressionRatio = 256 / memoryUsageKB;
100+
disp(['Bits per pixel: ', num2str(bitsPerPixel)]);
101+
disp(['Compression ratio: ', num2str(compressionRatio)]);
102+
103+
% Decode the compressed image
104+
fractal_dct_decompress; % Custom function for decoding
105+
106+
% Calculate and display PSNR
107+
psnrValue = calculatePSNR(grayscaleImage, decodedImage);
108+
disp(['PSNR: ', num2str(psnrValue), ' dB']);
109+
110+
% Custom function to calculate PSNR
111+
function psnrValue = calculatePSNR(originalImage, compressedImage)
112+
errorSum = 0;
113+
for i = 1:numel(originalImage)
114+
errorSum = errorSum + (originalImage(i) - compressedImage(i))^2;
115+
end
116+
mse = errorSum / numel(originalImage);
117+
psnrValue = 10 * log10(255^2 / mse);
118+
end

fractal_dct_decompress.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
% Fractal Image Decompression using DCT
2+
3+
% Initialize variables for decompression
4+
decodedImage = zeros(size(grayscaleImage));
5+
blockIndex = 0;
6+
7+
% Reconstruct the image from compressed data
8+
for row = 1:numBlocks
9+
for col = 1:numBlocks
10+
blockIndex = blockIndex + 1;
11+
dctBlock = blockStorage{blockIndex};
12+
quantizedBlock = dctBlock .* normMat;
13+
currentBlock = idct2(quantizedBlock) + 128;
14+
decodedImage(rangeBlockSize*(row-1)+1:rangeBlockSize*row, rangeBlockSize*(col-1)+1:rangeBlockSize*col) = currentBlock;
15+
end
16+
end
17+
18+
% Display the decoded image
19+
imshow(decodedImage / 256);

idct_transform.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
% Apply Inverse Discrete Cosine Transform
2+
function idctBlock = idct_transform(dctBlock)
3+
idctBlock = idct2(dctBlock) + 128;
4+
end

mean_domain_calculation.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
% Mean Domain Calculation and Transformations
2+
meanDomain = mean2(domainBlock);
3+
tmodify = domainBlock - meanDomain;

normalization_matrix.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
% Define Normalization Matrices for Quantization
2+
normMat = ones(16) * 10^4;
3+
normMat(1:8, 1:8) = [
4+
16 11 10 16 24 40 51 61
5+
12 12 14 19 26 58 60 55
6+
14 13 16 24 40 57 69 56
7+
14 17 22 29 51 87 80 62
8+
18 22 37 56 68 109 103 77
9+
24 35 55 64 81 104 113 92
10+
49 64 78 87 103 121 120 101
11+
72 92 95 98 112 100 103 99
12+
];

partition_no_search.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
% Partitioning of Image Blocks without Search
2+
newRangeBlockSize = rangeBlockSize / 2;
3+
newDomainBlockSize = domainBlockSize / 2;
4+
5+
numLevels = numLevels + 1;
6+
if numLevels == 2
7+
normMat = normMat(1:8, 1:8);
8+
errorThreshold = 6.5;
9+
elseif numLevels == 3
10+
normMat = 0.5 * normMat(1:4, 1:4);
11+
errorThreshold = 6.2;
12+
else
13+
normMat = 0.25 * normMat(1:2, 1:2);
14+
errorThreshold = inf;
15+
end
16+
17+
for i = 1:totalBlocks
18+
rangeBlock = blockStorage{i};
19+
for j = 1:4
20+
newBlock{j} = rangeBlock((j-1)*newRangeBlockSize+1:j*newRangeBlockSize, :);
21+
end
22+
blockStorage = [blockStorage newBlock];
23+
end
24+
25+
totalBlocks = length(blockStorage);

0 commit comments

Comments
 (0)