---
# System prepended metadata

title: Deskew

---

# Deskew

General programming/thinking flow on how to deskew.

### This diagram describes the big picture.
![](https://i.imgur.com/DoGyvmf.png)


1) We know that data acquisition happens on an angle, as described in this figure. 
![](https://i.imgur.com/nMDQT6W.png)
For more details of how the acquisition geometry works and why we need to deskew, please watch this video https://cbmf.hms.harvard.edu/avada_faq/deskewing/ (highly recommended)


if you compare these two figures carefully

| Figure 1 | Figure 2 | 
| -------- | -------- | 
| ![](https://i.imgur.com/NoIKwRb.png)    |     ![](https://i.imgur.com/2SPn18k.png) |


In the volume, we are translating each slice on the x-axis by some factor to make it look proper (deskew).

We calculate this factor as follow.

Known values from the microscope
- Aqusition angle = 31.8&deg;
- xy pixel size (dx) = 0.104 &micro;m
- step size (dz) = 0.4 &micro;m

Known values from tiff stack:
- nx = image_width
- ny = image_height
- nz = image_length (depth in z)

> shift_factor = dz * np.abs(np.cos(angle * np.pi / 180)) / dx

then we multiply it by the index of the slice to get the correct shift factor 

//considering i starts with 0
```
for i in range(nz):
    traslate_on_x(image[i], shift_factor * i)
```

- Here are the Cuda kernel and the kernel launch function
https://github.com/dmilkie/cudaDecon/blob/master/src/geometryTransform.cu
- This the deskew initiation https://github.com/dmilkie/cudaDecon/blob/8218b26fd345cd870acc008696dd7ce243996d9c/src/linearDecon.cpp#L656

- Here is a python wrapper for the same.
https://github.com/tlambert03/pycudadecon/blob/master/pycudadecon/affine.py