File size: 3,715 Bytes
8e6b3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from pystackreg import StackReg
import numpy as np
import imageio.v2 as iio
from PIL import Image

def get_sr_mode(mode_str): return {
    "TRANSLATION": StackReg.TRANSLATION,
    "RIGID_BODY": StackReg.RIGID_BODY,
    "SCALED_ROTATION": StackReg.SCALED_ROTATION,
    "AFFINE": StackReg.AFFINE,
    "BILINEAR": StackReg.BILINEAR,
}.get(mode_str, StackReg.RIGID_BODY)

def normalize_stack(stack):
    norm_stack = []
    for frame in stack:
        f = frame.astype(np.float32)
        low, high = np.percentile(f, (1, 99))
        f = np.clip(f, low, high)
        f = (f - f.min()) / (np.ptp(f) + 1e-8) * 255 if np.ptp(f) > 0 else np.zeros_like(f)
        norm_stack.append(f.astype(np.uint8))
    return np.stack(norm_stack)

def upscale(image, factor=3):
    return image.resize((image.width * factor, image.height * factor), Image.NEAREST)

def load_stack(file):
    stack = np.array(iio.mimread(file))
    if stack.ndim == 4 and stack.shape[-1] == 3:
        stack = np.mean(stack, axis=-1)
    return normalize_stack(stack)


##### Markdowns #####

citation_markdown = """
        ### 📘 Credits & Acknowledgments

        Register TIFF stacks using multiple alignment strategies.

        **App Author**: [Quentin Chappuis](https://github.com/qchapp)  
        **Pystackreg Author**: [Gregor Lichtenberg](https://github.com/glichtner)  
        🔗 [Pystackreg on GitHub](https://github.com/glichtner/pystackreg)  

        **Original Algorithm Author**: Philippe Thévenaz (EPFL)  
        The core algorithm was originally developed by Philippe Thévenaz and is described in the following publication:

        > P. Thévenaz, U.E. Ruttimann, M. Unser  
        > *A Pyramid Approach to Subpixel Registration Based on Intensity*  
        > *IEEE Transactions on Image Processing*, vol. 7, no. 1, pp. 27–41, January 1998.  
        > 🔗 [View paper](http://bigwww.epfl.ch/publications/thevenaz9801.html)
        """


documentation_markdown = """
        ### Overview

        This app provides three registration modes for 2D TIFF image stacks using the `pystackreg` library.

        ---

        ### 📚 Tab 1: Reference-Based Alignment

        Register a stack using a reference frame. You can:
        - Use a frame from the **same stack** as reference
        - Or upload an **external reference stack**

        1. Upload the stack you want to align.
        2. (Optional) Check "Use external reference stack" to align to a frame from another file.
        3. Choose the reference frame using the slider.
        4. (Optional) Choose transformation mode.
        5. Click **▶️ Align Stack**.
        6. Use sliders to browse original/aligned results and download the output.

        ---

        ### 🎯 Tab 2: Stack-Based Alignment

        Align one stack (moving) to another (reference).

        1. Upload both **reference** and **moving** stacks.
        2. (Optional) Choose transformation mode.
        3. Click **▶️ Register** to align.
        4. Browse and download registered stack.

        ---

        ### 🧩 Tab 3: Frame-to-Frame Alignment

        Align a **single frame to another** from the same stack.

        1. Upload a stack.
        2. Select **reference** and **moving** frames using sliders.
        3. Choose transformation mode.
        4. Click **▶️ Register Frame**.
        5. View/download the result.

        ---

        ### 🔄 Reset Buttons

        Each tab includes a **Reset Tab** button that clears inputs, outputs, and internal state.

        ---

        ### 🧠 Credits

        App developed by **Quentin Chapuis**  
        Library: [`pystackreg`](https://github.com/glichtner/pystackreg) by **Georg Lichtenberg**
        """