bioimage-analysis-fundamentals.md 13.3 KB
Newer Older
Christian Tischer's avatar
Christian Tischer committed
1 2
# Bioimage analysis fundamentals

Christian Tischer's avatar
Christian Tischer committed
3
## Pixel values, coordinates, and data types
Christian Tischer's avatar
Christian Tischer committed
4 5 6 7 8 9 10 11 12 13

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
    image -> pixel [label="  has many"];
    pixel -> value;
    pixel -> indices;
    pixel -> coordinates;
    indices -> calibration;
    calibration -> coordinates;
Christian Tischer's avatar
Christian Tischer committed
14
    image -> calibration [label="  can have"];
Christian Tischer's avatar
Christian Tischer committed
15 16 17 18 19 20
    pixel -> voxel [label="  3D"];   
  }
'/>

### Activity

Christian Tischer's avatar
Christian Tischer committed
21 22 23 24
* Open image: `xy_8bit__nuclei_noisy_different_intensity.tif`
	* Explore different ways to inspect pixel values and indices
	* Add image calibration
	* Check where the calibration is visible
Christian Tischer's avatar
Christian Tischer committed
25
	
Christian Tischer's avatar
Christian Tischer committed
26 27
### Formative assessment

Christian Tischer's avatar
Christian Tischer committed
28
True or false?
Christian Tischer's avatar
Christian Tischer committed
29

Christian Tischer's avatar
Christian Tischer committed
30 31 32 33 34
* Pixel coordinates are always integer values.
* Changing the image calibration changes the pixel values.
* Pixel coordinates depend on image calibration.
* Pixel indices are always positive integer values.
* The lowest pixel index of a 2D image always is `[1,1]`.
Christian Tischer's avatar
Christian Tischer committed
35

Christian Tischer's avatar
Christian Tischer committed
36 37 38 39 40 41 42
&nbsp;

&nbsp;

&nbsp;


Christian Tischer's avatar
Christian Tischer committed
43 44 45 46 47
## Image display

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
Christian Tischer's avatar
Christian Tischer committed
48 49 50 51
    LUT -> color;
    LUT -> brightness;
    min -> LUT;
    max -> LUT;
Christian Tischer's avatar
Christian Tischer committed
52
    "pixel value" -> LUT;
Christian Tischer's avatar
Christian Tischer committed
53 54 55 56
  }
'/>

```
Christian Tischer's avatar
Christian Tischer committed
57
brightness(pixel_value) = ( pixel_value * LUT_min ) / ( LUT_max - LUT_min )
Christian Tischer's avatar
Christian Tischer committed
58
0 <= brightness <= 1 
Christian Tischer's avatar
Christian Tischer committed
59
contrast = LUT_max * LUT_min 
Christian Tischer's avatar
Christian Tischer committed
60 61 62 63
``` 

### Activity

Christian Tischer's avatar
Christian Tischer committed
64 65 66
* Open image: `xy_8bit__nuclei_noisy_different_intensity.tif` 
* Change LUT settings
	* Appreciate that LUT settings do not affect image content.
Christian Tischer's avatar
Christian Tischer committed
67 68


Christian Tischer's avatar
Christian Tischer committed
69 70
### Formative Assessment

Christian Tischer's avatar
Christian Tischer committed
71 72 73 74
Fill in the blanks:

decrease, larger_than, increase, smaller_than 

Christian Tischer's avatar
Christian Tischer committed
75 76 77 78 79 80 81 82 83 84 85 86
* Pixels with values _____ the `LUT_max` will appear saturated. 
* Decreasing `LUT_max` while keeping `LUT_min` constant will _____ the contrast.
* Decreasing both `LUT_max` and `LUT_min` will _____ the overall brightness.
* Pixels with values _____ the `LUT_min` will appear black, when using a grayscale LUT.


&nbsp;

&nbsp;

&nbsp;

Christian Tischer's avatar
Christian Tischer committed
87

88

Christian Tischer's avatar
Christian Tischer committed
89 90 91 92 93
## Image math and pixel data types

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
94
    "data type" -> "pixel values" [label="  restricts"];
