Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GoogleCodeExporter opened this issue Oct 10, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant