-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImagecompression.m
More file actions
123 lines (107 loc) · 5.72 KB
/
Imagecompression.m
File metadata and controls
123 lines (107 loc) · 5.72 KB
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
function interactiveImageCompressionColorUI
% Create the main figure with dynamic layout and vibrant theme
hFig = figure('Name', 'Dynamic Image Compression UI', 'NumberTitle', 'off', ...
'Position', [100, 100, 1000, 700], 'Color', [0.15, 0.15, 0.2]);
% Title banner
uicontrol('Style', 'text', 'Position', [0, 660, 1000, 30], ...
'String', 'Wavelet vs Fourier Image Compression', ...
'BackgroundColor', [0.2, 0.3, 0.5], 'ForegroundColor', [1, 1, 1], ...
'FontSize', 14, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
% Left panel for controls
uipanel('Title', 'Controls', 'FontSize', 12, ...
'BackgroundColor', [0.2, 0.2, 0.3], 'ForegroundColor', [1, 1, 1], ...
'Position', [0.02, 0.05, 0.25, 0.85]);
% Threshold input
uicontrol('Style', 'text', 'Position', [30, 550, 200, 20], ...
'String', 'Select Threshold (0-1):', 'HorizontalAlignment', 'left', ...
'ForegroundColor', [1, 1, 1], 'BackgroundColor', [0.2, 0.2, 0.3], 'FontSize', 10);
hThreshold = uicontrol('Style', 'edit', 'Position', [30, 520, 200, 30], 'String', '0.1', ...
'BackgroundColor', [0.3, 0.3, 0.4], 'ForegroundColor', [1, 1, 1], ...
'FontSize', 10);
% Load image button
uicontrol('Style', 'pushbutton', 'Position', [30, 470, 200, 40], ...
'String', 'Load Image', 'Callback', @loadImage, ...
'BackgroundColor', [0.3, 0.5, 0.7], 'ForegroundColor', [1, 1, 1], ...
'FontWeight', 'bold', 'FontSize', 11);
% Run compression button
uicontrol('Style', 'pushbutton', 'Position', [30, 410, 200, 40], ...
'String', 'Run Compression', 'Callback', @runCompression, ...
'BackgroundColor', [0.3, 0.5, 0.7], 'ForegroundColor', [1, 1, 1], ...
'FontWeight', 'bold', 'FontSize', 11);
% Axes for displaying images
hAxesOriginal = axes('Parent', hFig, 'Position', [0.35, 0.55, 0.25, 0.35], 'Color', [0.15, 0.15, 0.2]);
hAxesWavelet = axes('Parent', hFig, 'Position', [0.7, 0.55, 0.25, 0.35], 'Color', [0.15, 0.15, 0.2]);
hAxesFourier = axes('Parent', hFig, 'Position', [0.35, 0.1, 0.25, 0.35], 'Color', [0.15, 0.15, 0.2]);
hAxesDifference = axes('Parent', hFig, 'Position', [0.7, 0.1, 0.25, 0.35], 'Color', [0.15, 0.15, 0.2]);
% Load and display the default image
defaultImagePath = 'C:/Users/hp/Downloads/logos/BDE.png';
originalImage = im2double(imread(defaultImagePath));
axes(hAxesOriginal);
imshow(originalImage, []);
title('Original Image', 'Color', [1, 1, 1]);
function loadImage(~, ~)
[file, path] = uigetfile({'*.*', 'All Files'; '*.jpg;*.jpeg;*.png;*.bmp;*.tif', 'Image Files'}, ...
'Select an Image File');
if isequal(file, 0)
return; % User canceled the file selection
end
imgPath = fullfile(path, file);
try
originalImage = im2double(imread(imgPath));
axes(hAxesOriginal);
imshow(originalImage, []);
title('Original Image', 'Color', [1, 1, 1]);
catch ME
errordlg(['Error loading image: ' ME.message], 'File Error', 'modal');
end
end
function runCompression(~, ~)
% Get threshold value from UI
threshold = str2double(get(hThreshold, 'String'));
if isnan(threshold) || threshold < 0 || threshold > 1
errordlg('Please enter a valid threshold between 0 and 1.', 'Invalid Threshold', 'modal');
return;
end
% Process each color channel separately
if size(originalImage, 3) == 3
% Color image (RGB)
[waveletImage, fourierImage] = processColorImage(originalImage, threshold);
else
% Grayscale image
waveletImage = processGrayscaleImage(originalImage, threshold);
fourierImage = processGrayscaleImage(originalImage, threshold, true);
end
% Display the wavelet compressed image
axes(hAxesWavelet);
imshow(waveletImage, []);
title('Wavelet Compressed', 'Color', [1, 1, 1]);
% Display the Fourier compressed image
axes(hAxesFourier);
imshow(fourierImage, []);
title('Fourier Compressed', 'Color', [1, 1, 1]);
% Display difference between original and compressed images
axes(hAxesDifference);
imshow(abs(waveletImage - fourierImage), []);
title('Wavelet vs Fourier Difference', 'Color', [1, 1, 1]);
end
function [waveletImage, fourierImage] = processColorImage(img, threshold)
% Initialize output images
waveletImage = zeros(size(img));
fourierImage = zeros(size(img));
% Process each color channel independently
for c = 1:3
% Wavelet Transform Compression
[coeffs, S] = wavedec2(img(:, :, c), 2, 'haar'); % 2-level decomposition
waveletCoeffs = coeffs;
waveletCoeffs(abs(waveletCoeffs) < threshold * max(abs(waveletCoeffs(:)))) = 0; % Thresholding
waveletImage(:, :, c) = waverec2(waveletCoeffs, S, 'haar'); % Reconstruction
% Fourier Transform Compression
F = fft2(img(:, :, c)); % Compute the 2D Fourier Transform
Fshift = fftshift(F); % Shift zero frequency to the center
magnitude = abs(Fshift);
Fshift(magnitude < threshold * max(magnitude(:))) = 0; % Thresholding
Fcompressed = ifftshift(Fshift); % Inverse shift
fourierImage(:, :, c) = real(ifft2(Fcompressed)); % Reconstruction
end
end
end