我正在尝试计算光流的衍生物(如引用in my previous SO question)并在执行计算时遇到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
documentation on the Scipy website非常稀疏,我找不到其他许多使用示例。 sepfir2d的所有三个参数都是numpy数组; velx是一个矩阵,gxFilt和gFilt都是相同长度的向量(在这种情况下,我认为是5)。 有关出现类型错误的原因吗?
答案 0 :(得分:1)
经过大量测试(查看sepfir2d的源代码根本没有帮助),事实证明问题在于我的velx
和vely
使用的是32位浮点原语,当他们需要64位时。修好了。