1 回答
TA贡献1836条经验 获得超13个赞
答案是使用numba。这就是代码现在的样子:
import numpy as np
from numba import jit
@jit(nopython=True)
def houghLines(edges, dTheta, threshold):
imageShape = edges.shape
imageDiameter = (imageShape[0]**2 + imageShape[1]**2)**0.5
rhoRange = [i for i in range(int(imageDiameter)+1)]
thetaRange = [dTheta*i for i in range(int(-np.pi/(2*dTheta)), int(np.pi/dTheta))]
cosTheta = []
sinTheta = []
for theta in thetaRange:
cosTheta.append(np.cos(theta))
sinTheta.append(np.sin(theta))
countMatrixSize = (len(rhoRange), len(thetaRange))
countMatrix = np.zeros(countMatrixSize)
eds = []
for (x,y), value in np.ndenumerate(edges):
if value > 0:
eds.append((x,y))
for thetaIndex in range(len(thetaRange)):
theta = thetaRange[thetaIndex]
cos = cosTheta[thetaIndex]
sin = sinTheta[thetaIndex]
for x, y in eds:
targetRho = x*cos + y*sin
closestRhoIndex = int(round(targetRho))
countMatrix[closestRhoIndex, thetaIndex] += 1
lines = []
for (p,t), value in np.ndenumerate(countMatrix):
if value > threshold:
lines.append((p,thetaRange[t]))
return lines
这使它至少快了 50 倍。
添加回答
举报