You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: episodes/01-opening-and-checking-an-image.md
+39-94Lines changed: 39 additions & 94 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -255,89 +255,62 @@ plt.show()
255
255
256
256
## Proprietary formats
257
257
258
-
Images can come in many formats, but for many of the common ones such as TIFF,
259
-
PNG and BMP, skimage is smart enough to know how to read each one.
258
+
Modern microscopes save files in vendor-specific formats (like .czi, .nd2, or .lif). While skimage can handle standard TIF(F)s, these complex files contain vital metadata, for example, pixel spacing information, that we need for accurate analysis.
260
259
261
-
Some image formats are associated with specific instruments or equipment and
262
-
require specialised packages to open. Depending on your system, these may
263
-
already be available via `import` the same as any other Python package. If
264
-
not, then these should be installed into whatever Python instance you are
265
-
using.
266
260
267
-
If using JupyterHub or JupyterLab, go to 'New' -> 'Terminal'. This will open
268
-
a shell session in a new browser tab, where you can run `pip install` commands.
269
-
270
-
### Carl Zeiss .czi
271
-
272
-
Images in .czi format can be opened with the `czifile` library. In the Terminal
273
-
you opened above:
261
+
### The Universal Adapter: BIOIO
274
262
275
-
$ pip install czifile
263
+
Instead of installing a different library for every microscope brand, we recommend BIOIO. It acts as a consistent interface for almost any biological image format, allowing you to use the same commands regardless of the file source.
276
264
277
-
### Nikon .nd2
278
-
279
-
$ pip install nd2
280
-
281
-
### Imaris .ims
265
+
If using JupyterHub or JupyterLab, go to 'New' -> 'Terminal'. This will open
266
+
a shell session in a new browser tab, where you can run `pip install` commands.
282
267
283
-
$ pip install imaris-ims-file-reader
268
+
::: callout
269
+
### Advanced: Scaling up with Dask
270
+
Once you are a bit more established as a Python user, the integration of BIOIO with Dask becomes a major advantage. It allows for "Lazy Loading," where the computer only reads the specific pixels you ask for. This is essential for analyzing massive datasets that are larger than your computer's RAM.
271
+
:::
284
272
285
-
### Leica .lif
273
+
:::: challenge
274
+
## Exercise 4: Reading with BIOIO
286
275
287
-
$ pip install readlif
276
+
Load the bioio package and use it to read the test file 'data/Ersi_organoid_WT2.nd2'.
288
277
278
+
1. Use print(img.dims) to check the dimensions. What does each letter represent?
279
+
2. Use get_image_data to extract a single 2D frame for the first channel (C=0) at the middle Z-slice (Z=13).
289
280
290
-
::::::::::::::::::::::::::::::::::::: challenge
291
-
## Exercise 4: Proprietary formats
281
+
::: solution
282
+
```python
283
+
from bioio import BioImage
292
284
293
-
Load the [nd2](https://www.talleylambert.com/nd2/#installation) package and use it to read
294
-
the test file 'Ersi_organoid_WT2.nd2'. Install it if you need to.
285
+
#Load the image object
286
+
img = BioImage('data/Ersi_organoid_WT2.nd2')
295
287
296
-
What axes are present in the image? Which look most likely to be the X and
297
-
Y axes? Use `imshow` to display a single frame from the image.
If not already present, the nd2 package can be installed in the Terminal with
301
-
`pip install nd2`. According to its linked documentation, it has
302
-
an `imread` function that works in a similar way to the one in skimage, and returns a numpy
303
-
multidimensional array that we can work with the same way we have before:
292
+
# Grab a specific slice (Channel 0, Z-slice 13)
293
+
pixel_data = img.get_image_data("YX", C=0, Z=13)
304
294
305
-
```python
306
-
import nd2
307
-
image = nd2.imread('data/Ersi_organoid_WT2.nd2')
308
-
image.shape
309
-
# returns: (27, 3, 512, 512)
310
-
```
311
-
312
-
`img.shape` shows that there are four axes, `(27, 3, 512, 512)`. The latter two numbers look like
313
-
the X and Y axes, while the second number looks like a number of colour channels. The first number
314
-
looks like either a time series or a Z axis. We can show a single frame with:
295
+
import matplotlib.pyplot as plt
296
+
plt.imshow(pixel_data)
315
297
316
-
```python
317
-
plt.imshow(image[0, 0, :, :])
318
298
```
319
299
320
300
{alt='ND2 image'}
321
301
322
302
:::::::::::::::::::::::::::::::::
323
303
::::::::::::::::::::::::::::::::::::::::::::::::
324
304
325
-
As discussed above in exercise 4, it may be difficult to distinguish a time series from a Z axis.
326
-
You may also notice that here the X/Y axes are the latter two numbers, but in other examples
327
-
above, the X/Y axes were the first two. This demonstrates the diversity and general lack of
328
-
consistency in image formatting, and how it's usually a good idea to find out as much as you can
329
-
about the image from documentation and metadata before processing it.
330
-
331
305
## Altering the lookup table
332
306
333
-
Now that we've been able to open an image, we can start to explore it and display
334
-
it in different ways.
307
+
Now that we have extracted a specific 2D slice of data using BIOIO, we can explore different ways to display it.
335
308
336
-
The `imshow()` function can take extra arguments in addition to the image to display. One
337
-
of these is called `cmap`, which can apply alternate lookup tables (a.k.a. colour maps):
309
+
The imshow() function can take an argument called cmap, which applies a "Lookup Table" (LUT). This maps the numerical intensity values in your image to specific colors on your screen.
338
310
339
311
```python
340
-
plt.imshow(image[0, 0, :, :], cmap='viridis')
312
+
# Using the pixel_data we extracted from the BIOIO object earlier
313
+
plt.imshow(pixel_data, cmap='viridis')
341
314
```
342
315
343
316
Skimage uses lookup tables from the plotting library matplotlib. A list of available tables
@@ -427,45 +400,17 @@ position 0 (the start).
427
400
428
401
### Pixel size
429
402
430
-
Pixels are an approximation of an object - knowing that something is 50 pixels long and
431
-
50 pixels wide doesn't tell us anything about its actual size. To make any real-world
432
-
measurements on the image, we need the image's **pixel size**.
433
-
434
-
To get this, it is necessary to read the image's **metadata**. For this we need a different
435
-
library, imageio. There are a couple of different places we can look:
436
-
403
+
To make real-world measurements, we need the image's pixel size. While older methods required digging through complex metadata dictionaries, BIOIO makes this simple:
437
404
```python
438
-
import imageio
439
-
meta = imageio.v3.immeta('data/FluorescentCells_3channel.tif')
0 commit comments