Spatial filters (optix.filters)
Neighbourhood filters applied to each image in a batch.
Spatial filters on the last two (spatial) dims of a tensor.
median_filter
median_filter(data, size, *, method='auto', budget=1 << 24)
Exact median filter over a rectangular window.
For every pixel (i, j) of the last two dims of data,
out[i, j] = lower median { data[i', j'] : i' ∈ Wᵢ, j' ∈ Vⱼ },
Wᵢ = [i − ⌊sₕ/2⌋, i − ⌊sₕ/2⌋ + sₕ − 1] ∩ [0, H),
(likewise Vⱼ with s_w), where the lower median of n values is the
value of rank ⌊(n − 1)/2⌋ (0-based) in increasing order, as returned
by torch.median and torch.nanmedian. Pixels outside the image are
ignored, so border windows are shrunk rather than padded. NaN pixels
are ignored as well; a window holding only NaN gives NaN. The result
equals torch.nanmedian over each window exactly (it is one of the
window's values), for floating and integer dtypes.
Leading dims are batch dims, filtered independently.
Method
The image is first replaced by the ranks of its pixels (a stable sort, NaN last), so that ties are broken by position and every rank is unique; the median rank of a window maps back to the median value through the sorted values, whatever the dtype. Ranks are grouped into blocks of F ≈ √(HW)/4 consecutive ranks, and blocks into S ≈ G superblocks of G blocks. Output rows are visited in order; for every column, histograms of the block and superblock indices of the sₕ window rows are kept, and updated by adding the entering row and removing the leaving one. Prefix sums of the column histograms along the columns give the window histogram of every pixel; a prefix sum over the superblocks locates the superblock that holds the median rank, then one over its G blocks locates the block. The median rank is finally selected among the F ranks of that block by testing which of their pixels lie inside the window. The cost per pixel is O(S + G + F) ≈ O((HW)^½) operations whatever the window size, plus the block prefix sums of the superblocks met on the current row (few when the median varies smoothly). Memory is about 4·√(HW)·W integers. Column histograms with a sliding update are the idea of Perreault & Hébert (2007); the search over rank blocks is a choice of this implementation.
References
- Perreault S, Hébert P. Median filtering in constant time. IEEE Trans Image Process 16(9):2389–2394, 2007. doi:10.1109/TIP.2007.902329
BibTeX
@article{perreault2007median,
author = {Perreault, S. and H{\'e}bert, P.},
title = {Median filtering in constant time},
journal = {IEEE Transactions on Image Processing},
volume = {16}, number = {9}, pages = {2389--2394},
year = {2007}, doi = {10.1109/TIP.2007.902329}
}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(..., H, W) Tensor
|
Image(s) to filter. |
required |
size
|
int or (int, int)
|
Window side sₕ = s_w, or (sₕ, s_w). An even side puts one more pixel before the centre than after it. A side larger than 2·n − 1 along a dim of length n is equivalent to 2·n − 1 there (every window then spans the whole dim). |
required |
method
|
(auto, histogram, sort)
|
|
"auto"
|
budget
|
int
|
|
1 << 24
|
Returns:
| Type | Description |
|---|---|
(..., H, W) Tensor
|
The filtered image, with the dtype and device of |
median_disk
median_disk(data, radius, *, rows=64)
Exact median filter over a disk of radius.
For every pixel (i, j) of the last two dims of data, the lower median
(rank ⌊(n − 1)/2⌋ of the n values, as torch.nanmedian) of the pixels
(i + di, j + dj) with di² + dj² ≤ radius². Pixels outside the image and
NaN pixels are ignored; a window holding only NaN gives NaN. Every
window is sorted, so the cost per pixel grows with the disk area; it
is computed in strips of rows rows. Leading dims are batch dims.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(..., H, W) Tensor
|
Image(s) to filter (floating or integer). |
required |
radius
|
int
|
Disk radius in pixels. |
required |
rows
|
int
|
Number of rows filtered at once. |
64
|
Returns:
| Type | Description |
|---|---|
(..., H, W) Tensor
|
The filtered image, with the dtype and device of |