Skip to content
Go to file
Cannot retrieve contributors at this time
executable file 141 lines (124 sloc) 4.17 KB
#!/usr/bin/env dub
/+ dub.json:
"name": "median_filter",
"dependencies": {"mir": {"path": ".."}, "imageformats": "==7.0.0"},
import mir.ndslice;
A median filter is implemented as an example. The function
`movingWindowByChannel` can also be used with other filters that use a sliding
window as the argument, in particular with convolution matrices such as the
$(LINK2, Sobel operator).
`movingWindowByChannel` iterates over an image in sliding window mode.
Each window is transferred to a `filter`, which calculates the value of the
pixel that corresponds to the given window.
This function does not calculate border cases in which a window overlaps
the image partially. However, the function can still be used to carry out such
calculations. That can be done by creating an amplified image, with the edges
reflected from the original image, and then applying the given function to the
new file.
Note: You can find the example at
$(LINK2, GitHub).
filter = unary function. Dimension window 2D is the argument.
image = image dimensions `(h, w, c)`,
where с is the number of channels in the image
nr = number of rows in the window
nс = number of columns in the window
image dimensions `(h - nr + 1, w - nc + 1, c)`,
where с is the number of channels in the image.
Dense data layout is guaranteed.
Slice!(C*, 3) movingWindowByChannel(alias filter, C)
(Slice!(C*, 3, Universal) image, size_t nr, size_t nc)
// 0. 3D
// The last dimension represents the color channel.
return image
// 1. 2D composed of 1D
// Packs the last dimension.
// 2. 2D composed of 2D composed of 1D
// Splits image into overlapping windows.
.windows(nr, nc)
// 3. 5D
// Unpacks the windows.
// 4. 5D
// Brings the color channel dimension to the third position.
.transposed!(0, 1, 4)
// 5. 3D Composed of 2D
// Packs the last two dimensions.
// 2D to pixel lazy conversion.
// Creates the new image. The only memory allocation in this function.
r = input range
buf = buffer with length no less than the number of elements in `r`
median value over the range `r`
T median(Iterator, SliceKind kind, T)(Slice!(Iterator, 2, kind) sl, T[] buf)
import std.algorithm.sorting : topN;
// copy sl to the buffer
auto retPtr = reduce!(
(ptr, elem) {
*ptr = elem;
return ptr + 1;
} )(buf.ptr, sl);
auto n = retPtr - buf.ptr;
buf[0 .. n].topN(n / 2);
return buf[n / 2];
This program works both with color and grayscale images.
void main(string[] args)
import std.conv: to;
import std.getopt: getopt, defaultGetoptPrinter;
import std.path: stripExtension;
uint nr, nc, def = 3;
auto helpInformation = args.getopt(
"nr", "number of rows in window, default value is " ~!string, &nr,
"nc", "number of columns in window, default value is equal to nr", &nc);
if (helpInformation.helpWanted)
"Usage: median-filter [<options...>] [<file_names...>]\noptions:",
if (!nr) nr = def;
if (!nc) nc = nr;
auto buf = new ubyte[nr * nc];
if (args.length == 1)
import std.stdio: writeln;
writeln("No input file given");
foreach (name; args[1 .. $])
import imageformats; // can be found at
IFImage image = read_image(name);
auto ret = image.pixels
.sliced(cast(size_t)image.h, cast(size_t)image.w, cast(size_t)image.c)
!(window => median(window, buf))
(nr, nc);
name.stripExtension ~ "_filtered.png",
You can’t perform that action at this time.