95 96
    "image math" -> "pixel values" [label="  changes"];
    "N-bit unsigned integer" -> "0, 1, ..., 2^N-1";
Christian Tischer's avatar
Christian Tischer committed
97 98
    "data type" -> float -> "..., -1031.0, ..., 10.5, ...";
    "data type" -> "...";
Christian Tischer's avatar
Christian Tischer committed
99
    "data type" -> "N-bit unsigned integer";
Christian Tischer's avatar
Christian Tischer committed
100 101 102 103 104
  }
'/>

### Motivation

Christian Tischer's avatar
Christian Tischer committed
105
It sometimes is necessary to change the numeric content of images. It is important to understand how to do this properly in order to avoid uncontrolled artifacts.
Christian Tischer's avatar
Christian Tischer committed
106 107 108 109 110 111 112 113 114 115 116 117 118

What are good reasons to change the pixel values in an image?

1. For intensity measurements, the image background (e.g. camera based offset) should be subtracted from all pixels.
2. For threshold based image segmentation (object detection), it helps to first filter noise in the image.
3. For intensity measurements, it helps to filter noise in the image.
4. The image appears to dark, multiplication of all pixels by a constant number is a means to make it brighter.
5. For uneven illumination (e.g. occuring in wide-field microscopy with large camera chips), one should do flat-field correction, which makes the intensity values even across the image.
6. Our microscope was broken. We took images on a replacement microscope. The pixel values were consistently higher than on our usual microscope. We multiplied the pixels on all images from the replacement microscope by a constant factor to make them comparable to our usual data.


### Activity: Pixel based background subtraction

119 120 121 122 123 124 125
* Open image: `xy_8bit__nuclei_noisy_different_intensity.tif`
* Appreciate the significant background intensity
* Measure pixel value at `[ 28, 35 ]` and `[ 28, 39 ]`
* Measure background intensity in below region:
    * upper left corner at `[ 20, 35 ]`
    * width = 10
    * height = 10
Christian Tischer's avatar
Christian Tischer committed
126 127 128
* Subtract the measured background intensity from each pixel.
* Measure pixel values again. 
* Observe that the resuls are incorrect.
Christian Tischer's avatar
Christian Tischer committed
129 130 131

Repeat above activity, but:

Christian Tischer's avatar
Christian Tischer committed
132
* After opening the image, convert its data type to floating point.
Christian Tischer's avatar
Christian Tischer committed
133 134 135

### Activity: Limitations of float

Christian Tischer's avatar
Christian Tischer committed
136 137 138 139
* Create an empty image
* Set all pixel values to 1000000000.0
* Add 1.0 to all pixel values
* Be shocked...
Christian Tischer's avatar
Christian Tischer committed
140 141 142 143 144

...it turns out that from 16777216 on you cannot represent all integers anymore within a float. 

### Formative Assessment

Christian Tischer's avatar
Christian Tischer committed
145
True or false?
Christian Tischer's avatar
Christian Tischer committed
146

Christian Tischer's avatar
Christian Tischer committed
147 148 149 150 151
* Subtracting 100 from 50 in a 8-bit image will result in -50.
* Adding 1 to 255 in a 8-bit image will result in 256.
* Subtracting 10.1 from 10.0 in a float image will result in -0.1
* Adding 1.0 to 255.0 in a float image will result in 256.0
* Adding 1000.0 to 1000000000.0 in a float image will result in 1000001000.0
Christian Tischer's avatar
Christian Tischer committed
152 153 154

### Learn more

Christian Tischer's avatar
Christian Tischer committed
155
* [Limitations of float](https://randomascii.wordpress.com/2012/02/13/dont-store-that-in-a-float/)
Christian Tischer's avatar
Christian Tischer committed
156

Christian Tischer's avatar
Christian Tischer committed
157 158 159 160 161 162
&nbsp;

&nbsp;

&nbsp;

163
## Pixel data type conversions
Christian Tischer's avatar
Christian Tischer committed
164 165 166 167

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
168 169
    "pixel_type_conversion" -> "pixel_values" [label="  can change"];
    "pixel_type_conversion" -> pixel_value_range [label="  changes"];
Christian Tischer's avatar
Christian Tischer committed
170 171 172 173 174 175 176
  }
