Skip to content

Commit

Permalink
Add median filter
Browse files Browse the repository at this point in the history
  • Loading branch information
gecko0307 committed May 4, 2023
1 parent 6af54eb commit 602781d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
96 changes: 96 additions & 0 deletions dlib/image/filters/median.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright (c) 2022-2023 Oleg Baharev, Timur Gafarov
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

/**
* Median filter.
*
* Copyright: Oleg Baharev, Timur Gafarov 2022-2023.
* License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Oleg Baharev, Timur Gafarov
*/
module dlib.image.filters.median;

import std.algorithm: sort;
import dlib.core.memory;
import dlib.image.image;
import dlib.image.color;

/// Median filter
auto medianFilter(SuperImage img, SuperImage outp, uint windowWidth, uint windowHeight)
{
SuperImage res;
if (outp)
res = outp;
else
res = img.dup;

struct ColorAndLuma
{
Color4f color;
float luminance;
}

ColorAndLuma[] window = New!(ColorAndLuma[])(windowWidth * windowHeight);

uint halfWindowWidth = windowWidth / 2;
uint halfWindowHeight = windowHeight / 2;

foreach(y; 0..img.height)
foreach(x; 0..img.width)
{
foreach(wy; 0..windowHeight)
foreach(wx; 0..windowWidth)
{
int sampleX = x - halfWindowWidth + wx;
int sampleY = y - halfWindowHeight + wy;
if (sampleX < 0) sampleX = 0;
else if (sampleX >= img.width) sampleX = img.width - 1;
if (sampleY < 0) sampleY = 0;
else if (sampleY >= img.height) sampleY = img.height - 1;

auto sample = img[sampleX, sampleY];

auto windowSample = &window[wy * windowWidth + wx];
windowSample.color = sample;
windowSample.luminance = sample.luminance;
}

sort!("a.luminance > b.luminance")(window);
res[x, y] = window[$ / 2].color;
}

Delete(window);

return res;
}

/// ditto
SuperImage medianFilter(SuperImage img, uint windowWidth, uint windowHeight)
{
return medianFilter(img, null, windowWidth, windowHeight);
}
1 change: 1 addition & 0 deletions dlib/image/filters/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public
import dlib.image.filters.desaturate;
import dlib.image.filters.edgedetect;
import dlib.image.filters.lens;
import dlib.image.filters.median;
import dlib.image.filters.morphology;
import dlib.image.filters.normalmap;
import dlib.image.filters.sharpen;
Expand Down

0 comments on commit 602781d

Please sign in to comment.