Add some tests on different variations of Hough algorithm.
[master-thesis.git] / src / Tests_hough / test_hough_ellipses2.m
diff --git a/src/Tests_hough/test_hough_ellipses2.m b/src/Tests_hough/test_hough_ellipses2.m
new file mode 100644 (file)
index 0000000..41be4a8
--- /dev/null
@@ -0,0 +1,49 @@
+function test_hough_ellipses2(images_path, image_name)
+    i = imread([images_path image_name]);
+
+    green = i(:,:,2);
+    green_filtered = imgaussfilt(green, 1.0);
+    otsu = ~im2bw(green_filtered, graythresh(green_filtered));
+
+    se = strel('rectangle', [3 3]);
+    edges = imdilate(otsu, se) - imerode(otsu, se);
+
+    s = [-1 0 1; -2 0 2; -1 0 1];
+    x_edge = conv2(double(green_filtered), s, 'valid');
+    y_edge = conv2(double(green_filtered), s', 'valid');
+
+    magnitudes = sqrt(x_edge .^ 2 + y_edge .^ 2);
+
+    % Unit gradient.
+    x_dir = x_edge ./ magnitudes; 
+    y_dir = y_edge ./ magnitudes;
+
+    disp('Applying Houg...');
+    tic
+    [acc_votes, acc_radius1, acc_radius2, acc_alpha] = HTEllipse2(x_dir, y_dir, edges(2:end-1, 2:end-1), [20, 40]);
+    toc
+
+    disp('Finding ellipses...');
+    tic
+    ellipses = find_ellipses(acc_votes, acc_radius1, acc_radius2, acc_alpha);
+    toc
+    
+    name = ['Hough ellipses 2 - ' image_name];
+    show_figures(name, {...
+        {green_filtered, 'original filtered with a gaussian, sigma=1'},...
+        {edges, 'edges (otsu -> dilate - erode)'},...
+        {expand_matrix(x_edge), 'composante x du gradient'},...
+        {expand_matrix(acc_votes), 'votes'}}, green, ellipses);
+    
+    f = figure;
+    img = green_filtered;
+    grayscale = linspace(0, 1, 255);
+    colormap([grayscale' grayscale' grayscale']);
+    img(~edges) = 0;
+    image(img);
+    axis image;
+    hold on;
+    [X, Y] = meshgrid(1:size(green, 2), 1:size(green, 1));
+    quiver(X, Y, expand_matrix(x_dir), expand_matrix(y_dir), 'Color', 'yellow');
+    savefig(['results/' name ' - gradient.fig']);
+end
\ No newline at end of file