'/>

### Motivation

What are good reasons to change the pixel data type of an image?

Christian Tischer's avatar
Christian Tischer committed
177 178 179 180
* a
* b
* c
* d
Christian Tischer's avatar
Christian Tischer committed
181 182 183

### Activity: 16-bit to 8-bit conversion

Christian Tischer's avatar
Christian Tischer committed
184 185
* Open image: `xy_16bit__two_values.tif`
* Convert to 8-bit
Christian Tischer's avatar
Christian Tischer committed
186 187 188

### Activity: 16-bit to float conversion

Christian Tischer's avatar
Christian Tischer committed
189 190
* Open image: `xy_16bit__two_values.tif`
* Convert to float
Christian Tischer's avatar
Christian Tischer committed
191 192 193

### Formative Assessment

Christian Tischer's avatar
Christian Tischer committed
194 195 196 197 198
True or false?

1. Changing pixel data type never changes pixel values.
2. Converting from 16-bit unsigned integer to float never changes the pixel values.
3. Changing from float to 16-bit unsigned integer never changes the pixel values.
Christian Tischer's avatar
Christian Tischer committed
199 200


Christian Tischer's avatar
Christian Tischer committed
201 202 203 204 205 206
&nbsp;

&nbsp;

&nbsp;

Christian Tischer's avatar
Christian Tischer committed
207 208
## Image segmentation overview

209

Christian Tischer's avatar
Christian Tischer committed
210 211 212
<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
Christian Tischer's avatar
Christian Tischer committed
213
    "intensity image" -> "binary image" -> "label image";
Christian Tischer's avatar
Christian Tischer committed
214 215 216 217 218 219
    "binary image" -> "background value";
    "background value" -> "0";
    "foreground value" -> "1";
    "foreground value" -> "255";
    "binary image" -> "foreground value";
    "label image" -> "pixel values" -> "object indices";
Christian Tischer's avatar
Christian Tischer committed
220 221 222
  }
'/>

Christian Tischer's avatar
Christian Tischer committed
223 224 225 226 227 228 229
&nbsp;

&nbsp;

&nbsp;


Christian Tischer's avatar
Christian Tischer committed
230 231 232

## Thresholding

Christian Tischer's avatar
Christian Tischer committed
233
In order to find objects in a image, the first step often is to determine whether a pixel is part of an object (foreground) or of the image background. In fluorescence microscopy this often can be achieved by thresholding.
Christian Tischer's avatar
Christian Tischer committed
234 235 236 237

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
Christian Tischer's avatar
Christian Tischer committed
238
    "intensity image" -> threshold;
Christian Tischer's avatar
Christian Tischer committed
239 240 241
    threshold -> "binary image";
    value -> ">= threshold" -> foreground;
    value -> "< threshold" -> background;
Christian Tischer's avatar
Christian Tischer committed
242 243 244 245
 }
'/>


Christian Tischer's avatar
Christian Tischer committed
246
### Activity: Threshold an image
Christian Tischer's avatar
Christian Tischer committed
247

Christian Tischer's avatar
Christian Tischer committed
248 249
* Open image: `xy_8bit__two_cells.tif`
* Convert the image to a binary image by means of thresholding.
Christian Tischer's avatar
Christian Tischer committed
250

Christian Tischer's avatar
Christian Tischer committed
251
### Formative assessment
Christian Tischer's avatar
Christian Tischer committed
252

Christian Tischer's avatar
Christian Tischer committed
253
True or false? Discuss with your neighbor!
Christian Tischer's avatar
Christian Tischer committed
254

Christian Tischer's avatar
Christian Tischer committed
255 256 257 258
* For each image there is only one correct threshold value.
* The result of thresholding is a binary image.
* A binary image can have three values: `-1, 0, +1`
* Values below the threshold are always set to `1`.
Christian Tischer's avatar
Christian Tischer committed
259

