|
1 | 1 | import numpy as np |
| 2 | + |
2 | 3 | from elf.segmentation.embeddings import embedding_pca |
3 | 4 | from nifty.tools import blocking |
4 | 5 | from skimage.transform import resize |
5 | 6 |
|
| 7 | +from .util import ImageEmbeddings |
| 8 | + |
6 | 9 |
|
7 | 10 | # |
8 | 11 | # PCA visualization for the image embeddings |
9 | 12 | # |
10 | 13 |
|
11 | | -def compute_pca(embeddings): |
| 14 | +def compute_pca(embeddings: np.ndarray) -> np.ndarray: |
12 | 15 | """Compute the pca projection of the embeddings to visualize them as RGB image. |
| 16 | +
|
| 17 | + Args: |
| 18 | + embeddings: The embeddings. For example predicted by the SAM image encoder. |
| 19 | +
|
| 20 | + Returns: |
| 21 | + PCA of the embeddings, mapped to the pixels. |
13 | 22 | """ |
14 | 23 | if embeddings.ndim == 4: |
15 | 24 | pca = embedding_pca(embeddings.squeeze()).transpose((1, 2, 0)) |
@@ -131,7 +140,18 @@ def _project_tiled_embeddings(image_embeddings): |
131 | 140 | return embedding_vis, scale |
132 | 141 |
|
133 | 142 |
|
134 | | -def project_embeddings_for_visualization(image_embeddings): |
| 143 | +def project_embeddings_for_visualization( |
| 144 | + image_embeddings: ImageEmbeddings |
| 145 | +) -> tuple[np.ndarray, tuple[float, ...]]: |
| 146 | + """Project image embeddings to pixel-wise PCA. |
| 147 | +
|
| 148 | + Args: |
| 149 | + image_embeddings: The image embeddings. |
| 150 | +
|
| 151 | + Returns: |
| 152 | + The PCA of the embeddings. |
| 153 | + The scale factor for resizing to the original image size. |
| 154 | + """ |
135 | 155 | is_tiled = image_embeddings["input_size"] is None |
136 | 156 | if is_tiled: |
137 | 157 | embedding_vis, scale = _project_tiled_embeddings(image_embeddings) |
|
0 commit comments