Add some tests on different variations of Hough algorithm.
[master-thesis.git] / src / Tests_hough / find_ellipses.m
1 function [ellipses] = find_ellipses(acc_votes, acc_radius_1, acc_radius_2, acc_alpha)
2     if ~exist('acc_radius_2', 'var')
3         acc_radius_2 = acc_radius_1;    
4     end
5
6     if ~exist('acc_alpha', 'var')
7         acc_alpha = zeros(size(acc_votes));
8     end
9
10     ellipses = {}; % struct('x0', 'y0', 'r1', 'r2', 'alpha')
11     max_votes = max(acc_votes(:));
12     min_votes = min(acc_votes(:));
13     threshold_votes = (max_votes - min_votes) * 0.7 + min_votes;
14     
15     acc_votes_suppressed = acc_votes;
16     while true
17         [votes, index_max] = max(acc_votes_suppressed(:));
18         if votes <= threshold_votes
19             break;
20         end
21         
22         acc_votes_suppressed(index_max) = 0; % Suppress the vote.
23         
24         [y, x] = ind2sub(size(acc_votes_suppressed), index_max);        
25         
26         % If the center ellipse isn't in a known ellipse then add it to 'ellipses'.
27         accept_ellipse = true;
28         for i = 1:length(ellipses)
29             ellipse = ellipses{i};
30 %             module = sqrt((x - ellipse.x)^2 + (y - ellipse.y)^2);
31 %             phi = asin((y - ellipse.y) / module);
32 %             
33 %             p = [cos(a) -sin(a); sin(a) cos(a)] * [ellipse.r1 * cos(phi); ellipse.r2 * sin(phi)];
34 %             module_ellipse = sqrt(p(1)^2 + p(2)^2);                
35             
36             a = ellipse.alpha;
37             x0 = ellipse.x0;
38             y0 = ellipse.y0;
39             r1 = ellipse.r1;
40             r2 =  ellipse.r2;
41             n = ((x - x0) * cos(a) + (y - y0) * sin(a))^2 / r1^2 + ((x - x0) * sin(a) - (y - y0) * cos(a))^2 / r2^2;
42             if n <= 1
43                 accept_ellipse = false;
44                 break;
45             end
46         end
47         
48         if accept_ellipse            
49             e = struct(...
50                 'x0', x,...
51                 'y0', y,...
52                 'r1', acc_radius_1(index_max),...
53                 'r2', acc_radius_2(index_max),...
54                 'alpha', acc_alpha(index_max));            
55             ellipses{end+1} = e;
56             fprintf('Ellipse : (x0: %.1f, y0: %.1f, r1: %.1f, r2: %.1f, alpha: %.2f)\n', e.x0, e.y0, e.r1, e.r2, e.alpha);
57         end
58     end
59 end
60