# robust\_2d\_unwrap

The following is an implementation of the algorithm proposed in "Robust 2D phase unwrapping algorithm based on the transport of intensity equation" ([DOI:10.1088/1361-6501/aaec5c](https://doi.org/10.1088/1361-6501/aaec5c)) by Zixin Zhao, Hangying Zhang, Zhaoxian Xiao, Hubing Du, Yiying Zhuang, Chen Fan and Hong Zhao.

In particular, the code is directly ported from the [MatLab implementation](https://www.mathworks.com/matlabcentral/fileexchange/68493-robust-2d-phase-unwrapping-algorithm) to be python compatible. 


## Installation with pip
```bash
pip install robust_2d_unwrap
```

## Basic Usage

```python
import numpy as np
from robust_2d_unwrap import unwrap
import matplotlib.pyplot as plt

# Generate a surface, with wrapped phase for every point
h,w = 640, 480
y0, x0 = (h - 1) / 2, (w - 1) / 2
y = np.arange(h)
x = np.arange(w)
Y, X = np.meshgrid(y,x, indexing='ij')
hmap = 4 * np.exp( -( ((Y - y0)**2) / (2 * 100**2) + ((X - x0)**2) / (2 * 100**2) ))
abs_phase = hmap * 2 * np.pi
wrapped_phase = (abs_phase + np.pi) % (2 * np.pi) - np.pi

# add some noise
wrapped_phase += np.random.randn(h,w) # mu=0, std=1 rad

# visualise wrapped phase
plt.imshow(wrapped_phase) # 2d headmap
plt.title('Wrapped Phase w/ Noise')
plt.show()

# wrap phase
unwrapped_phase, _ = unwrap(wrapped_phase)

# visualize result
plt.imshow(unwrapped_phase)
plt.title('Unwrapped Phase')
plt.show()

# Noise will persist, but it will not break the unwrapping
# algorithm.
```