Christian Tischer's avatar
Christian Tischer committed
260 261 262 263 264 265 266
&nbsp;

&nbsp;

&nbsp;


Christian Tischer's avatar
Christian Tischer committed
267 268 269 270 271
## Connected components analysis

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
Christian Tischer's avatar
Christian Tischer committed
272 273
    "intensity image" -> "connected component analysis" -> "label image";
    connectivity -> "connected component analysis"; 
Christian Tischer's avatar
Christian Tischer committed
274 275 276 277 278 279
  }
'/>


### Activity: 2D connected components analysis

Christian Tischer's avatar
Christian Tischer committed
280 281 282 283
* Open image: `xy_8bit_binary__nuclei.tif`
* Perform connected components analysis
* Explore multi-color LUTs for object labelling
* Explore removing and joining labels
Christian Tischer's avatar
Christian Tischer committed
284 285 286 287 288 289


### Activity: 3D connected components analysis

Repeat above activity but use a 3D image:

Christian Tischer's avatar
Christian Tischer committed
290
* Open image: `xyz_8bit_binary__spots.tif`
Christian Tischer's avatar
Christian Tischer committed
291 292 293

### Formative assessment

Christian Tischer's avatar
Christian Tischer committed
294 295 296
Fill in the blanks:

less, more, 8, 255, 4, more.
Christian Tischer's avatar
Christian Tischer committed
297

Christian Tischer's avatar
Christian Tischer committed
298 299 300 301 302 303 304
* For a given input image there is only one correct connectivity.
* In 3D, pixels have _____ neighbors than in 2D.
* 8-connected connectivity results in _____ objects than 4-connected connectivity.
* In 3D, pixels have ____ non-diagonal neighbors.
* In 2D, pixels have ____ non-diagonal neighbors.
* A 8-bit label image can maximally have _____ objects.
* The maximum value in a label image is equal to or _____ than the number of objects.
Christian Tischer's avatar
Christian Tischer committed
305 306


Christian Tischer's avatar
Christian Tischer committed
307 308 309 310 311 312 313
&nbsp;

&nbsp;

&nbsp;


Christian Tischer's avatar
Christian Tischer committed
314 315 316 317 318
## Shape measurements

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
Christian Tischer's avatar
Christian Tischer committed
319
    "label image" -> shape_analysis -> table;
Christian Tischer's avatar
Christian Tischer committed
320 321
    table -> object_rows;
    table -> feature_columns;  
Christian Tischer's avatar
Christian Tischer committed
322 323 324 325 326 327
}
'/>


### Activity: Measure object shape parameters

Christian Tischer's avatar
Christian Tischer committed
328 329 330 331 332
* Open image: `xy_8bit_labels__four_objects.tif`
* Perform shape measurements and discuss their meanings.
* Color objects by their measurement values.
* Add a calibration to the image and check which shape measurements are affected.
* Draw a test image to understand the shape measurements even better.
Christian Tischer's avatar
Christian Tischer committed
333 334 335 336


### Formative assessment

Christian Tischer's avatar
Christian Tischer committed
337
Which statements are true? Discuss with your neighbor!
Christian Tischer's avatar
Christian Tischer committed
338

Christian Tischer's avatar
Christian Tischer committed
339 340 341 342 343
* Circularity is independent of image calibration.
* Area is independent of image calibration.
* Perimeter can strongly depend on spatial sampling.
* Volume can strongly depend on spatial sampling.
* Drawing test images to check how certain shape parameters behave is a good idea.
Christian Tischer's avatar
Christian Tischer committed
344 345 346

### Learn more

