1

我正在尝试计算光流的导数(如我之前的 SO 问题中所引用的),并且在执行计算时遇到了 TypeError。

我首先阅读了使用 OpenCV 的视频,并使用其光流方法来查找速度。然后我使用 scipy.signal 库对速度运行高斯滤波器并计算导数。

cv.CalcOpticalFlowLK(prev_frame, curr_frame, (11, 11), velx, vely)

# ... convert velx and vely to numpy arrays ...

# Set up the gaussian filter and its derivative.
sigmaBlur = 1
sigmaGrey = 4
gBlurSize = 2 * np.around(2.5 * sigmaBlur) + 1
x = np.mgrid[1:gBlurSize + 1] - np.around((gBlurSize + 1) / 2)
gFilt = np.exp(-(x ** 2) / (2 * (sigmaBlur ** 2)))
gFilt /= np.sum(gFilt)
gxFilt = (-x / (sigmaBlur ** 2)) * gFilt

# Now calculate the derivative of the velocity.
res = scipy.signal.sepfir2d(velx, gxFilt, gFilt)

# ... 3 more calls to sepfir2d ... #

不幸的是,在调用 sepfir2d 时,我收到以下错误:

TypeError: array cannot be safely cast to required type

Scipy 网站上的文档非常稀少,我找不到很多其他使用示例。sepfir2d 的所有三个参数都是 numpy 数组;velx 是一个矩阵,而 gxFilt 和 gFilt 都是相同长度的向量(我认为在这种情况下为 5)。有什么想法为什么会发生类型错误?

4

1 回答 1

1

经过大量测试(查看 sepfir2d 的源代码根本没有帮助),事实证明问题在于我velxvely正在使用 32 位浮点原语,而它们需要 64 位。那解决了它。

于 2011-11-01T18:54:31.773 回答