Add some tests on different variations of Hough algorithm.
[master-thesis.git] / src / Tests_hough / HTCircleMa.m
1 % Hough Transform for circles.
2 % x_dir : the x component of the normalized gradient
3 % y_dir : the y component of the normalized gradient
4 % edges : a binary image with edge = 1
5 % radius_range is a vector : [min, max]
6 function [acc_votes, acc_radius] = HTCircleMa(x_dir, y_dir, edges, radius_range)
7
8     [rows, columns] = size(edges);
9     
10     acc_votes = zeros(rows, columns);
11     acc_radius = zeros(rows, columns);    
12     
13     indexes_edges = find(edges)';
14     s = size(edges);
15     
16     for r = radius_range(1):radius_range(2)
17         acc = zeros(rows, columns);
18         for i = indexes_edges
19             [y, x] = ind2sub(s, i);      
20             
21             x0 = round(x + x_dir(i) * r);
22             y0 = round(y + y_dir(i) * r);
23             
24             if x0 < columns && x0 > 0 && y0 < rows && y0 > 0
25                 acc(y0, x0) = acc(y0, x0) + 1;
26             end
27         end
28         
29         for x = 1:columns
30             for y = 1:rows
31                 if acc(y, x) > acc_votes(y, x)
32                     acc_votes(y, x) = acc(y, x);
33                     acc_radius(y, x) = r;
34                 end
35             end
36         end        
37     end
38     
39     acc_votes = imgaussfilt(acc_votes, 1.0);
40 end
41