vhbdragon / aforge

Automatically exported from code.google.com/p/aforge
0 stars 0 forks source link

HoughLineTransformation unecessarily slow due to use of Math.Round #380

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

I discovered that a lot of the time in the HoughLineTransformation is spent in 
Math.Round to calculate the radius variable as an integer. In my use case the 
patch below doubles overall performance of the HoughLineTransformation.

Robin

Index: HoughLineTransformation.cs
===================================================================
--- HoughLineTransformation.cs  (revision 1729)
+++ HoughLineTransformation.cs  (working copy)
@@ -500,7 +500,8 @@
                             // for each Theta value
                             for ( int theta = 0; theta < houghHeight; theta++ )
                             {
-                                int radius = (int) Math.Round( cosMap[theta] * 
x - sinMap[theta] * y ) + halfHoughWidth;
+                                double dRadius = (cosMap[theta] * x - 
sinMap[theta] * y);
+                                int radius = Round(dRadius) + halfHoughWidth;

                                 if ( ( radius < 0 ) || ( radius >= houghWidth ) )
                                     continue;
@@ -700,5 +701,13 @@

             lines.Sort( );
         }
+
+        // Faster simple alternative to Math.Round for finding nearest 
integer. 
+        // May give different results for values very close to integer+0.5 or 
very large values
+        // Neither of which should be important in this context
+        private int Round(double value)
+        {
+            return (int)(value + (value > 0 ? 0.5 : -0.5));
+        }
     }
 }

Original issue reported on code.google.com by robin.w....@gmail.com on 14 Mar 2014 at 9:06