Jump to content

Median filter: Difference between revisions

Updated D entry
(Updated D entry)
Line 309:
 
Image!Color medianFilter(uint radius=10, Color)(in Image!Color img)
pure nothrow @safe if (radius > 0) in {
assert(img.nx >= radius && img.ny >= radius);
} body {
alias Hist = uint[256];
 
static ubyte median(uint no)(in ref Hist cumulative) pure nothrow {
pure nothrow @safe @nogc {
size_t localSum = 0;
foreach (immutable /*ubyte*/ k, immutable v; cumulative)
if (v) {
localSum += v;
if (localSum > no / 2)
return cast(ubyte)k;
}
return 0;
Line 327 ⟶ 330:
foreach (immutable y; 0 .. img.ny)
foreach (immutable x; 0 .. img.nx)
if (x < radius || x > img.nx - radius - 1 ||
y < radius || y > img.ny - radius - 1)
result[x, y] = img[x, y];
 
Line 357 ⟶ 360:
H[k] += v;
 
//result[x, y] = Color(median!(edge ^^ 2)(H));
 
// Drop left-most column.
Line 375 ⟶ 378:
version (median_filter_main) {
void main() { // Demo.
loadPGM!Gray(null, "lena.pgm").
.medianFilter!10()
.savePGM("lena_median_r10.pgm");
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.