cv2.Filter2D 与 ndimage.convolve?

发布于 2025-01-10 03:32:52 字数 988 浏览 0 评论 0原文

我是图像处理新手,如果您觉得问题太简单,请原谅我。

我试图用 cv2.filter2D() 函数替换 ndimage.convolve() 进行卷积。

这是原始代码

ndimage.convolve(Gxx,f,origin=0)

这是替换函数

cv2.filter2D(Gxx,-1,cv2.flip(f,-1),borderType=cv2.BORDER_REFLECT,anchor=(-1,-1))

两个代码都返回不同的值,我尝试使用 ndimage.convolve 处的参数并将 origin 设置为 -1 然后返回准确的结果为cv2.filter2D 但这不是我想要的,因为我希望 ndimage.convolve 使用默认的原点为零。

在我再次研究了 filter2D 上的 opencv 文档后,我意识到

过滤函数实际上是计算相关性。如果你有一个 对称卷积核,数学表达式为 相关性和卷积是相同的。

如果内核不对称,则必须翻转内核并设置 锚点指向 (kernel.cols -anchor.x - 1, kernel.rows -anchor.y - 1)。这将计算实际的卷积。

所以我用 cv2.flip(f,-1) 双向翻转内核,但我不知道翻转内核后如何计算新的锚点,根据文档,我需要将锚点设置为(kernel.cols -anchor.x - 1, kernel.rows -anchor.y - 1)。我猜 kernel.cols 和 kernel.rows 是两个方向的内核大小,但是 anchor.xanchor.x 是什么呢? y 在这里意味着什么?它是内核的原始锚点吗?假设内核是 42 X 42,新的锚点是什么?提前致谢。

I am new to image processing, please forgive me if you find the question too simple.

I was trying to replace ndimage.convolve() with cv2.filter2D() function for convolution.

Here is the original code

ndimage.convolve(Gxx,f,origin=0)

Here is the replace function

cv2.filter2D(Gxx,-1,cv2.flip(f,-1),borderType=cv2.BORDER_REFLECT,anchor=(-1,-1))

Both of the code return different values, I try to play around with the parameter at ndimage.convolve and set origin to -1 then it return the exact result as the cv2.filter2D but this is not what I want, since I want ndimage.convolve to use the default origin which is zero.

After I study the opencv documentation again on filter2D I realize that

The filtering function actually calculates correlation. If you have a
symmetrical convolution kernel, the mathematical expressions for
correlation and convolution are the same.

If the kernel is not symmetric, you must flip the kernel and set the
anchor point to (kernel.cols - anchor.x - 1, kernel.rows - anchor.y -
1). This will calculate the actual convolution.

So I flip the kernel with cv2.flip(f,-1) in both direction, but I don't know how to calculate the new anchor point after flip the kernel, according to the documentation, I need to set anchor point to (kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1). I guess kernel.cols and kernel.rows is the kernel size in both direction but what does anchor.x and anchor.y means here? Is it original anchor point of the kernel? Assume the kernel is 42 X 42, what will be the new anchor point? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

作死小能手 2025-01-17 03:32:52

过滤器的锚点通常位于其中心。因此,对于 3x3 过滤器,当从零开始索引时,它是 (1, 1)。例如,一个简单的边缘过滤器:

1 0 -1
1 0 -1
1 0 -1

锚点位于中心 (1, 1) 是有意义的,因为当对图像中的特定点应用卷积时,它会计算左侧点和右侧点之间的差异就在它的直接附近。

现在让我们看看您的 42x42 内核。对于 41x41,中心将为 (20, 20),对于 43x43,中心将为 (21, 21)。

42x42 的内核具有均匀的边长,这使得它不对称。这就是为什么首选奇数边长的内核的原因之一。

对于这样的过滤器内核,锚点从中心移动到下一个有效坐标。例如,4x4 内核的中心位于 (1.5, 1.5),锚点位于 (2, 2)。

因此,在 42x42 内核的情况下,锚点将从中心 (20.5, 20.5) 移动到 (21, 21)。如果翻转内核,锚点也需要翻转:以点对称的方式,中心在 (20.5, 20.5) 到 (20, 20)。
的结果

这也是给定公式 kernel.cols -anchor.x - 1: 42 - 21 - 1 = 20

The anchor of a filter is typically at its center. So, for a 3x3 filter it is (1, 1) when indexing from zero. For example, a simple edge filter:

1 0 -1
1 0 -1
1 0 -1

It makes sense that the anchor is at the center (1, 1) because when the convolution is applied for a specific point in the image, it computes the difference between the points to the left and to the right in its direct neighborhood.

Now let's look at your 42x42 kernel. For 41x41 the center would be (20, 20) and for 43x43 it would be (21, 21).

The kernel 42x42 has even side lengths which makes it asymmetrical. This is one of the reasons why kernels with odd side lengths are preferred.

For a filter kernel like this, the anchor is shifted from the center to the next valid coordinate. For example, a 4x4 kernel has it's center at (1.5, 1.5) and anchor at (2, 2), accordingly.

So in your case of a 42x42 kernel, the anchor would be shifted to (21, 21) from center (20.5, 20.5). If you flip the kernel, the anchor needs to be flipped as well: in a point-wise symmetry with the center at (20.5, 20.5), to (20, 20).
This is also the result of the given formula kernel.cols - anchor.x - 1:

42 - 21 - 1 = 20

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