Skip to content
Snippets Groups Projects

Implemented beta version of annoation tool + unit test

1 file
+ 49
34
Compare changes
  • Side-by-side
  • Inline
+ 49
34
%% Cell type:markdown id: tags:
# Demo of using the annotation tool in qim3d
# Image annotation tool
This notebook shows how the annotation interface can be used to create masks for images
%% Cell type:code id: tags:
``` python
import qim3d
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
%matplotlib inline
```
%% Cell type:code id: tags:
``` python
# Load 2D example image
blobs = qim3d.examples.blobs_256x256
img = qim3d.examples.blobs_256x256
plt.imshow(blobs)
# Display image
plt.imshow(img)
plt.show()
```
%% Output
<matplotlib.image.AxesImage at 0x7f1bad098af0>
%% Cell type:code id: tags:
``` python
# Start annotation tool with image
# Start annotation tool
interface = qim3d.gui.annotation_tool.Interface()
interface.launch(img=blobs)
```
%% Output
Running on local URL: http://127.0.0.1:7860
interface.max_masks = 4
# We can directly pass the image we loaded to the interface
interface.launch(img=img)
```
%% Cell type:code id: tags:
``` python
# When 'prepare mask for download' is pressed once, the mask can be retrieved with the get_result() method
mask = interface.get_result()
```
%% Cell type:code id: tags:
``` python
# Conform that they are of the same shape
print(f'{blobs.shape=}, {mask.shape=}')
```
%% Output
%% Cell type:markdown id: tags:
blobs.shape=(256, 256), mask.shape=(256, 256)
## Check the obtained mask
%% Cell type:code id: tags:
``` python
# Mask is saved as a one-channnel image where each non-zero value corresponds to the given mask
print(f'{np.unique(mask)=}')
print (f"Original image shape..: {img.shape}")
print (f"Mask image shape......: {mask.shape}")
print (f"\nNumber of masks: {np.max(mask)}")
```
%% Output
%% Cell type:markdown id: tags:
np.unique(mask)=array([0, 1, 2], dtype=uint8)
## Show the masked regions
%% Cell type:code id: tags:
``` python
plt.imshow(mask)
```
%matplotlib inline
%% Output
nmasks = np.max(mask)
fig, axs = plt.subplots(nrows=1, ncols=nmasks+2, figsize=(12,3))
<matplotlib.image.AxesImage at 0x7f1b944cf820>
# Show original image
axs[0].imshow(img)
axs[0].set_title("Original")
axs[0].axis('off')
# Show masks
cmap = mpl.colormaps["rainbow"].copy()
cmap.set_under(color='black') # Sets the background to black
axs[1].imshow(mask, interpolation='none', cmap=cmap, vmin=1, vmax=nmasks+1)
axs[1].set_title("Masks")
axs[1].axis('off')
# Show masked regions
for idx in np.arange(2, nmasks+2):
mask_id = idx-1
submask = mask.copy()
submask[submask != mask_id] = 0
masked_img = img.copy()
masked_img[submask==0] = 0
axs[idx].imshow(masked_img)
axs[idx].set_title(f"Mask {mask_id}")
axs[idx].axis('off')
plt.show()
```
Loading