-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
263 lines (238 loc) · 10.2 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
function main
% Create the GUI figure
fig = figure('Name', 'Image Processing GUI', ...
'NumberTitle', 'off', ...
'Position', [100, 100, 1200, 800], ...
'Color', [0.2, 0.2, 0.2], ...
'MenuBar', 'none', ...
'ToolBar', 'none', ...
'Resize', 'on');
% Title
uicontrol('Style', 'text', ...
'String', 'Image Processing Application', ...
'FontSize', 20, ...
'FontWeight', 'bold', ...
'ForegroundColor', [1, 1, 1], ...
'BackgroundColor', [0.2, 0.2, 0.2], ...
'Units', 'normalized', ...
'Position', [0.35, 0.9, 0.3, 0.05], ...
'HorizontalAlignment', 'center');
% Axes for displaying images and results
originalAxis = axes('Units', 'normalized', 'Position', [0.05, 0.45, 0.4, 0.4], ...
'Box', 'on', 'XColor', 'none', 'YColor', 'none');
processedAxis = axes('Units', 'normalized', 'Position', [0.55, 0.45, 0.4, 0.4], ...
'Box', 'on', 'XColor', 'none', 'YColor', 'none');
% Upload / Change Image Button
uicontrol('Style', 'pushbutton', ...
'String', 'Upload / Change Image', ...
'Units', 'normalized', ...
'Position', [0.2, 0.3, 0.2, 0.05], ...
'FontSize', 14, ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1], ...
'Callback', @uploadImage);
% Convert to Grayscale Button
uicontrol('Style', 'pushbutton', ...
'String', 'Convert to Grayscale', ...
'Units', 'normalized', ...
'Position', [0.2, 0.24, 0.2, 0.05], ...
'FontSize', 14, ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1], ...
'Callback', @convertToGrayscale);
% Grayscale Weights Testing Section
uicontrol('Style', 'text', ...
'String', 'RGB Weights for Grayscale:', ...
'Units', 'normalized', ...
'Position', [0.2, 0.21, 0.2, 0.03], ...
'ForegroundColor', [1, 1, 1], ...
'BackgroundColor', [0.2, 0.2, 0.2]);
rWeightInput = uicontrol('Style', 'edit', ...
'String', '0.2989', ...
'Units', 'normalized', ...
'Position', [0.2, 0.18, 0.05, 0.03], ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1]);
gWeightInput = uicontrol('Style', 'edit', ...
'String', '0.5870', ...
'Units', 'normalized', ...
'Position', [0.26, 0.18, 0.05, 0.03], ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1]);
bWeightInput = uicontrol('Style', 'edit', ...
'String', '0.1140', ...
'Units', 'normalized', ...
'Position', [0.32, 0.18, 0.05, 0.03], ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1]);
% Test All Weights Button
uicontrol('Style', 'pushbutton', ...
'String', 'Test All Weights', ...
'Units', 'normalized', ...
'Position', [0.2, 0.12, 0.2, 0.05], ...
'FontSize', 14, ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1], ...
'Callback', @testAllWeights);
% Operation Buttons
uicontrol('Style', 'pushbutton', ...
'String', 'Calculate Contrast', ...
'Units', 'normalized', ...
'Position', [0.7, 0.3, 0.2, 0.05], ...
'FontSize', 14, ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1], ...
'Callback', @calculateContrast);
uicontrol('Style', 'pushbutton', ...
'String', 'Show Histogram', ...
'Units', 'normalized', ...
'Position', [0.7, 0.24, 0.2, 0.05], ...
'FontSize', 14, ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1], ...
'Callback', @showHistogram);
uicontrol('Style', 'pushbutton', ...
'String', 'Binarize Image', ...
'Units', 'normalized', ...
'Position', [0.7, 0.18, 0.2, 0.05], ...
'FontSize', 14, ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1], ...
'Callback', @binarizeImage);
% Threshold Slider
uicontrol('Style', 'text', ...
'String', 'Threshold:', ...
'Units', 'normalized', ...
'Position', [0.7, 0.15, 0.1, 0.03], ...
'ForegroundColor', [1, 1, 1], ...
'BackgroundColor', [0.2, 0.2, 0.2]);
thresholdSlider = uicontrol('Style', 'slider', ...
'Min', 0, 'Max', 1, 'Value', 0.5, ...
'Units', 'normalized', ...
'Position', [0.8, 0.15, 0.1, 0.03], ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1]);
% Morphological Operation Dropdown
uicontrol('Style', 'text', ...
'String', 'Morphological Operation:', ...
'Units', 'normalized', ...
'Position', [0.7, 0.09, 0.2, 0.03], ...
'ForegroundColor', [1, 1, 1], ...
'BackgroundColor', [0.2, 0.2, 0.2]);
morphOperationDropdown = uicontrol('Style', 'popupmenu', ...
'String', {'Dilation', 'Erosion'}, ...
'Units', 'normalized', ...
'Position', [0.7, 0.06, 0.2, 0.03], ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1]);
uicontrol('Style', 'pushbutton', ...
'String', 'Morphological Filtering', ...
'Units', 'normalized', ...
'Position', [0.7, 0.02, 0.2, 0.05], ...
'FontSize', 14, ...
'BackgroundColor', [0.3, 0.3, 0.3], ...
'ForegroundColor', [1, 1, 1], ...
'Callback', @morphologicalFiltering);
% Variables to store the current image and results
currentImage = [];
grayscaleImage = [];
binarizedImage = [];
% Upload Image Function
function uploadImage(~, ~)
[file, path] = uigetfile({'*.jpg;*.png;*.bmp', 'Image Files'});
if isequal(file, 0)
return;
end
currentImage = imread(fullfile(path, file));
imshow(currentImage, 'Parent', originalAxis);
end
% Calculate Contrast Function
function calculateContrast(~, ~)
if isempty(currentImage)
errordlg('Please upload an image first.');
return;
end
grayImg = convertToGrayscale();
meanIntensity = mean(grayImg(:));
contrast = sqrt(mean((double(grayImg(:)) - meanIntensity).^2));
msgbox(sprintf('RMS Contrast: %.2f', contrast));
end
% Convert to Grayscale Function
function grayImg = convertToGrayscale(~, ~)
if isempty(currentImage)
errordlg('Please upload an image first.');
return;
end
r = str2double(rWeightInput.String);
g = str2double(gWeightInput.String);
b = str2double(bWeightInput.String);
grayImg = uint8(r * double(currentImage(:, :, 1)) + ...
g * double(currentImage(:, :, 2)) + ...
b * double(currentImage(:, :, 3)));
imshow(grayImg, 'Parent', processedAxis);
grayscaleImage = grayImg;
end
% Show Histogram Function
function showHistogram(~, ~)
if isempty(grayscaleImage)
errordlg('Convert the image to grayscale first.');
return;
end
axes(processedAxis);
imhist(grayscaleImage);
title('Histogram', 'Color', [1, 1, 1]);
end
% Binarize Image Function
function binarizeImage(~, ~)
if isempty(grayscaleImage)
errordlg('Convert the image to grayscale first.');
return;
end
threshold = get(thresholdSlider, 'Value');
binImg = imbinarize(grayscaleImage, threshold);
imshow(binImg, 'Parent', processedAxis);
binarizedImage = binImg;
end
% Morphological Filtering Function
function morphologicalFiltering(~, ~)
if isempty(binarizedImage)
errordlg('Binarize the image first.');
return;
end
operation = morphOperationDropdown.Value;
se = strel('disk', 5); % Example structuring element
if operation == 1
filteredImg = imdilate(binarizedImage, se);
else
filteredImg = imerode(binarizedImage, se);
end
imshow(filteredImg, 'Parent', processedAxis);
end
% Test All Weights Function
function testAllWeights(~, ~)
if isempty(currentImage)
errordlg('Please upload an image first.');
return;
end
results = [];
step = 0.1;
for r = 0:step:1
for g = 0:step:1
b = 1 - r - g;
if b < 0 || b > 1
continue;
end
grayImg = uint8(r * double(currentImage(:, :, 1)) + ...
g * double(currentImage(:, :, 2)) + ...
b * double(currentImage(:, :, 3)));
meanIntensity = mean(grayImg(:));
contrast = sqrt(mean((double(grayImg(:)) - meanIntensity).^2));
results = [results; r, g, b, contrast];
end
end
[~, idx] = max(results(:, 4));
bestWeights = results(idx, 1:3);
msgbox(sprintf('Best Weights: R=%.2f, G=%.2f, B=%.2f', ...
bestWeights(1), bestWeights(2), bestWeights(3)));
end
end