-
Notifications
You must be signed in to change notification settings - Fork 0
/
slope_LS.m
executable file
·83 lines (61 loc) · 1.06 KB
/
slope_LS.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
function [ B Xrange Yrange ] = slope_LS( BW, lmin )
%{
calculate slope by least square method.
(y = ax + b)
B(N,D) :
B(:,1): b (intercept)
B(:,2): a (slope)
Xrange : x range of line
Yrange : y range of line
%}
%Black-and-white inverted
BW = not(BW);
%labeling
L = bwlabel(BW);
num_region = max(L(:));
nr = 0;
for ic=1:num_region
[y x] = find(L==ic);
xmin = min(x); xmax = max(x);
ymin = min(y); ymax = max(y);
dx = xmax-xmin;
dy = ymax-ymin;
l = sqrt( dx*dx + dy*dy );
%fprintf( '%3d l=%f', ic, l );
if l < lmin
continue
end
%least square regression (1st order)
X1 = [ ones(length(x),1) x ];
Y = y;
try
b = (X1'*X1)\(X1'*Y);
catch
continue
end
xr = [ xmin xmax ]';
xx = [ ones(length(xr),1) xr ];
yr = xx*b;
dx = xr(2) - xr(1);
dy = yr(2) - yr(1);
l = sqrt( dx*dx + dy*dy );
if l < lmin
continue
end
nr = nr+1;
B(nr,:) = b;
Xrange(nr,:) = xr;
Yrange(nr,:) = yr;
%debug
%{
BW2 = zeros( size(BW) );
for ii=1:length(x)
BW2(y(ii), x(ii)) = 255;
end
figure
imshow( uint8(BW2) );
hold on
plot( xr, yr, '-r');
pause
%}
end