-
Notifications
You must be signed in to change notification settings - Fork 0
/
dct_debug.m
129 lines (85 loc) · 3.09 KB
/
dct_debug.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
clc;
clear all;
close all;
function [C1,C2] = getkernels(image)
[M,N] = size(image);
i = (0:M-1);
j = (0:M-1);
C0 = cos(pi*(2*i'+1)*j/(2*M));
C1 = C0 * sqrt(diag([1, 2*ones(1,M-1)])/M);
k = (0:N-1);
l = (0:N-1);
C0 = cos(pi*(2*k'+1)*l/(2*N));
C2 = C0 * sqrt(diag([1, 2*ones(1,N-1)])/N);
endfunction
function dct_image = fdct2_scratch(image, C1, C2)
dct_image = C1*image*C2';
endfunction
function idct_image = ifdct2_scratch(image, C1, C2)
idct_image = C1'*image*C2;
endfunction
function compressed_img = compressImageDCT(img, compression_factor)
% Convert the image to grayscale if it's in color
if size(img, 3) == 3
img = rgb2gray(img);
img = im2double(img);
endif
% Calculate the DCT kernels
[C1, C2] = getkernels(img);
% Apply 2D DCT to the image
img_dct = fdct2_scratch(img, C1, C2);
% Set high-frequency coefficients to zero based on the compression factor
#threshold = compression_factor * max(img_dct(:));
threshold=0.1;
% Find the number of coefficients to keep
num_coeffs = round(threshold * numel(img_dct));
% Sort the DCT coefficients in descending order of magnitude
sorted_coeffs = sort(abs(img_dct(:)), 'descend');
% Find the threshold value
threshold_value = sorted_coeffs(num_coeffs);
img_dct(abs(img_dct) < threshold_value) = 0;
% Apply inverse 2D DCT to get the compressed image
compressed_img = ifdct2_scratch(img_dct, C1, C2);
% Set a threshold value for compression
#threshold = 0.1;
% Find the number of coefficients to keep
#num_coeffs = round(threshold * numel(dct_img));
% Sort the DCT coefficients in descending order of magnitude
#sorted_coeffs = sort(abs(dct_img(:)), 'descend');
% Find the threshold value
#threshold_value = sorted_coeffs(num_coeffs);
% Set the coefficients below the threshold to zero
#dct_img(abs(dct_img) < threshold_value) = 0;
% Apply inverse DCT to the compressed image
#compressed_img = ifdct2_scratch(dct_img, C1, C2);
endfunction
% Read the image
img = imread('C:\Users\HP\Desktop\College\Sem 5\DSP\Project\monalisa.jpg');
% Convert the image to grayscale
gray_img = rgb2gray(img);
gray_img = im2double(gray_img);
% Calculate the DCT kernels
[C1, C2] = getkernels(gray_img);
% Apply DCT to the image
dct_img = fdct2_scratch(gray_img, C1, C2);
Q = input('Q: ');
dct_img = Q*(round(dct_img/Q));
#dct_img(abs(dct_img) < threshold_value) = 0;
% Apply inverse DCT to the compressed image
compressed_img = ifdct2_scratch(dct_img, C1, C2);
% Display the original and compressed images
figure;
subplot(1,2,1);
imshow(img);
title('Original Image');
subplot(1,2,2);
imshow(compressed_img);
title('Compressed Image');
# Calculate size of original image and compressed image in kilobytes
original_size = numel(gray_img);
compressed_size = nnz(dct_img);
# Calculate compression ratio
compression_ratio = original_size/compressed_size;
fprintf("Original size: %d\n", original_size);
fprintf("Compressed size: %d\n", compressed_size);
fprintf("Compression ratio: %d\n", compression_ratio);