rank-filters.md 5.36 KB
Newer Older
Christian Tischer's avatar
Christian Tischer committed
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
# Neighborhood filters

<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
	"neighbourhood filter" -> "central neighbourhood pixel" [label="  replaces"]; 
	"neighbourhood filter" -> "size" [label="  has"];
	"neighbourhood filter" -> "shape" [label="  has"];
	"neighbourhood filter" -> "convolution filters";
 	"neighbourhood filter" -> "rank filters";
}
'/>


|   |   |   |   |   |   |   |   |
|---|---|---|---|---|---|---|---|
| NC  | NC  | NC  |   |   |   |   |   |
| NC  | C, NC  | NC  |   |   |   |   |   |
| NC  | NC  | NC  |   |   |   |   |   |
|   |   |   |   | NB  | NB  | NB  |   |
|   |   |   |   |  NB | B, NB| NB  |   |
|   |   |   |   |  NB |  NB |  NB |   |
|   |   |   |   |   |   |   |   |





Christian Tischer's avatar
Christian Tischer committed
29 30
# Rank filters

Christian Tischer's avatar
Christian Tischer committed
31 32 33 34
## Basic rank filters
<img src='https://g.gravizo.com/svg?
 digraph G {
    shift [fontcolor=white,color=white];
Christian Tischer's avatar
Christian Tischer committed
35
	"rank filters" -> "neighbourhood filters" [label="  are"];
Christian Tischer's avatar
Christian Tischer committed
36 37 38 39
	"rank filters" -> minimum; 
	minimum -> erosion [label="  aka"];
	"rank filters" -> maximum; 
	maximum -> dilation [label="  aka"];
Christian Tischer's avatar
Christian Tischer committed
40 41
	"rank filters" -> median;
	"rank filters" -> "size" [label="  have"];
Christian Tischer's avatar
Christian Tischer committed
42 43 44
  }
'/>

Christian Tischer's avatar
Christian Tischer committed
45 46


Christian Tischer's avatar
Christian Tischer committed
47 48 49
### Activity: Explore rank filters on binary images

- Open image: xy_8bit_binary__two_spots_different_size.tif
Christian Tischer's avatar
Christian Tischer committed
50
- Explore how structures grow and shrink, using erosion and dilation
Christian Tischer's avatar
Christian Tischer committed
51 52 53 54 55 56 57 58

### 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
59
- Compare median filter to mean filter of same radius
Christian Tischer's avatar
Christian Tischer committed
60 61 62 63


### Formative assessment

Christian Tischer's avatar
Christian Tischer committed
64 65 66 67 68 69 70 71 72 73 74
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
75 76 77 78 79 80 81 82 83 84


## 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
85
	"closing" -> "fills small gaps";
Christian Tischer's avatar
Christian Tischer committed
86 87 88 89
  }
'/>

```
Christian Tischer's avatar
Christian Tischer committed
90
opening( image, r ) = dilation( erosion( image, r ), r )
Christian Tischer's avatar
Christian Tischer committed
91 92 93
```

```
Christian Tischer's avatar
Christian Tischer committed
94
closing( image, r ) = erosion( dilation( image, r ), r )
Christian Tischer's avatar
Christian Tischer committed
95 96 97 98 99 100
```


### 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
101 102 103
- Explore effects of morphological closing and opening:
	- closing can fill holes
	- closing can connect gaps
Christian Tischer's avatar
Christian Tischer committed
104
	- opening can remove thin structures
Christian Tischer's avatar
Christian Tischer committed
105 106 107 108


### Formative assessment

Christian Tischer's avatar
Christian Tischer committed
109
True of false? Discuss with your neighbour!
Christian Tischer's avatar
Christian Tischer committed
110

Christian Tischer's avatar
Christian Tischer committed
111 112 113
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
114 115 116 117 118 119

## 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
120 121
	"tophat" -> "rank filter sequence"; 
	"tophat" -> "local background subtraction";
Christian Tischer's avatar
Christian Tischer committed
122 123 124 125 126
  }
'/>


```
Christian Tischer's avatar
Christian Tischer committed
127
tophat( image ) = image - opening( image, r ) =  image - dilation( erosion( image, r), r )
Christian Tischer's avatar
Christian Tischer committed
128 129
```

Christian Tischer's avatar
Christian Tischer committed
130

Christian Tischer's avatar
Christian Tischer committed
131 132 133
### Activity: Explore tophat filter

- Open image: xy_8bit__spots_local_background.tif
Christian Tischer's avatar
Christian Tischer committed
134
- Use a tophat filter to remove local background
Christian Tischer's avatar
Christian Tischer committed
135 136 137

## Activity: Implement a tophat filter

Christian Tischer's avatar
Christian Tischer committed
138
- Devise code implementing a tophat filter, using minimum and maximum filters
Christian Tischer's avatar
Christian Tischer committed
139 140 141 142

## Activity: Explore tophat filter on biological data

- Open image: xy_16bit__autophagosomes.tif 
Christian Tischer's avatar
Christian Tischer committed
143 144 145
- 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
146

Christian Tischer's avatar
Christian Tischer committed
147
## Activity: Explore tophat filter on noisy data
Christian Tischer's avatar
Christian Tischer committed
148 149 150 151 152 153 154

- 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
155
<img src='https://g.gravizo.com/svg?
Christian Tischer's avatar
Christian Tischer committed
156 157
 digraph G {
    shift [fontcolor=white,color=white];
Christian Tischer's avatar
Christian Tischer committed
158
	"median" -> "local background" [label="  approximates"];
Christian Tischer's avatar
Christian Tischer committed
159 160
	"median" -> "radius" -> "> object width";
	"radius" -> "< spatial background frequency";
Christian Tischer's avatar
Christian Tischer committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  }
'/>


```
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
178 179
	- xy_8bit__spots_local_background_with_noise.tif 
- Use tophat filter to remove local background
Christian Tischer's avatar
Christian Tischer committed
180 181 182 183
- Devise code to implement a tophat filter using basic functions

### Formative assessment

Christian Tischer's avatar
Christian Tischer committed
184 185 186 187 188
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
189

Christian Tischer's avatar
Christian Tischer committed
190
## Learn more
Christian Tischer's avatar
Christian Tischer committed
191

Christian Tischer's avatar
Christian Tischer committed
192
- https://imagej.net/MorphoLibJ#Grayscale_morphological_filters
Christian Tischer's avatar
Christian Tischer committed
193 194 195