Newer
Older
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Image annotation tool\n",
"This notebook shows how the annotation interface can be used to create masks for images"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import qim3d\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib as mpl\n",
"import numpy as np\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load 2D example image\n",
"img = qim3d.examples.blobs_256x256\n",
"\n",
"# Display image\n",
"plt.imshow(img)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start annotation tool\n",
"interface = qim3d.gui.annotation_tool.Interface()\n",
"interface.max_masks = 4\n",
"\n",
"# We can directly pass the image we loaded to the interface\n",
"interface.launch(img=img)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# When 'prepare mask for download' is pressed once, the mask can be retrieved with the get_result() method\n",
"mask = interface.get_result()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Check the obtained mask"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print (f\"Original image shape..: {img.shape}\")\n",
"print (f\"Mask image shape......: {mask.shape}\")\n",
"print (f\"\\nNumber of masks: {np.max(mask)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Show the masked regions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"nmasks = np.max(mask)\n",
"fig, axs = plt.subplots(nrows=1, ncols=nmasks+2, figsize=(12,3))\n",
"\n",
"# Show original image\n",
"axs[0].imshow(img)\n",
"axs[0].set_title(\"Original\")\n",
"axs[0].axis('off')\n",
"\n",
"\n",
"# Show masks\n",
"cmap = mpl.colormaps[\"rainbow\"].copy()\n",
"cmap.set_under(color='black') # Sets the background to black\n",
"axs[1].imshow(mask, interpolation='none', cmap=cmap, vmin=1, vmax=nmasks+1)\n",
"axs[1].set_title(\"Masks\")\n",
"axs[1].axis('off')\n",
"\n",
"# Show masked regions\n",
"for idx in np.arange(2, nmasks+2):\n",
" mask_id = idx-1\n",
" submask = mask.copy()\n",
" submask[submask != mask_id] = 0\n",
" \n",
" masked_img = img.copy()\n",
" masked_img[submask==0] = 0\n",
" axs[idx].imshow(masked_img)\n",
" axs[idx].set_title(f\"Mask {mask_id}\")\n",
"\n",
" axs[idx].axis('off')\n",
"\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}