|
| 1 | +# --- |
| 2 | +# title: Custom median filters |
| 3 | +# cover: assets/median.gif |
| 4 | +# author: Johnny Chen |
| 5 | +# date: 2020-09-23 |
| 6 | +# --- |
| 7 | + |
| 8 | +# With median filter as an example, this demo shows how you could construct a custom kernel and |
| 9 | +# pass it to [`mapwindow`](@ref) for common stencil operations. |
| 10 | + |
| 11 | +# Unlike other image processing toolboxes, `ImageFiltering.jl` does not provide you an out-of-box |
| 12 | +# function for median filters. It instead provides the more general [`mapwindow`](@ref) function, |
| 13 | +# which allows you to apply any function to the window around each pixel. |
| 14 | + |
| 15 | +using ImageFiltering, ImageCore, ImageShow # or you could just `using Images` |
| 16 | +using TestImages |
| 17 | +using Statistics |
| 18 | +using Random #hide |
| 19 | +Random.seed!(0) #hide |
| 20 | + |
| 21 | +img = Float64[isodd(i) - isodd(j) for i = 1:5, j = 1:5] |
| 22 | +img[3, 3] = 1000 |
| 23 | +img #hide #md |
| 24 | + |
| 25 | +#- |
| 26 | + |
| 27 | +patch_size = (3, 3) |
| 28 | +imgm = mapwindow(median, img, patch_size) |
| 29 | + |
| 30 | +# `mapwindow` provides a high-level interface to loop over the image and call a function (`median` |
| 31 | +# in this demo) on each patch/window. It's performing an operation that is nearly equivalent to the |
| 32 | +# loop below, albeit more efficiently: |
| 33 | + |
| 34 | +## For simplicity, border condition is not included here. |
| 35 | +imgm = zeros(axes(img)) |
| 36 | +R = CartesianIndices(img) |
| 37 | +I_first, I_last = first(R), last(R) |
| 38 | +Δ = CartesianIndex(patch_size .÷ 2) |
| 39 | +for I in R |
| 40 | + patch = max(I_first, I-Δ):min(I_last, I+Δ) |
| 41 | + imgm[I] = median(img[patch]) |
| 42 | +end |
| 43 | +imgm |
| 44 | + |
| 45 | +# Compared to this hand-written loop, `mapwindow` offers additional flexibility in handling the |
| 46 | +# borders and allows you to process only a subset of the windows. For some functions (e.g., `min`, |
| 47 | +# `max`, and `extrema`), `mapwindow` uses a custom implementation that is far more efficient than |
| 48 | +# naively computing the function "freshly" on each window. |
| 49 | + |
| 50 | +# When the input array has `NaN` or some unwanted pixel values, a pre-filtering process is needed to |
| 51 | +# exclude them first. This can be done quite easily by compositing a given kernel operation and a |
| 52 | +# `filter` operation. Let's still take `median` filter as an example. |
| 53 | + |
| 54 | +img[2,2] = NaN |
| 55 | +imgm = mapwindow(median, img, (3,3)) |
| 56 | + |
| 57 | +# `NaN` has polluted the output, which is usually not wanted. This can be fixed quite easily by |
| 58 | +# compositing a new filter kernel. This way, we only compute the median value on the non-NaN subset |
| 59 | +# of each patch/window. |
| 60 | + |
| 61 | +_median(patch) = median(filter(x->!isnan(x), patch)) |
| 62 | +imgm = mapwindow(_median, img, patch_size) |
| 63 | + |
| 64 | + |
| 65 | +# ## Impulse (Salt and Pepper) noise removal |
| 66 | + |
| 67 | +# Median filters are quite robust to outliers (unusual values), this property can be used to remove |
| 68 | +# salt&pepper noise. An image with `n`-level salt&papper noise is defined as: each pixel `p` has |
| 69 | +# `n/2` probabilty to be filled by `0`(the minimal gamut value), `n/2` probabilty to be filled by |
| 70 | +# `1`(the maximal gamut value), and `1-n` probablity to be undamaged. |
| 71 | + |
| 72 | +img = testimage("cameraman") |
| 73 | + |
| 74 | +n = 0.2 # noise level |
| 75 | +noisy_img = map(img) do p |
| 76 | + sp = rand() |
| 77 | + if sp < n/2 |
| 78 | + eltype(img)(gamutmin(eltype(img))...) |
| 79 | + elseif sp < n |
| 80 | + eltype(img)(gamutmax(eltype(img))...) |
| 81 | + else |
| 82 | + p |
| 83 | + end |
| 84 | +end |
| 85 | + |
| 86 | +denoised_img = mapwindow(median!, noisy_img, (5, 5)) |
| 87 | + |
| 88 | +mosaicview(img, noisy_img, denoised_img; nrow=1) |
| 89 | + |
| 90 | + |
| 91 | +## save covers #src |
| 92 | +using ImageMagick #src |
| 93 | +mkpath("assets") #src |
| 94 | +ImageMagick.save("assets/median.gif", cat(noisy_img, denoised_img; dims=3); fps=1) #src |
0 commit comments