-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathLineFeature.m
310 lines (272 loc) · 9.99 KB
/
LineFeature.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
%LineFeature Line feature class
%
% This class represents a line feature.
%
% Methods::
% plot Plot the line segment
% seglength Determine length of line segment
% display Display value
% char Convert value to string
%
% Properties::
% rho Offset of the line
% theta Orientation of the line
% strength Feature strength
% length Length of the line
%
% Properties of a vector of LineFeature objects are returned as a vector.
% If L is a vector (Nx1) of LineFeature objects then L.rho is an Nx1 vector
% of the rho element of each feature.
%
% Note::
% - LineFeature is a reference object.
% - LineFeature objects can be used in vectors and arrays
%
% See also Hough, RegionFeature, PointFeature.
% Copyright (C) 1993-2011, by Peter I. Corke
%
% This file is part of The Machine Vision Toolbox for Matlab (MVTB).
%
% MVTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% MVTB 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 Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with MVTB. If not, see <http://www.gnu.org/licenses/>.
classdef LineFeature < handle
properties
rho_
theta_
strength_
length_
end
methods
function h = LineFeature(rho, theta, strength, length)
%LineFeature.LineFeature Create a line feature object
%
% L = LineFeature() is a line feature object with null parameters.
%
% L = LineFeature(RHO, THETA, STRENGTH) is a line feature object with
% the specified properties. LENGTH is undefined.
%
% L = LineFeature(RHO, THETA, STRENGTH, LENGTH) is a line feature object
% with the specified properties.
%
% L = LineFeature(L2) is a deep copy of the line feature L2.
if isa(rho, 'LineFeature')
% clone the passed object
obj = rho;
h.rho_ = obj.rho_;
h.theta_ = obj.theta_;
h.strength_ = obj.strength_;
h.length_ = obj.length_;
return
end
if nargin > 0
h.rho_ = rho;
h.theta_ = theta;
h.strength_ = strength;
if nargin > 3
h.length_ = length;
end
end
end
function val = rho(lines)
val = [lines.rho_];
end
function val = theta(lines)
val = [lines.theta_];
end
function val = strength(lines)
val = [lines.strength_];
end
function val = length(lines)
val = [lines.length_];
end
function display(h)
%LineFeature.display Display value
%
% L.display() displays a compact human-readable representation of the feature.
% If L is a vector then the elements are printed one per line.
%
% Notes::
% - This method is invoked implicitly at the command line when the result
% of an expression is a LineFeature object and the command has no trailing
% semicolon.
%
% See also LineFeature.char.
loose = strcmp( get(0, 'FormatSpacing'), 'loose');
if loose
disp(' ');
end
disp([inputname(1), ' = '])
if loose
disp(' ');
end
disp(char(h))
if loose
disp(' ');
end
end
function ss = char(lines)
%LineFeature.char Convert to string
%
% S = L.char() is a compact string representation of the line feature.
% If L is a vector then the string has multiple lines, one per element.
ss = [];
for line=lines
s = sprintf('theta=%g, rho=%g, strength=%g', ...
line.theta_, line.rho_, line.strength_);
if ~isempty(line.length)
s = strcat(s, sprintf(', length=%d', line.length_));
end
ss = strvcat(ss, s);
end
end
%fprintf(' intercept theta strength\n');
%disp(p);
function handles = plot(lines, varargin)
%LineFeature.plot Plot line
%
% L.plot() overlay the line on current plot.
%
% L.plot(LS) as above but the optional line style arguments LS are
% passed to plot.
%
% Notes::
% - If L is a vector then each element is plotted.
holdon = ishold;
hold on
% figure the x-axis scaling
scale = axis;
x = [scale(1):scale(2)]';
y = [scale(3):scale(4)]';
hl = [];
% plot it
for line=lines
%fprintf('theta = %f, d = %f\n', line.theta, line.rho);
if abs(cos(line.theta_)) > 0.5,
% horizontalish lines
%disp('hoz');
h = plot(x, -x*tan(line.theta_) + line.rho_/cos(line.theta_), varargin{:});
else
% verticalish lines
%disp('vert');
h = plot( -y/tan(line.theta_) + line.rho_/sin(line.theta_), y, varargin{:});
end
hl = [hl h];
end
if ~holdon,
hold off
end
if nargout > 0,
handles = hl;
end
figure(gcf); % bring it to the top
end
function out = seglength(lines, im_edge, gap)
%LineFeature.seglength Compute length of line segments
%
% The Hough transform identifies lines but cannot determine their length.
% This method examines the edge pixels in the original image and determines
% the longest stretch of non-zero pixels along the line.
%
% L2 = L.seglength(EDGE, GAP) is a copy of the line feature object with the
% property length updated to the length of the line (pixels). Small gaps,
% less than GAP pixels are tolerated.
%
% L2 = L.seglength(EDGE) as above but the maximum allowable gap is
% 5 pixels.
%
% See also ICANNY.
if nargin < 3
gap = 5;
end
out = [];
for L=lines
%fprintf('d=%f, theta=%f; ', L.rho, L.theta)
% find it's extreme points in the image
if abs(L.theta) < pi/4
xmin = 1; xmax = numcols(im_edge);
m = -tan(L.theta); c = L.rho/cos(L.theta);
ymin = round(xmin*m + c);
ymax = round(xmax*m + c);
else
ymin = 1; ymax = numrows(im_edge);
m = -1/tan(L.theta); c = L.rho/sin(L.theta);
xmin = round(ymin*m + c);
xmax = round(ymax*m + c);
end
line = bresenham(xmin, ymin, xmax, ymax);
line = line(line(:,2)>=1,:);
line = line(line(:,2)<=numrows(im_edge),:);
line = line(line(:,1)>=1,:);
line = line(line(:,1)<=numcols(im_edge),:);
contig = 0;
contig_max = 0;
total = 0;
missing = 0;
for pp=line'
pix = im_edge(pp(2), pp(1));
if pix == 0
missing = missing+1;
if missing > gap
contig_max = max(contig_max, contig);
contig = 0;
end
else
contig = contig+1;
total = total+1;
missing = 0;
end
%ee(pp(2), pp(1))=1;
end
contig_max = max(contig_max, contig);
%fprintf(' strength=%f, len=%f, total=%f\n', L.strength, contig_max, total);
o = LineFeature(L); % clone the object
o.length_ = contig_max;
out = [out o];
end
end
function P = points(lines, im_edge)
%LineFeature.points Return points on line segments
%
% P = L.points(EDGE) is the set of points that lie along the
% line in the edge image EDGE are determined.
%
% See also ICANNY.
% TODO
% refactor this code with segLength
P = [];
for L=lines
%fprintf('d=%f, theta=%f; ', L.rho, L.theta)
% find it's extreme points in the image
if abs(L.theta) < pi/4
xmin = 1; xmax = numcols(im_edge);
m = -tan(L.theta); c = L.rho/cos(L.theta);
ymin = round(xmin*m + c);
ymax = round(xmax*m + c);
else
ymin = 1; ymax = numrows(im_edge);
m = -1/tan(L.theta); c = L.rho/sin(L.theta);
xmin = round(ymin*m + c);
xmax = round(ymax*m + c);
end
line = bresenham(xmin, ymin, xmax, ymax);
line = line(line(:,2)>=1,:);
line = line(line(:,2)<=numrows(im_edge),:);
line = line(line(:,1)>=1,:);
line = line(line(:,1)<=numcols(im_edge),:);
i = sub2ind(size(im_edge), line(:,2), line(:,1));
k = im_edge(i);
P = [P line(k,:)'];
end
end
end % methods
end % Hough