-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenMV_test.m
192 lines (146 loc) · 6.12 KB
/
OpenMV_test.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
%{
EdgyOceans: Vertical Edge detection for Obstacle Avoidance... at sea!
Copyright (C) 2018 C. Drew and P. Brine
cdrew2@stevens.edu pbrine@stevens.edu
GNU GPL v.3
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
NO WARRANTY IS IMPLIED NOR LIABILITY FOR ANY PURPOSE ACCEPTED.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
%}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Filter Block:
%{
TODO:
3. Angle calculation
4. Focal length Input
5. Web hosting
6. Update report: components, usage results
%}
filterRoberts1 = [-1 0;
0 1];
filterRoberts2 = [0 -1;
1 0];
filterPrewittVert = [-1 0 1;
-1 0 1;
-1 0 1;];
filterSobelVert = [-1 0 1;
-2 0 2;
-1 0 1];
filterLaplacian = [ 0 -1 0;
-1 4 -1;
0 -1 0];
%{
this filter is too weak, testing stronger filter
filterGaussian = [0 0.125 0;
0.125 0.5 0.125;
0 0.125 0;];
%}
filterGaussian = [1 4 7 4 1;
4 16 26 16 4;
7 26 41 26 7;
4 16 26 16 4;
1 4 7 4 1;]/273;
filterLoG = conv2(filterLaplacian, filterGaussian);
filterLoGVert = conv2(filterPrewittVert, filterGaussian);
border = 10; %pixels to remove for border created by convolutions
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Input Block:
[fileName,pathName] = uigetfile({'*.jpg; *.png', 'Image Files (*.jpg, *.png)'},'Please Choose an Image');
if isequal(fileName,0)
disp('No file chosen!');
quit;
else
disp(['Image selected: ', fullfile(pathName,fileName)]);
end
imageColor = imread(fullfile(pathName, fileName));
image = rgb2gray(im2double(imageColor));
% image = histeq(image); % not used due to amplifying noise. Needs
% tweaking.
%image = rgb2gray(im2double(imread(fullfile(pathName, fileName))));
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Processing Block:
outputRoberts = abs(conv2(image,filterRoberts1));
outputRoberts = outputRoberts + abs(conv2(image,filterRoberts2));
outputVertEdge = abs(conv2(image,filterPrewittVert));
outputLaplacian = abs(conv2(image,filterLaplacian));
outputGaussian = abs(conv2(image,filterGaussian));
outputLoG = abs(conv2(image,filterLoG));
outputLoGVert = abs(conv2(image,filterLoGVert));
threshold = (2*graythresh(image) + 8*graythresh(outputLoGVert))/10; %set threshold level
outputThresholding = imbinarize(outputLoGVert, threshold); %binarize image
[width, height] = size(outputThresholding); %obtain width and height of output
%next 4 lines: set border pixels to 0 to compensate for convolution
outputThresholding(1:10,:) = 0;
outputThresholding((width-border):width,:) = 0;
outputThresholding(:,1:border) = 0;
outputThresholding(:,(height-border):height) = 0;
outputThresholding = medfilt2(outputThresholding,[7 3]);
% ^ median-filter for threshold to eliminate small artifacts and fill-in gaps
% the 7x3 is a vertically-oriented rectangular median filter to eliminate more
% of the horizontal artifacts and fill-in more vertical artifacts
outputThresholding = medfilt2(outputThresholding,[3 3]);
% ^ this iteration is to get rid of any remaining symmetrical artifacts
% changes as of 28 November: commented-out 2nd median filter, changed
% threshold from "outputLogVert" to "image" (line ~ 101)
%TODO: FINISH Blobbing
profileVertical = any(outputThresholding > 0,1);
profileHorizontal = any(outputThresholding > 0,2);
xLeft = find(profileVertical, 1, 'first');
xRight = find(profileVertical, 1, 'last');
yTop = find(profileHorizontal, 1, 'first');
yBottom = find(profileHorizontal, 1, 'last');
%TODO: Angle calculation
outputAngle = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Display Block:
% Row 1:
subplot(3,3,1);
imshow(image);
title('Greyscale Image Input');
imwrite(image, 'Output-1-Greyscale.png');
subplot(3,3,2);
imshow(outputRoberts);
title('Roberts Omnidirectional Edge Detection');
imwrite(outputRoberts, 'Output-2-Roberts.png');
subplot(3,3,3);
imshow(outputVertEdge);
title('Vertical Edge Detection'); %TODO: test Prewitt -vs- Sobel (see lect.8)
imwrite(outputVertEdge, 'Output-3-VerticalEdgeDetection.png');
% Row 2:
subplot(3,3,4);
imshow(outputLaplacian);
title('Laplacian Edge Detection');
imwrite(outputLaplacian, 'Output-4-Laplacian3x3.png');
subplot(3,3,5);
imshow(outputLoG);
title('Laplacian of Gaussian (LoG)');
imwrite(outputLoG, 'Output-5-LoG.png');
subplot(3,3,6);
imshow(outputLoGVert);
title('Vertical-Edge-Biased LoG (Vertical Edge filter convolved with Gaussian)');%TODO: convolve vertical w/ Gaussian
imwrite(outputLoGVert, 'Output-6-LoGVert.png');
% Row 3:
subplot(3,3,7);
imshow(outputThresholding);
title('Thresholding'); %TODO: test graythresh() and imbinarize()
imwrite(outputThresholding, 'Output-7-Thresholding.png');
subplot(3,3,8);
title('Blobbing Output');
%imwrite(outputThresholding, 'Output-8-Blobbing.png');
imshow(outputThresholding);
rectangle('Position',[xLeft yTop (xRight-xLeft) (yBottom-yTop)],'EdgeColor','r');
%rectangle('Position',[((xLeft-xRight)/2 + 2) ((yBottom-yTop)/2 + 2) ((xLeft-xRight)/2 - 2) ((yBottom-yTop)/2 - 2)],'FaceColor','red');
subplot(3,3,9);
% TODO: find conservative angle from blob
title('Obstacle Solution Angle');
imshow(imageColor);
rectangle('Position',[xLeft yTop (xRight-xLeft) (yBottom-yTop)],'EdgeColor','r');
%imwrite(outputAngle, 'Output-9-XXXXXXXXXXXXXXXXXXX.png'); TODO: find way to write angle to text file