Repository navigation
Expand file tree
/
Copy pathImageFilterComparisonApp.m
More file actions
229 lines (202 loc) · 9.5 KB
/
Copy pathImageFilterComparisonApp.m
File metadata and controls
229 lines (202 loc) · 9.5 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
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
classdef ImageFilterComparisonApp < matlab.apps.App
%IMAGEFILTERCOMPARISONAPP Compare results of image filtering
% The ImageFilterComparisonTool app visualizes and helps compare
% the result of various image filtering functions in Image Processing
% Toolbox. Users can change various parameters and see the results
% live update. The best result can then be exported to file or
% workspace.
%
% Copyright 2026 The MathWorks, Inc.
% Used to locate and load the app's XML configuration file
properties (Access = public, Constant)
AppConfigFilename = './ImageFilterComparisonApp.xml'; % File path to the app configuration file containing component layout and settings
end
% Properties that correspond to app components
properties (Access = public)
ImageFilterComparisonToolFigure matlab.ui.Figure
MainGridLayout matlab.ui.container.GridLayout
WorkingAreaGridLayout matlab.ui.container.GridLayout
ToolstripGridLayout matlab.ui.container.GridLayout
ExportPanel matlab.ui.container.Panel
ExportSectionGridLayout matlab.ui.container.GridLayout
ToFileDropDown matlab.ui.control.DropDown
ToWorkspaceDropDown matlab.ui.control.DropDown
ToWorkspaceLabel matlab.ui.control.Label
ToFileLabel matlab.ui.control.Label
ImportPanel matlab.ui.container.Panel
ImportSectionGridLayout matlab.ui.container.GridLayout
FromWorkspaceDropDown matlab.ui.control.DropDown
FromWorkspaceDropDownLabel matlab.ui.control.Label
BrowseButton matlab.ui.control.Button
FromFileLabel matlab.ui.control.Label
end
properties (Access = private)
LeftComponent ImageFilter
RightComponent ImageFilter
end
properties (Access = private,Constant)
DefaultImportFromWorkspaceDropDownValue = "Select..."; % First option in import from workspace drop down
end
methods (Access = private)
function resetAppOnNewDataLoad(app,img)
% Function to set input image and reset app on new data load
app.LeftComponent.Image = img;
app.RightComponent.Image = img;
app.ToFileLabel.Enable = "on";
app.ToFileDropDown.Enable = "on";
app.ToWorkspaceLabel.Enable = "on";
app.ToWorkspaceDropDown.Enable = "on";
end
function filterSpec = getSupportedFileFilter(~,writeTF)
% Function to get filter for uigetfile or uiputfile containing
% all the formats supported by imread or imwrite. If writeTF is
% true, this function returns the filter for imwrite.
arguments
~
writeTF = false
end
% Get all supported formats of imread and imwrite
fmts = imformats;
% Remove .gif (unsupported format)
fmts(arrayfun(@(f) any(strcmp(f.ext, 'gif')), fmts)) = [];
if writeTF
% Remove formats not supported by imwrite
fmts(cellfun(@isempty,{fmts.write})) = [];
% Remove .pcx and .xwd (require indexed image + colormap)
fmts(arrayfun(@(f) any(ismember(f.ext, {'pcx','xwd'})), fmts)) = [];
end
% Create list of formats and their names
n = numel(fmts);
filterSpec = cell(n+2,2); % Pre-allocate
allExts = {};
for idx = 1:n
allExts = [allExts,fmts(idx).ext]; %#ok<AGROW>
end
allPatterns = strcat("*.",allExts);
filterSpec{1,1} = strjoin(allPatterns,";");
filterSpec{1,2} = "All Image Files";
for idx = 1:n
exts = strcat("*.",fmts(idx).ext);
pattern = strjoin(exts,";");
desc = fmts(idx).description;
filterSpec{idx+1,1} = pattern;
filterSpec{idx+1,2} = desc;
end
filterSpec{n+2,1} = "*.*";
filterSpec{n+2,2} = "All Files";
end
end
% Callbacks that handle component events
methods
% Code that executes after component creation
function startupFcn(app)
% Initialization function to create the component containers
% and link their viewers
app.LeftComponent = ImageFilter(app.WorkingAreaGridLayout);
app.RightComponent = ImageFilter(app.WorkingAreaGridLayout);
linkviewers([app.LeftComponent.Viewer,app.RightComponent.Viewer],"on");
% Set a name for each component to make it easy to identity them
app.LeftComponent.ImageName = "Left Image";
app.RightComponent.ImageName = "Right Image";
end
% Button pushed function: BrowseButton
function BrowseButtonPushed(app, event)
% Function to import an image file by browsing file system
filterSpec = app.getSupportedFileFilter();
[file,location] = uigetfile(filterSpec,"Select an image file");
if ~isequal(file,0)
try
imageData = imread(fullfile(location,file));
catch
uialert(app.ImageFilterComparisonToolFigure,"Unable to read image file.","Invalid File");
return;
end
app.resetAppOnNewDataLoad(imageData);
end
end
% Drop down opening function: FromWorkspaceDropDown
function FromWorkspaceDropDownOpening(app, event)
% Callback function to filter possible images in workspace and
% display them in FromWorkspaceDropDown for input selection
vars = evalin("base","whos");
supportedClasses = ["int8","uint8","int16","uint16","int32","uint32","single","double","logical"];
ValidInputVariables = app.DefaultImportFromWorkspaceDropDownValue;
for idx = 1:numel(vars)
var = vars(idx);
TF = ismember(var.class,supportedClasses) && ((length(var.size) == 2) || (length(var.size) == 3 && var.size(3) == 3));
if TF
ValidInputVariables(end+1) = convertCharsToStrings(vars(idx).name);%#ok<AGROW>
end
end
app.FromWorkspaceDropDown.Items = ValidInputVariables;
end
% Value changed function: FromWorkspaceDropDown
function FromWorkspaceDropDownValueChanged(app, event)
% Callback function to read an image variable from workspace and
% load it to the app
value = app.FromWorkspaceDropDown.Value;
if ~strcmp(value,app.DefaultImportFromWorkspaceDropDownValue)
imageData = evalin("base",value);
app.resetAppOnNewDataLoad(imageData);
end
app.FromWorkspaceDropDown.Items = app.DefaultImportFromWorkspaceDropDownValue;
end
% Value changed function: ToFileDropDown
function ToFileDropDownValueChanged(app, event)
% Callback function to write filtered image to disk
value = app.ToFileDropDown.Value;
app.ToFileDropDown.Value = app.DefaultImportFromWorkspaceDropDownValue;
switch value
case "Image 1"
imageToExport = app.LeftComponent.Image;
case "Image 2"
imageToExport = app.RightComponent.Image;
otherwise
return;
end
filterSpec = app.getSupportedFileFilter(true);
[file,location] = uiputfile(filterSpec,"Save Filtered Image","filteredImage.png");
if ~(isequal(file,0) || isequal(location,0))
try
imwrite(imageToExport,fullfile(location,file));
uialert(app.ImageFilterComparisonToolFigure,...
"Filtered image saved successfully",...
"Export Success",...
Icon="success");
catch ME
uialert(app.ImageFilterComparisonToolFigure,ME.message,"Export Failed");
end
end
end
% Value changed function: ToWorkspaceDropDown
function ToWorkspaceDropDownValueChanged(app, event)
% Callback function to write filtered image to base workspace
value = app.ToWorkspaceDropDown.Value;
app.ToWorkspaceDropDown.Value = app.DefaultImportFromWorkspaceDropDownValue;
switch value
case "Image 1"
imageToExport = app.LeftComponent.Image;
assignin("base","filteredImageLeft",imageToExport);
case "Image 2"
imageToExport = app.RightComponent.Image;
assignin("base","filteredImageRight",imageToExport);
otherwise
return;
end
uialert(app.ImageFilterComparisonToolFigure,...
"Filtered image saved to workspace successfully",...
"Export Success",...
Icon="success");
end
end
% App creation
methods (Access = public)
% Construct app
function app = ImageFilterComparisonApp(varargin)
app = app@matlab.apps.App(varargin{:});
if nargout == 0
clear app
end
end
end
end