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