-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhey23.m
More file actions
212 lines (199 loc) · 7.94 KB
/
hey23.m
File metadata and controls
212 lines (199 loc) · 7.94 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
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
%===============================================================================
% Read in gray scale demo image.
folder = pwd; % Determine where demo folder is (works with all versions).
baseFileName = 'Capture10.PNG';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 1); % Take red channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the image.
subplot(3, 3, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Binarize the image
binaryImage = grayImage > 128;
% Display the image.
subplot(3, 3, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Make one type of mask, and display it.
mask1 = bwareafilt(binaryImage, 1);
mask1 = imfill(mask1, 'holes');
% Display the image.
subplot(3, 3, 3);
imshow(mask1, []);
title('Mask1 = exact outline of biggest blob', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Find boundary and display it
boundary1 = bwboundaries(mask1);
hold on;
plot(boundary1{1}(:, 2), boundary1{1}(:, 1), 'r-', 'LineWidth', 2);
% Make another type of mask, and display it.
mask2 = imclose(binaryImage, true(3));
mask2 = bwareafilt(mask2, 1);
mask2 = imfill(mask2, 'holes');
mask2 = bwconvhull(mask2);
% Display the image.
subplot(3, 3, 4);
imshow(mask2, []);
title('Mask2 = smoothed then convex hull', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Find boundary and display it
boundary2 = bwboundaries(mask2);
hold on;
plot(boundary2{1}(:, 2), boundary2{1}(:, 1), 'r-', 'LineWidth', 2);
% Make another type of mask, and display it.
windowWidth = 89;
kernel = ones(windowWidth) / windowWidth^2;
threshold = 0.13;
mask3 = conv2(double(binaryImage), kernel, 'same') > threshold;
mask3 = bwareafilt(mask3, 1);
mask3 = imfill(mask3, 'holes');
% Display the image.
subplot(3, 3, 5);
imshow(mask3, []);
title('Mask3 = blurred then thresholded', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Find boundary and display it
boundary3 = bwboundaries(mask3);
hold on;
plot(boundary3{1}(:, 2), boundary3{1}(:, 1), 'r-', 'LineWidth', 2);
% Make another type of mask, and display it.
mask4 = imclose(binaryImage, true(3));
mask4 = bwareafilt(mask4, 1);
mask4 = imfill(mask4, 'holes');
mask4 = bwconvhull(mask4);
se = strel('disk', 35, 0);
mask4 = imdilate(mask4, se);
% Display the image.
subplot(3, 3, 6);
imshow(mask4, []);
title('Mask4 = dilated convex hull', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Find boundary and display it
boundary4 = bwboundaries(mask4);
hold on;
plot(boundary4{1}(:, 2), boundary4{1}(:, 1), 'r-', 'LineWidth', 2);
% Make one type of mask, and display it.
mask5 = bwareafilt(binaryImage, 1);
mask5 = imfill(mask5, 'holes');
% Find centroid
props = regionprops(mask5, 'Centroid');
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
[columnsInImage rowsInImage] = meshgrid(1:columns, 1:rows);
% Next create the circle in the image.
centerX = props.Centroid(1);
centerY = props.Centroid(2);
radius = 200;
mask5 = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Display the image.
subplot(3, 3, 7);
imshow(mask5, []);
title('Mask5 = perfect circle', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Find boundary and display it
boundary5 = bwboundaries(mask5);
hold on;
plot(boundary5{1}(:, 2), boundary5{1}(:, 1), 'r-', 'LineWidth', 2);
%=====================================================================
% Show all boundaries on original image.
% Display the image.
figure;
imshow(binaryImage, []);
title('All Boundaries', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
hold on;
plot(boundary1{1}(:, 2), boundary1{1}(:, 1), 'r-', 'LineWidth', 2);
plot(boundary2{1}(:, 2), boundary2{1}(:, 1), 'r-', 'LineWidth', 2);
plot(boundary3{1}(:, 2), boundary3{1}(:, 1), 'r-', 'LineWidth', 2);
plot(boundary4{1}(:, 2), boundary4{1}(:, 1), 'r-', 'LineWidth', 2);
plot(boundary5{1}(:, 2), boundary5{1}(:, 1), 'r-', 'LineWidth', 2);
% Compute number of white, black, and area fraction of each mask
fprintf('Mask # Num White, Num Black, Num Mask, Area Fraction\n----------------------------------------------------------\n');
% For mask #1:
areaMask = nnz(mask1);
areaWhite = nnz(mask1 & binaryImage);
areaBlack = areaMask - areaWhite;
areaFraction = areaWhite / areaMask;
fprintf(' 1 %12d %10d %11d %12.3f\n', areaWhite, areaBlack, areaMask, areaFraction);
% For mask #2:
areaMask = nnz(mask2);
areaWhite = nnz(mask2 & binaryImage);
areaBlack = areaMask - areaWhite;
areaFraction = areaWhite / areaMask;
fprintf(' 2 %12d %10d %11d %12.3f\n', areaWhite, areaBlack, areaMask, areaFraction);
% For mask #3:
areaMask = nnz(mask3);
areaWhite = nnz(mask3 & binaryImage);
areaBlack = areaMask - areaWhite;
areaFraction = areaWhite / areaMask;
fprintf(' 3 %12d %10d %11d %12.3f\n', areaWhite, areaBlack, areaMask, areaFraction);
% For mask #4:
areaMask = nnz(mask4);
areaWhite = nnz(mask4 & binaryImage);
areaBlack = areaMask - areaWhite;
areaFraction = areaWhite / areaMask;
fprintf(' 4 %12d %10d %11d %12.3f\n', areaWhite, areaBlack, areaMask, areaFraction);
% For mask #5:
areaMask = nnz(mask5);
areaWhite = nnz(mask5 & binaryImage);
areaBlack = areaMask - areaWhite;
areaFraction = areaWhite / areaMask;
fprintf(' 5 %12d %10d %11d %12.3f\n', areaWhite, areaBlack, areaMask, areaFraction);