scipy.signal.sepfir2d 中的类型错误

发布于 2024-12-13 00:56:24 字数 1258 浏览 0 评论 0原文

我正在尝试计算光流的导数(参考在我之前的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多孤肩上扛 2024-12-20 00:56:24

经过大量测试(查看 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 and vely were using 32-bit floating point primitives, when they needed to be 64-bit. That fixed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文