rank-filters.md 4.61 KB
Newer Older
Christian Tischer's avatar
Christian Tischer committed
1 2
# Rank filters

Christian Tischer's avatar
Christian Tischer committed
3 4 5 6 7 8
## Basic rank filters

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
	"rank filters" -> awesome [label="  are"];
Christian Tischer's avatar
Christian Tischer committed
9 10 11 12
	"rank filters" -> minimum; 
	minimum -> erosion [label="  aka"];
	"rank filters" -> maximum; 
	maximum -> dilation [label="  aka"];
Christian Tischer's avatar
Christian Tischer committed
13 14
	"rank filters" -> median;
	"rank filters" -> "size" [label="  have"];
Christian Tischer's avatar
Christian Tischer committed
15 16 17 18 19 20
  }
'/>

### Activity: Explore rank filters on binary images

- Open image: xy_8bit_binary__two_spots_different_size.tif
Christian Tischer's avatar
Christian Tischer committed
21
- Explore how structures grow and shrink, using erosion and dilation
Christian Tischer's avatar
Christian Tischer committed
22 23 24 25 26 27 28 29

### Activity: Explore rank filters on grayscale images

- Open image: xy_8bit__two_noisy_squares_different_size.tif
- Explore how a median filter
	- removes noise
	- removes small structures
	- preserves egdes
Christian Tischer's avatar
Christian Tischer committed
30
- Compare median filter to mean filter of same radius
Christian Tischer's avatar
Christian Tischer committed
31 32 33 34


### Formative assessment

Christian Tischer's avatar
Christian Tischer committed
35 36 37 38 39 40 41 42 43 44 45
True or false? Discuss with your neighbour!

1. Median filter is just another name for mean filter.
2. Small structures can completely disappear from an image when applying a median filter.

Fill in the blanks, using those words: shrinks, increases, decreases, enlarges.

1. An erosion _____ objects in a binary image. 
2. An erosion in a binary image _____ the number of foreground pixels.
3. A dilation in a grayscale image _____ the average intensity in the image.
4. A dilation _____ objects in a binary image.
Christian Tischer's avatar
Christian Tischer committed
46 47 48 49 50 51 52 53 54 55


## Morphological opening and closing

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
        "opening" -> "rank filter sequence" [label="  is"];
        "closing" -> "rank filter sequence" [label="  is"];
	"opening" -> "removes small structures";
Christian Tischer's avatar
Christian Tischer committed
56
	"closing" -> "fills small gaps";
Christian Tischer's avatar
Christian Tischer committed
57 58 59 60
  }
'/>

```
Christian Tischer's avatar
Christian Tischer committed
61
opening( image, r ) = dilation( erosion( image, r ), r )
Christian Tischer's avatar
Christian Tischer committed
62 63 64
```

```
Christian Tischer's avatar
Christian Tischer committed
65
closing( image, r ) = erosion( dilation( image, r ), r )
Christian Tischer's avatar
Christian Tischer committed
66 67 68 69 70 71
```


### Activity: Explore opening and closing on binary images

- Open image: xy_8bit_binary__for_open_and_close.tif
Christian Tischer's avatar
Christian Tischer committed
72 73 74
- Explore effects of morphological closing and opening:
	- closing can fill holes
	- closing can connect gaps
Christian Tischer's avatar
Christian Tischer committed
75
	- opening can remove thin structures
Christian Tischer's avatar
Christian Tischer committed
76 77 78 79


### Formative assessment

Christian Tischer's avatar
Christian Tischer committed
80
True of false? Discuss with your neighbour!
Christian Tischer's avatar
Christian Tischer committed
81

Christian Tischer's avatar
Christian Tischer committed
82 83 84
1. Morphological openings on binary images can decrease the number of foreground pixels.
2. Morphological closings on binary images never decreases the number of foreground pixels.
3. Performing a morphological closing a twice in a row does not make sense, because the second closing does not further change the image.   
Christian Tischer's avatar
Christian Tischer committed
85 86 87 88 89 90

