[Feature] 3d implementation of the sobel filter
See original GitHub issue🚀 Feature
Hello, I’m using kornia to work with MRI images for the auto-differentiation feature but out of the deep-learning context. It came super usefull to not re-implement every thing.
So I found that the sobel filter had not been implemented for the 3d images case. As I need it I propose here an implementation, more for the users that like me may need it, waiting for the devs to publish one. What I have done is taking much inspiration from the diff implementation of the spatial gradient. To my understanding it should works with the auto-differentiation form backpropagation. However I am not sure that is the best and faster implementation and feel free to comment or improve.
My proposal :
def get_sobel_kernel_3d():
""" Return a [3,1,3,3,3] sobel kernel"""
return torch.tensor(
[
[[[-1,0,1],
[-2,0,2],
[-1,0,1]],
[[-2,0,2],
[-4,0,4],
[-2,0,2]],
[[-1,0,1],
[-2,0,2],
[-1,0,1]]],
[[[-1,-2,-1],
[0,0,0],
[1,2,1]],
[[-2,-4,-2],
[0,0,0],
[2,4,2]],
[[-1,-2,-1],
[0,0,0],
[1,2,1]]],
[[[-1,-2,-1],
[-2,-4,-2],
[-1,-2,-1]],
[[0,0,0],
[0,0,0],
[0,0,0]],
[[1,2,1],
[2,4,2],
[1,2,1]]]
]).unsqueeze(1)
def spacialGradient_3d(image):
""" Implementation of a sobel 3d spatial gradient inspired by the kornia library.
:param image: Tensor [B,1,H,W,D]
:return: Tensor [B,3,H,W,D]
:Example:
H,W,D = (50,75,100)
image = torch.zeros((H,W,D))
mX,mY,mZ = torch.meshgrid(torch.arange(H),
torch.arange(W),
torch.arange(D))
mask_rond = ((mX - H//2)**2 + (mY - W//2)**2).sqrt() < H//4
mask_carre = (mX > H//4) & (mX < 3*H//4) & (mZ > D//4) & (mZ < 3*D//4)
mask_diamand = ((mY - W//2).abs() + (mZ - D//2).abs()) < W//4
mask = mask_rond & mask_carre & mask_diamand
image[mask] = 1
grad_image = spacialGradient_3d(image[None,None])
"""
# sobel kernel is not implemented for 3D images yet in kornia
# grad_image = SpatialGradient3d(mode='sobel')(image)
kernel = get_sobel_kernel_3d().to(image.device).to(image.dtype)
spatial_pad = [1,1,1,1,1,1]
image_padded = F.pad(image,spatial_pad,'replicate').repeat(1,3,1,1,1)
grad_image = F.conv3d(image_padded,kernel,padding=0,groups=3,stride=1)
return grad_image
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
implement 3d sobel operator - Stack Overflow
I visualized all three 3D sobel operators to make it more intuitive. Here the sobel filters in X direction, Y direction and Z...
Read more >ECE5780 Lab 3 3D Edge Detection
Filter (Prefilter smoothing (LPF)) remove noise. 2. Enhancement: Gradient Estimation 1'st derivative,. 3. Detection: select an appropriate threshold.
Read more >Sobel operator - Wikipedia
The Sobel–Feldman operator is based on convolving the image with a small, separable, and integer-valued filter in the horizontal and vertical directions and...
Read more >3D Sobel operator in the x, y, and z directions. - ResearchGate
We present a new edge based approach for salt dome detection in migrated 3D seismic data. The proposed algorithm overcomes the drawbacks of...
Read more >Sobel filter for edge detection of hexagonally sampled 3D ...
The computational cost of edge detection can be reduced by exploring other sampling approaches instead of the regular rectangular sampling commonly used.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@antonfrancois Please go ahead for the PR!
Nice repo you got! I particularly like the visualization of the gradient arrows you did. We were actually thinking of making tutorials to demonstrate the differentiability of the low-level computer vision operations. So I think it could be a great match to your examples!
\cc @edgarriba @ducha-aiki Take a look at this.
As you can see here https://github.com/kornia/kornia/blob/43a9469b93f61aa91e0a779d3abfe2eb68a9eb22/kornia/filters/kernels.py#L308 at line 326, the filter is not implemented.
For examples you can check my repository : https://github.com/antonfrancois/Demeter_metamorphosis There are some example in jupyter notebooks, where I use kornia for basic operations. However it is a bit hidden. What kind of tutorials do you think about ?