scipy.signal.sepfir2d 中的类型错误
我正在尝试计算光流的导数(参考在我之前的 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)。 有什么想法为什么会发生类型错误吗?
I'm attempting to calculate the derivatives of optical flow (as referenced in my previous SO question) and am running into a TypeError when performing the calculation.
I first read the video in using OpenCV and use its optical flow methods to find the velocities. Then I use the scipy.signal library to run a gaussian filter over the velocities and calculate the derivatives.
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 ... #
Unfortunately, at the call to sepfir2d, I get the following error:
TypeError: array cannot be safely cast to required type
The documentation on the Scipy website is extremely sparse, and I can't find many other examples of its use. All three arguments to sepfir2d are numpy arrays; velx is a matrix, and gxFilt and gFilt are both vectors of the same length (5 in this case, I think). Any thoughts why the type error is occurring?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量测试(查看 sepfir2d 的源代码根本没有帮助),事实证明问题在于我的 velx 和 vely 使用的是 32-位浮点原语,当它们需要是 64 位时。这解决了它。
After much testing (looking at the source for sepfir2d didn't help at all), it turns out the issue was in the fact that my
velx
andvely
were using 32-bit floating point primitives, when they needed to be 64-bit. That fixed it.