-
Notifications
You must be signed in to change notification settings - Fork 0
/
Distance Transform.m
70 lines (61 loc) · 2.11 KB
/
Distance Transform.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
[rows, cols] = size(binary_img); % here binary_img is the matrix computed in gray to binary conversion program.
nonZeroPixels_cords = [];
pix_rows = 0;
% find Non-zero pixels coordinates
for row=1 : rows
for col=1: cols
if binary_img(row, col) == 1
pix_rows = pix_rows+1;
nonZeroPixels_cords(pix_rows, 1) = row;
nonZeroPixels_cords(pix_rows, 2) = col;
end
end
end
% Compute Euclidean Distance Transform
eucl_dist = zeros(rows, cols);
for row=1 : rows
for col=1: cols
if binary_img(row, col) ~= 1
eucl_dist(row, col) = sqrt((row - nonZeroPixels_cords(1,1))^2 + (col - nonZeroPixels_cords(1,2))^2);
for j = 2: pix_rows
temp_dist = sqrt((nonZeroPixels_cords(j,1) - row)^2 + (nonZeroPixels_cords(j,2) - col)^2);
if temp_dist < eucl_dist(row, col)
eucl_dist(row, col) = temp_dist;
end
end
end
end
end
% Compute D-4 Distance Transform
d4_dist = zeros(rows, cols);
for row=1 : rows
for col=1: cols
if binary_img(row, col) ~= 1
d4_dist(row, col) = abs(row - nonZeroPixels_cords(1,1)) + abs(col - nonZeroPixels_cords(1,2));
for j = 2: pix_rows
temp_dist = abs(nonZeroPixels_cords(j,1) - row) + abs(nonZeroPixels_cords(j,2) - col);
if temp_dist < d4_dist(row, col)
d4_dist(row, col) = temp_dist;
end
end
end
end
end
% Compute D-8 Distance Transform
d8_dist = zeros(rows, cols);
for row=1 : rows
for col=1: cols
if binary_img(row, col) ~= 1
d8_dist(row, col) = max(abs(row - nonZeroPixels_cords(1,1)), abs(col - nonZeroPixels_cords(1,2)));
for j = 2: pix_rows
temp_dist = max(abs(nonZeroPixels_cords(j,1) - row) ,abs(nonZeroPixels_cords(j,2) - col));
if temp_dist < d8_dist(row, col)
d8_dist(row, col) = temp_dist;
end
end
end
end
end
subplot(131); imshow(eucl_dist), title("Euclidean");
subplot(132); imshow(d4_dist), title("D-4");
subplot(133); imshow(d8_dist), title("D-8");