-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtomasiKanade.m
81 lines (75 loc) · 2.29 KB
/
tomasiKanade.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
% @param array img The input image to filter.
% @param integer dim_mask The mask's size. We prefer a odd size.
% @param array img_out The output image with the resultant curves detection.
% The algorithm is similar to Harris Detection.
function [img_out] = tomasiKanade(img, dim_mask)
[n_row, n_col]=size(img);
img_out=double(img);
% Matrix n_row*n_col initialization.
Dx=zeros(n_row,n_col);
Dy=Dx;
A=Dx;
B=Dx;
C=Dx;
marker=255; %White color
threshold=10^6.5;
% Computes derivatives
a = ceil(dim_mask/2);
b = ceil(dim_mask/2);
% Old part
% for i=a:n_row-a
% Dx(i,:)=img_out(i+1,:)-img_out(i-1,:);
% end
% for j=b:n_col-b
% Dy(:,j)=img_out(:,j+1)-img_out(:,j-1);
% end
for i=a:n_row-a
for j=b:n_col-b
Dx(i,j)=img_out(i,j+1)-img_out(i,j-1);
Dy(i,j)=img_out(i+1,j)-img_out(i-1,j);
end
end
% Compute A(x,y) and B(x,y) of Harris.
A=computeMatrix(A, Dx, dim_mask);
B=computeMatrix(B, Dy, dim_mask);
% Compute C(x,y)
C=A.*B;
for i=1:n_row
for j=1:n_col
% Harris Matrix
M=[A(i,j), C(i,j); C(i,j), B(i,j)];
% Eighvalues
lambda=eig(M);
% Min value of all POSITIVE eigenvalues
lambda_min=min(lambda(lambda>0));
if isempty(lambda_min)
lambda_min = 0;
end
if lambda_min>=threshold
img_out(i,j)=marker;
end
end
end
end
% @param array M The input matrix.
% @param array M The input matrix.
% @param integer dim_mask The mask's size.
% @param array matrix The output matrix.
function [matrix] = computeMatrix(M, D, dim_mask)
[n_row, n_col]=size(M);
% Compute half-width and half-height of the mask.
a = ceil(dim_mask/2);
b = ceil(dim_mask/2);
for i=a:n_row-a
for j=b:n_col-b
for ii=(i-a+1):(i+a-1)
for jj=(j-b+1):(j+b-1)
% Sum the previous value with the square correspondent
% derivatives.
M(i,j)=M(i,j)+D(ii,jj)^2;
end
end
end
end
matrix=M;
end