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