Python中的3D Sobel算法? [英] 3d sobel algorithm in python?

查看:179
本文介绍了Python中的3D Sobel算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python中计算3d sobel过滤器.我在下面有一个很好的2d图像代码.

I'm trying to calculate a 3d sobel filter in python. I have a pretty good code for 2d image which is below.

顺便说一句.我的原始图片是uint8类型.

btw. my original image is uint8 type.

    preSobel = preSobel.astype('int32')
    dx = ndimage.sobel(preSobel, 0)  # horizontal derivative
    dy = ndimage.sobel(preSobel, 1)  # vertical derivative
    mag = numpy.hypot(dx, dy)  # magnitude
    mag *= 255.0 / numpy.max(mag)  # normalize (Q&D)
    img[i,:,:]=mag

但是,根据我对用于计算2d的 Wiki页面的理解,我应该乘以1d sobel结果而不是hypot:confused

but from my understanding of the wiki page for calculating 2d, i should have multiplied the 1d sobel results rather than hypot :confused

无论如何,要转到3d,我想我需要在每个轴上计算1d sobel,然后将其相乘,但我不确定...那里有没有可以更快地计算3d sobel的库?

anyway, to go to 3d, I guess I need to calculate 1d sobel on each axis and then multiply all but I'm not sure... Is there any library out there that calculates 3d sobel faster ?

推荐答案

首先,参考您的维基百科链接:那里的乘法是指构造sobel卷积核的方法,而不是最终结果.

First, in reference to your wikipedia link: The multiplication there is referring to the way to construct the sobel convolution kernel, not the end result.

对于2D sobel滤波器,您需要一个内核来获取x方向的导数,而另一个内核要获取Y方向的导数,例如

For a 2D sobel filter you need a kernel to get the derivate in x direction, and another kernel to get the derivate in Y direction, e.g.

从本质上讲,这是您的两个命令所执行的操作,因此,如果您使用的是numpy,则无需自己构造这些内核.

This is essentially what your two commands do, so if you are using numpy you do not need to construct these kernels yourself.

dx = ndimage.sobel(preSobel, 0)  # horizontal derivative
dy = ndimage.sobel(preSobel, 1)  # vertical derivative

对于3D情况,现在需要3个内核进行3个操作,其中1个用于dx,dy,dz. 链接的Wiki部分告诉您如何通过乘以组件来构造内核.例如,用于dZ的成品sobel内核是一个3x3x3的矩阵,如下所示:

Now for the 3D case you need 3 operations with 3 kernels, one for dx, dy, dz. The linked wiki section is telling you how to construct the kernels by multiplying components. The finished sobel kernel for dZ for example is a 3x3x3 matrix that looks like this:

要获得幅度,您仍然必须随后取平方导数(斜边)的平方根.

To get the magnitude you still have to take the square root of the squared derivatives (the hypotenuse) afterwards.

我没有numpy,但据我可以从文档 ndimage sobel命令可以处理任意多个维度,因此再次提供了内核:

I do not have numpy but as far as I can tell from the documentation the ndimage sobel command can deal with any number of dimensions, so again, the kernels are already provided:

dx = ndimage.sobel(your3Dmatrix, 0)  # x derivative
dy = ndimage.sobel(your3Dmatrix, 1)  # y derivative
dz = ndimage.sobel(your3Dmatrix, 2)  # z derivative

现在斜边命令可能仅包含两个参数,因此您将不得不寻找另一种有效计算mag = sqrt(dx dx + dy dy + dz * dz)的方法. 但是NumPy应该拥有您所需的一切.

now the hypotenuse command probably only take 2 parameters, so you will have to find another way to efficiently calculate mag = sqrt(dxdx + dydy + dz*dz) . But NumPy should have everything you need for that.

更新

实际上,如果您只对幅度感兴趣,请 numpy中有一个完整的功能:

Actually, if you are only interested in the magnitude anyway, there is a complete function in numpy for this:

 mag = generic_gradient_magnitude(your3Dmatrix, sobel)

这篇关于Python中的3D Sobel算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