-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPE462_Project.m
196 lines (155 loc) · 6.88 KB
/
CPE462_Project.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
%{
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:
filterRoberts1 = [-1 0;
0 1];
filterRoberts2 = [0 -1;
1 0];
filterPrewittVert = [-1 0 1;
-1 0 1;
-1 0 1;];
filterLaplacian = [ 0 -1 0;
-1 4 -1;
0 -1 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!');
return;
else
disp(['Image selected: ', fullfile(pathName,fileName)]);
end
imageColor = imread(fullfile(pathName, fileName));
image = rgb2gray(im2double(imageColor));
%receive input for optics parameters
userInput = inputdlg({'Enter sensor width (mm):','Enter lens focal length (mm):'},'Optical Parameter Input',[1 50],{'35','50'});
%to catch if user did not input anything, check if length is equal to 2
if length(userInput) ~= 2
warndlg('Invalid Entry! Please enter a number.','Danger Will Robinson!');
return;
end
sensorSize = str2num(userInput{1}); %convert string to number
focalLength = str2num(userInput{2});
%check if length of each variable 1 to make sure user did not input a string
if length(sensorSize) ~= 1 | length(focalLength) ~= 1
warndlg('Invalid Entry! Please enter a number.','Danger Will Robinson!');
return;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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 = graythresh(image); %set threshold level
outputThresholding = imbinarize(outputLoGVert, threshold); %binarize image
[height, width] = size(outputThresholding); %obtain width and height of output
%next 4 lines: set border pixels to 0 to compensate for convolution
outputThresholding(1:border,:) = 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"
% Drawing the box:
profileVertical = any(outputThresholding, 1);
profileHorizontal = any(outputThresholding, 2);
xLeft = find(profileVertical, 1, 'first');
xRight = find(profileVertical, 1, 'last');
yTop = find(profileHorizontal, 1, 'first');
yBottom = find(profileHorizontal, 1, 'last');
position = [xLeft yTop (xRight-xLeft) (yBottom-yTop)];
obstacleX = (xRight + xLeft) / 2;
obstacleY = (yBottom + yTop) / 2;
% Creating the angle of view from above values... :
% AoV = 2 arctan (d / (2F)) where d = sensor width and F = focal length
% assuming infinite focus
angleOfView = rad2deg(2*atan(sensorSize/(2*focalLength)));
% now, we convert to degrees per pixel column:
[heightImage, widthImage] = size(image); %use image instead of color image because size() is affected by the total dimensions
degColumns = (angleOfView / widthImage);
% now, we find the angle to the obstacle (L vs R) :
obstacleAngle = round((degColumns*(obstacleX-(widthImage/2))),2);
% ^^^ + is right of center ; - is left of center
if obstacleX ~= 0 | obstacleY ~= 0 %if an obstacle is detected
outputObstacleBoundary = insertShape(imageColor,'rectangle',position,'LineWidth',5,'Color','red');
outputObstacleBoundary = insertText(outputObstacleBoundary, [obstacleX obstacleY], num2str(obstacleAngle), 'TextColor', 'red', 'BoxColor', 'white', 'FontSize', 20, 'AnchorPoint', 'Center', 'BoxOpacity', 0.7);
else %if no obstacle is detected
outputObstacleBoundary = imageColor;
position = [0 0 1 1]; %draw a 1px box in top left hand corner
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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');
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 filter convolved with Gaussian');
imwrite(outputLoGVert, 'Output-6-LoGVert.png');
% Row 3:
subplot(3,3,7);
imshow(outputThresholding);
title('Thresholding');
imwrite(outputThresholding, 'Output-7-Thresholding.png');
subplot(3,3,8);
imshow(outputThresholding);
title('Blobbing Output');
rectangle('Position',position,'EdgeColor','r');
subplot(3,3,9);
imshow(outputObstacleBoundary);
title('Angle to Obstacle Centroid');
imwrite(outputObstacleBoundary, 'Output-9-ObstacleBoundary.png');