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_radius1, acc_radius2, acc_alpha] = HTEllipse(img, radius_range)
6 [rows, columns] = size(img);
8 acc_votes = zeros(rows, columns);
9 acc_radius1 = zeros(rows, columns);
10 acc_radius2 = zeros(rows, columns);
11 acc_alpha = zeros(rows, columns);
13 alpha_range = linspace(0, pi / 2, 8);
14 alpha_range(end) = [];
16 phi_range = linspace(0, 2 * pi, 32);
19 indexes_non_zero = find(img)';
22 for alpha = alpha_range
23 transform_mat = [cos(alpha) -sin(alpha); sin(alpha) cos(alpha)];
24 for r1 = radius_range(1):radius_range(2)
25 for r2 = radius_range(1):radius_range(2)
26 acc = zeros(rows, columns);
27 for i = indexes_non_zero
28 [y, x] = ind2sub(s, i);
30 p0 = [x; y] - transform_mat * [r1 * cos(phi); r2 * sin(phi)];
34 if x0 < columns && x0 > 0 && y0 < rows && y0 > 0
35 acc(y0, x0) = acc(y0, x0) + 1;
42 if acc(y, x) > acc_votes(y, x)
43 acc_votes(y, x) = acc(y, x);
44 acc_radius1(y, x) = r1;
45 acc_radius2(y, x) = r2;
46 acc_alpha(y, x) = alpha;
54 acc_votes = imgaussfilt(acc_votes, 1.0);