Christian Tischer's avatar
Christian Tischer committed
347
* Especially surface and perimeter measurements are affected by sampling and resolution (see for example: https://en.wikipedia.org/wiki/Coastline_paradox).
Christian Tischer's avatar
Christian Tischer committed
348

Christian Tischer's avatar
Christian Tischer committed
349 350 351 352 353 354 355
&nbsp;

&nbsp;

&nbsp;


Christian Tischer's avatar
Christian Tischer committed
356 357 358 359 360
## Object shape measurement workflow

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
Christian Tischer's avatar
Christian Tischer committed
361 362 363
    "intensity image" -> "binary image" [label="  threshold"];
    "binary image" -> "label image" [label="  connected components"];
    "label image" -> table [label="  measure_shape"];
Christian Tischer's avatar
Christian Tischer committed
364 365 366 367 368
}
'/>

### Activity: Segment objects and measure shapes

Christian Tischer's avatar
Christian Tischer committed
369 370 371
* Open image: `xy_8bit__two_cells.tif`
* Segment the cells and measure their shapes.
	* Devise code to automate the workflow.
Christian Tischer's avatar
Christian Tischer committed
372 373 374

### Formative assessment

Christian Tischer's avatar
Christian Tischer committed
375
Fill in below blanks, using these words:
Christian Tischer's avatar
Christian Tischer committed
376

Christian Tischer's avatar
Christian Tischer committed
377
equal_to, larger_than, smaller_than, binary, connected_component_analysis, thresholding
Christian Tischer's avatar
Christian Tischer committed
378

Christian Tischer's avatar
Christian Tischer committed
379 380 381 382 383
* A label image is the result of _____ .
* The number of pixels in a binary image is typically _____ the number of connected components. 
* The number of distinct values in a label image is _____ the number of objects (minus one).
* Converting an intensity image to a _____ image can be achieved by _____ .
* The number of connected components can be _____ the maximal label.
Christian Tischer's avatar
Christian Tischer committed
384

Christian Tischer's avatar
Christian Tischer committed
385 386 387 388 389 390 391
&nbsp;

&nbsp;

&nbsp;


Christian Tischer's avatar
Christian Tischer committed
392
## Intensity measurements
Christian Tischer's avatar
Christian Tischer committed
393

Christian Tischer's avatar
Christian Tischer committed
394
### Activity: Measure intensities in image regions
Christian Tischer's avatar
Christian Tischer committed
395

Christian Tischer's avatar
Christian Tischer committed
396 397 398 399 400 401 402 403
* Open image: `xy_float__h2b_bg_corr.tif`
* Measure for both nuclei:
	* Maximum intensity
	* Average intensity
	* Median intensity
	* Sum intensity
* Discuss the interpretation!
* Discuss where to measure!
Christian Tischer's avatar
Christian Tischer committed
404 405


Christian Tischer's avatar
Christian Tischer committed
406 407 408
### Activity: Intensity measurements without pixel based background correction

#### Motivation
Christian Tischer's avatar
Christian Tischer committed
409

Christian Tischer's avatar
Christian Tischer committed
410
There are several good reasons not to subtract the background from each pixel in an image: 
Christian Tischer's avatar
Christian Tischer committed
411

Christian Tischer's avatar
Christian Tischer committed
412 413
* It is a bit tricky to do it right, because one has to convert to float to accomodate floting point and negative values.
* If one has really big image data (TB) one would need (at least) another TB storage for the background corrected version of the image.
Christian Tischer's avatar
Christian Tischer committed
414

Christian Tischer's avatar
Christian Tischer committed
415
#### Workflow
Christian Tischer's avatar
Christian Tischer committed
416

Christian Tischer's avatar
Christian Tischer committed
417 418 419 420 421 422 423 424 425 426 427
* Open image: `xy_calibrated_8bit__two_nuclei_high_background.tif`
* Measure for both nuclei and a background region:
	* Maximum intensity
	* Average intensity
	* Median intensity
	* Sum intensity
* Discuss how to correct the intensities for the background
	* Appreciate that you also need the region areas for this task
	* Measure the region areas
		* Watch out: the image is calibrated!
		* Use the area for the correction.
Christian Tischer's avatar
Christian Tischer committed
428

Christian Tischer's avatar
Christian Tischer committed
429
### Formative assessment: Intensity measurements
Christian Tischer's avatar
Christian Tischer committed
430

