-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdifferentiation.py
46 lines (30 loc) · 906 Bytes
/
differentiation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np
import torch
"""
We use the roll function to compute the derivatives because we assume that the image is periodic.
"""
def D(img, axis):
if isinstance(img, np.ndarray):
return np.roll(img, -1, axis) - img
return torch.roll(img, -1, axis) - img
def D_transpose(img, axis):
if isinstance(img, np.ndarray):
return np.roll(img, 1, axis) - img
return torch.roll(img, 1, axis) - img
def Dx(img):
return D(img, 1)
def Dy(img):
return D(img, 0)
def Dx_transpose(img):
return D_transpose(img, 1)
def Dy_transpose(img):
return D_transpose(img, 0)
def grad(img):
if isinstance(img, np.ndarray):
return np.stack((Dx(img), Dy(img)), axis=0)
return torch.stack((Dx(img), Dy(img)), dim=0)
"""
The laplacian is $- \nabla^\top (\nabla I)$.
"""
def laplacian(img):
return -(Dx_transpose(Dx(img)) + Dy_transpose(Dy(img)))