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] = HTEllipse2(x_dir, y_dir, edges, radius_range)
6 [rows, columns] = size(edges);
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) = [];
17 indexes_edges = find(edges)';
20 for alpha = alpha_range
21 rot_mat = [cos(alpha) -sin(alpha); sin(alpha) cos(alpha)];
22 rot_mat_inv = [cos(-alpha) -sin(-alpha); sin(-alpha) cos(-alpha)];
23 for r1 = radius_range(1):radius_range(2)
24 for r2 = radius_range(1):radius_range(2)
25 acc = zeros(rows, columns);
27 [y, x] = ind2sub(s, i);
28 v = [-x_dir(i); -y_dir(i)];
44 theta = 2*pi + 2 * atan(...
45 (-r1 - r2*t*sqrt((r1^2+r2^2*t^2)/(r2^2*t^2)))...
50 (-r1 + r2*t*sqrt((r1^2+r2^2*t^2)/(r2^2*t^2)))...
53 C = round([x; y] - rot_mat * [r1*cos(theta); r2*sin(theta)]);
54 if C(1) < columns && C(1) > 0 && C(2) < rows && C(2) > 0
55 acc(C(2),C(1)) = acc(C(2),C(1)) + 1;
61 if acc(y, x) > acc_votes(y, x)
62 acc_votes(y, x) = acc(y, x);
63 acc_radius1(y, x) = r1;
64 acc_radius2(y, x) = r2;
65 acc_alpha(y, x) = alpha;
73 acc_votes = imgaussfilt(acc_votes, 1.0);