## Top hat filter for local background subtraction

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
Christian Tischer's avatar
Christian Tischer committed
91 92
	"tophat" -> "rank filter sequence"; 
	"tophat" -> "local background subtraction";
Christian Tischer's avatar
Christian Tischer committed
93 94 95 96 97
  }
'/>


```
Christian Tischer's avatar
Christian Tischer committed
98
tophat( image ) = image - opening( image, r ) =  image - dilation( erosion( image, r), r )
Christian Tischer's avatar
Christian Tischer committed
99 100
```

Christian Tischer's avatar
Christian Tischer committed
101

Christian Tischer's avatar
Christian Tischer committed
102 103 104
### Activity: Explore tophat filter

- Open image: xy_8bit__spots_local_background.tif
Christian Tischer's avatar
Christian Tischer committed
105
- Use a tophat filter to remove local background
Christian Tischer's avatar
Christian Tischer committed
106 107 108

## Activity: Implement a tophat filter

Christian Tischer's avatar
Christian Tischer committed
109
- Devise code implementing a tophat filter, using minimum and maximum filters
Christian Tischer's avatar
Christian Tischer committed
110 111 112 113

## Activity: Explore tophat filter on biological data

- Open image: xy_16bit__autophagosomes.tif 
Christian Tischer's avatar
Christian Tischer committed
114 115 116
- Appreciate that you cannot readliy segment the spots.
- Use a tophat filter to remove local background.
- Threshold the spots in the tophat filtered image.
Christian Tischer's avatar
Christian Tischer committed
117

Christian Tischer's avatar
Christian Tischer committed
118
## Activity: Explore tophat filter on noisy data
Christian Tischer's avatar
Christian Tischer committed
119 120 121 122 123 124 125

- Open image: xy_8bit__spots_local_background_with_noise.tif 
- Use topHat filter to remove local background
- Appreciate that noise poses a challenge to the tophat filter

## Median filter for local background subtraction

Christian Tischer's avatar
Christian Tischer committed
126
<img src='https://g.gravizo.com/svg?
Christian Tischer's avatar
Christian Tischer committed
127 128
 digraph G {
    shift [fontcolor=white,color=white];
Christian Tischer's avatar
Christian Tischer committed
129
	"median" -> "local background" [label="  approximates"];
Christian Tischer's avatar
Christian Tischer committed
130 131
	"median" -> "radius" -> "> object width";
	"radius" -> "< spatial background frequency";
Christian Tischer's avatar
Christian Tischer committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  }
'/>


```
median_based_background_correction = image - median( image, r)
```

### Activity: Implement median based background subtraction

- Write code to implement a median based background subtraction


### Activity: Explore median filter for local background subtraction

- Open images: 
	- xy_8bit__spots_local_background.tif 
Christian Tischer's avatar
Christian Tischer committed
149 150
	- xy_8bit__spots_local_background_with_noise.tif 
- Use tophat filter to remove local background
Christian Tischer's avatar
Christian Tischer committed
151 152 153 154
- Devise code to implement a tophat filter using basic functions

### Formative assessment

Christian Tischer's avatar
Christian Tischer committed
155 156 157 158 159
Answer below questions. Discuss with your neighbour!

1. What could one do to close small gaps in a binary image? 
2. What could one do to remove small objects in a image?
3. What could you use for local background subtraction in a very noisy image?
Christian Tischer's avatar
Christian Tischer committed
160

Christian Tischer's avatar
Christian Tischer committed
161
## Learn more
Christian Tischer's avatar
Christian Tischer committed
162

Christian Tischer's avatar
Christian Tischer committed
163
- https://imagej.net/MorphoLibJ#Grayscale_morphological_filters
Christian Tischer's avatar
Christian Tischer committed
164 165 166