Christian Tischer's avatar
Christian Tischer committed
431 432 433 434
Fill in below blanks with those words:

integrated, mean, number_of_pixels, decrease, increase, sum

Christian Tischer's avatar
Christian Tischer committed
435 436 437 438
* Average intensity is just another word for _____ intensity.
* The _____ intensity is equal to the mean intensity times the _____ in the measured region.
* In an 8-bit image, increasing the size of the measurement region can only _____ the sum intensity.
* In a float image, increasing the size of the measurement region can _____ the sum intensity. 
Christian Tischer's avatar
Christian Tischer committed
439

Christian Tischer's avatar
Christian Tischer committed
440 441 442 443 444 445 446
&nbsp;

&nbsp;

&nbsp;


Christian Tischer's avatar
Christian Tischer committed
447 448 449 450 451 452
## Convolution filters

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
    "intensity image" -> "convolution" -> "filtered image";
Christian Tischer's avatar
Christian Tischer committed
453 454
    "small image" -> size;
    "small image" -> "pixel values";
Christian Tischer's avatar
Christian Tischer committed
455
    "kernel" -> "small image" [label="  is"]; 
Christian Tischer's avatar
Christian Tischer committed
456 457 458 459 460 461
    "kernel" -> "convolution";
}
'/>

### Activity: Explore convolution filters

Christian Tischer's avatar
Christian Tischer committed
462 463 464 465 466 467 468
* Open image: `xy_8bit__nuclei_noisy_different_intensity.tif` 
* Try the result of different convolution filters, e.g.
	* https://en.wikipedia.org/wiki/Kernel_(image_processing)
	* Mean filter
	* Gaussian blur
	* Edge detection
* Appreciate that the results are (slightly) wrong within the 8-bit range of the input image.
Christian Tischer's avatar
Christian Tischer committed
469 470 471

### Activity: Use mean filter to facilitate image segmentation

Christian Tischer's avatar
Christian Tischer committed
472 473 474 475
* Open image: `xy_8bit__nuclei_noisy_different_intensity.tif` 
* Appreciate that you cannot readily threshold the image
* Apply a mean filter
* Threshold the filtered image
Christian Tischer's avatar
Christian Tischer committed
476 477 478

### Formative assessment

Christian Tischer's avatar
Christian Tischer committed
479 480
* Draw the kernel of a 3x3 mean filter.
* Draw three different kernels that enhance edges.
Christian Tischer's avatar
Christian Tischer committed
481 482 483
 
### Learn more

Christian Tischer's avatar
Christian Tischer committed
484
* https://en.wikipedia.org/wiki/Kernel_(image_processing)
Christian Tischer's avatar
Christian Tischer committed
485

Christian Tischer's avatar
Christian Tischer committed
486

Christian Tischer's avatar
Christian Tischer committed
487 488 489 490 491 492 493
&nbsp;

&nbsp;

&nbsp;


Christian Tischer's avatar
Christian Tischer committed
494
## Typical image analysis workflow
Christian Tischer's avatar
Christian Tischer committed
495

Christian Tischer's avatar
Christian Tischer committed
496
![image](/uploads/b4bdce17515908f40d858b35d5e9256e/image.png)
Christian Tischer's avatar
Christian Tischer committed
497

Christian Tischer's avatar
Christian Tischer committed
498 499 500 501 502 503 504
&nbsp;

&nbsp;

&nbsp;


Christian Tischer's avatar
Christian Tischer committed
505 506
## Recap

Christian Tischer's avatar
Christian Tischer committed
507
(Work in pairs of two)
Christian Tischer's avatar
Christian Tischer committed
508

Christian Tischer's avatar
Christian Tischer committed
509 510 511 512 513 514 515
* Take one A4 paper
* Draw a typical workflow: From intensity image to objects shape table.
* Write down what you remember (max. 3 facts) about:
	* Intensity measurements
	* Object shape measurements
	* Label image
	* Pixel data types
Christian Tischer's avatar
Christian Tischer committed
516 517 518