python - 优化点-圆距离法

标签 python opencv numpy optimization cython

我正在实现用于图像中圆检测的 RANSAC 算法。我分析了执行过程并得到:

13699392 function calls in 799.981 seconds

   Random listing order was used

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 {time.time}
   579810    0.564    0.000    0.564    0.000 {getattr}
   289905    2.343    0.000    8.661    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/blas.py:226(_get_funcs)
   579810    0.124    0.000    0.124    0.000 {method 'get' of 'dict' objects}
   289905    0.645    0.000    2.676    0.000 {map}
     2954    0.005    0.000    0.005    0.000 {method 'transpose' of 'numpy.ndarray' objects}
     2954    0.023    0.000    0.464    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/shape_base.py:179(vstack)
     2954    2.373    0.001    2.373    0.001 {method 'read' of 'cv2.VideoCapture' objects}
   579810    0.966    0.000    2.031    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/function_base.py:550(asarray_chkfinite)
   289905   10.164    0.000   24.316    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/basic.py:456(lstsq)
     2954    1.090    0.000    1.090    0.000 {normalize}
  1455433    3.827    0.000    3.827    0.000 {numpy.core.multiarray.array}
   579810    2.899    0.000    3.148    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/numerictypes.py:949(_can_coerce_all)
        1    0.000    0.000    0.000    0.000 {numpy.core.multiarray.empty}
     2954   32.544    0.011  795.875    0.269 git/tra-python-processer/tra/ransac.py:31(image_search)
   289905    0.714    0.000   38.644    0.000 git/tra-python-processer/tra/features.py:44(__init__)
   289905    2.157    0.000    2.157    0.000 {method 'randint' of 'mtrand.RandomState' objects}
        1    0.005    0.005    0.005    0.005 {VideoCapture}
   289905    1.026    0.000    1.026    0.000 {method 'astype' of 'numpy.generic' objects}
     2954    0.006    0.000    0.010    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/fromnumeric.py:495(transpose)
   289905   11.303    0.000   37.930    0.000 git/tra-python-processer/tra/features.py:48(__gen)
  3496584    0.343    0.000    0.343    0.000 {len}
     2954    0.344    0.000    0.344    0.000 {numpy.core.multiarray.concatenate}
     2954    3.214    0.001    3.214    0.001 {numpy.core.multiarray.where}
   869715    0.575    0.000    0.575    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/fromnumeric.py:2514(size)
   869715    0.778    0.000    2.278    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/numeric.py:394(asarray)
   289905  716.946    0.002  716.946    0.002 git/tra-python-processer/tra/features.py:89(points_distance)
     5908    0.015    0.000    0.031    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/numeric.py:464(asanyarray)
   289905    0.275    0.000    0.275    0.000 {isinstance}
   289905    0.342    0.000    9.003    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/lapack.py:255(get_lapack_funcs)
     5908    0.058    0.000    0.097    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/shape_base.py:60(atleast_2d)
   295813    0.089    0.000    0.089    0.000 {method 'append' of 'list' objects}
   289905    0.645    0.000    3.793    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/numerictypes.py:970(find_common_type)
     2954    0.221    0.000    0.221    0.000 {threshold}
        1    0.000    0.000    0.000    0.000 {method 'get' of 'cv2.VideoCapture' objects}
        1    0.000    0.000    0.000    0.000 git/tra-python-processer/tra/ransac.py:24(__init__)
     2954    0.009    0.000    0.009    0.000 {numpy.core.multiarray.zeros}
   579810    0.143    0.000    0.143    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/misc.py:126(_datacopied)
        1    0.201    0.201  799.981  799.981 git/tra-python-processer/tra/ransac.py:122(video_processing)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
     2954    1.528    0.001    1.528    0.001 {cvtColor}
   289905    1.280    0.000    5.346    0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/blas.py:182(find_best_blas_type)
   289905    0.198    0.000    0.198    0.000 {method 'index' of 'list' objects}

这是我第一次使用探查器,但据我所知,最重要的功能是 features.py:89(points_distance),它的实现非常简单:

def points_distance(self,points):
    d = n.abs(\
              n.sqrt(\
                     n.power(self.xc - points[:,0],2) + n.power(self.yc - points[:,1],2)
                     )\
              - self.radius
              )
    return d

有什么建议吗?也许 cython

最佳答案

points_distance中使用scipy.spatial.distance.cdist进行距离计算。

首先,使用纯 Python 和 numpy 优化您的代码。然后,如有必要,将关键部分移植到 Cython。由于许多函数被重复调用了大约 100000 次,因此对于这些部分,您应该从 Cython 获得一些加速。当然,除非计算瓶颈在距离计算中,否则会限制整体执行时间。

顺便说一下,您应该按 tottime 对分析器结果进行排序,这样它们更易于阅读。

关于python - 优化点-圆距离法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30750397/

相关文章:

python - 替换 numpy ndarray 中的字符(Python)

python - Google Cloud 上的 Tensorflow 问题

python - Python 协程可以不用 await 或 yield 实现吗?

python - 在 sklearn NearestNeighbor 中使用每个邻居一次

opencv - 使用 OpenCV 检测表格

python - LibSVM 预测不工作

Python TA-Lib 不适用于 Pandas 系列

python - 如何将QLineEdit添加到QMessageBox PyQt5

python - 即使使用 Auth Pre-Flight 的头,Python Falcon 的 CORS 失败

java - 将 openCV 添加到 eclipse 中的 java 构建路径