-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdemo_script.m
More file actions
180 lines (159 loc) · 6.66 KB
/
Copy pathdemo_script.m
File metadata and controls
180 lines (159 loc) · 6.66 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
% Demo to threshold anad mask a double or integer image.
% By Image Analyst, May 2015
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% User has the Image Processing Toolbox.
% Continue with the demo. Do some initialization stuff.
clc; % Clear command window.
clear; % Delete all variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% File doesn't exist. Try it without the folder.
% It might be able to find it in a folder off the search path.
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
% Can't find it off the search path either.
errorMessage = sprintf('Error: cannot find demo image %s', baseFileName);
uiwait(msgbox(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
axis off;
title('Original Grayscale Image', 'FontSize', fontSize);
% Set up figure properties.
set(gcf, 'Name', 'Thresholding Demo by ImageAnalyst', 'NumberTitle', 'off')
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.
message = sprintf('Thresholding demo by ImageAnalyst.\n\nDo you want to use an integer image or a floating point image?');
button = questdlg(message, 'Image Type?', 'Integer', 'Floating Point', 'Cancel', 'Integer');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'Cancel')
close(gcf); % Get rid of window.
return;
end
if strcmpi(button, 'Floating Point')
% Convert to double in the range -5000 to + 15000
% Get input min and max.
minGL = double(min(grayImage(:)));
maxGL = double(max(grayImage(:)));
% Scale the image
imageToThreshold = 20000 * mat2gray(grayImage) - 5000;
% Verify them
minDblGL = min(imageToThreshold(:));
maxDblGL = max(imageToThreshold(:));
fprintf('Before scaling, min gray level = %.1f, max gray level = %.1f\nAfter scaling, min gray level = %.1f, max gray level = %.1f\n', ...
minGL, maxGL, minDblGL, maxDblGL);
startingLowThreshold = -800;
startingHighThreshold = 10400;
% Get the histogram
[pixelCount, grayLevels] = hist(imageToThreshold(:), 300);
subplot(2, 3, 2);
bar(grayLevels, pixelCount, 'BarWidth', 1, 'FaceColor', 'b');
title('Histogram of Original Double Image', 'FontSize', fontSize);
xlim([minDblGL, maxDblGL]); % Scale x axis manually.
grid on;
else
% Integer image. Just leave it alone.
imageToThreshold = grayImage;
startingLowThreshold = 7;
startingHighThreshold = 23;
% Let's compute and display the histogram, just for fun.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 3, 2);
bar(grayLevels, pixelCount, 'BarWidth', 1, 'FaceColor', 'b');
title('Histogram of Original Integer Image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
grid on;
end
%====================== KEY PART RIGHT HERE!!!! ===================================================
% Threshold with starting range startingLowThreshold to startingHighThreshold.
[lowThreshold, highThreshold] = threshold(startingLowThreshold, startingHighThreshold, imageToThreshold);
%====================== KEY PART RIGHT HERE!!!! ===================================================
% Place vertical red bars over the histogram at the threshold locations.
hold on;
yAxisLimits = ylim();
line([lowThreshold, lowThreshold], yAxisLimits, 'Color', 'r', 'LineWidth', 2);
line([highThreshold, highThreshold], yAxisLimits, 'Color', 'r', 'LineWidth', 2);
caption = sprintf(' Low Threshold = %.1f', lowThreshold);
text(lowThreshold, 0.7*yAxisLimits(2), caption, 'Color', 'r', 'FontWeight', 'Bold', 'FontSize', 15);
caption = sprintf(' High Threshold = %.1f', highThreshold);
text(highThreshold, 0.9*yAxisLimits(2), caption, 'Color', 'r', 'FontWeight', 'Bold', 'FontSize', 15);
% Binarize the image.
binaryImage = (imageToThreshold > lowThreshold) & (imageToThreshold < highThreshold);
subplot(2, 3, 3);
imshow(binaryImage, []);
axis off;
title('Binary Image or "Mask"', 'FontSize', fontSize);
% Compute max and min of the original image.
minValue = min(imageToThreshold(:));
maxValue = max(imageToThreshold(:));
% Make the image inside the mask have a value of zero.
maskedImage = imageToThreshold;
maskedImage(binaryImage) = 0;
subplot(4, 3, 7);
imshow(maskedImage, []);
axis off;
title('Zero Value Inside the Mask', 'FontSize', fontSize);
% Make the image inside the mask have the min value.
maskedImage = imageToThreshold;
maskedImage(binaryImage) = minValue;
subplot(4, 3, 8);
imshow(maskedImage, []);
axis off;
caption = sprintf('Min Value (%.1f) Inside the Mask', minValue);
title(caption, 'FontSize', fontSize);
% Make the image inside the mask have the max value.
maskedImage = imageToThreshold;
maskedImage(binaryImage) = maxValue;
subplot(4, 3, 9);
imshow(maskedImage, []);
axis off;
caption = sprintf('Max Value (%.1f) Inside the Mask', maxValue);
title(caption, 'FontSize', fontSize);
% Now do the same thing but OUTSIDE the mask.
outsideMask = ~binaryImage;
% Make the image outside the mask have a value of zero.
maskedImage = imageToThreshold;
maskedImage(outsideMask) = 0;
subplot(4, 3, 10);
imshow(maskedImage, []);
axis off;
title('Zero Value Outside the Mask', 'FontSize', fontSize);
% Make the image outside the mask have the min value.
maskedImage = imageToThreshold;
maskedImage(outsideMask) = minValue;
subplot(4, 3, 11);
imshow(maskedImage, []);
axis off;
caption = sprintf('Min Value (%.1f) Outside the Mask', minValue);
title(caption, 'FontSize', fontSize);
% Make the image outside the mask have the max value.
maskedImage = imageToThreshold;
maskedImage(outsideMask) = maxValue;
subplot(4, 3, 12);
imshow(maskedImage, []);
axis off;
caption = sprintf('Max Value (%.1f) Outside the Mask', maxValue);
title(caption, 'FontSize', fontSize);
% Alert user we're done.
uiwait(helpdlg('Done with demo.'));