Add some tests on different variations of Hough algorithm.
[master-thesis.git] / src / Tests_hough / find_ellipses.m
diff --git a/src/Tests_hough/find_ellipses.m b/src/Tests_hough/find_ellipses.m
new file mode 100644 (file)
index 0000000..4c5e7a0
--- /dev/null
@@ -0,0 +1,60 @@
+function [ellipses] = find_ellipses(acc_votes, acc_radius_1, acc_radius_2, acc_alpha)
+    if ~exist('acc_radius_2', 'var')
+        acc_radius_2 = acc_radius_1;    
+    end
+
+    if ~exist('acc_alpha', 'var')
+        acc_alpha = zeros(size(acc_votes));
+    end
+
+    ellipses = {}; % struct('x0', 'y0', 'r1', 'r2', 'alpha')
+    max_votes = max(acc_votes(:));
+    min_votes = min(acc_votes(:));
+    threshold_votes = (max_votes - min_votes) * 0.7 + min_votes;
+    
+    acc_votes_suppressed = acc_votes;
+    while true
+        [votes, index_max] = max(acc_votes_suppressed(:));
+        if votes <= threshold_votes
+            break;
+        end
+        
+        acc_votes_suppressed(index_max) = 0; % Suppress the vote.
+        
+        [y, x] = ind2sub(size(acc_votes_suppressed), index_max);        
+        
+        % If the center ellipse isn't in a known ellipse then add it to 'ellipses'.
+        accept_ellipse = true;
+        for i = 1:length(ellipses)
+            ellipse = ellipses{i};
+%             module = sqrt((x - ellipse.x)^2 + (y - ellipse.y)^2);
+%             phi = asin((y - ellipse.y) / module);
+%             
+%             p = [cos(a) -sin(a); sin(a) cos(a)] * [ellipse.r1 * cos(phi); ellipse.r2 * sin(phi)];
+%             module_ellipse = sqrt(p(1)^2 + p(2)^2);                
+            
+            a = ellipse.alpha;
+            x0 = ellipse.x0;
+            y0 = ellipse.y0;
+            r1 = ellipse.r1;
+            r2 =  ellipse.r2;
+            n = ((x - x0) * cos(a) + (y - y0) * sin(a))^2 / r1^2 + ((x - x0) * sin(a) - (y - y0) * cos(a))^2 / r2^2;
+            if n <= 1
+                accept_ellipse = false;
+                break;
+            end
+        end
+        
+        if accept_ellipse            
+            e = struct(...
+                'x0', x,...
+                'y0', y,...
+                'r1', acc_radius_1(index_max),...
+                'r2', acc_radius_2(index_max),...
+                'alpha', acc_alpha(index_max));            
+            ellipses{end+1} = e;
+            fprintf('Ellipse : (x0: %.1f, y0: %.1f, r1: %.1f, r2: %.1f, alpha: %.2f)\n', e.x0, e.y0, e.r1, e.r2, e.alpha);
+        end
+    end
+end
+