Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Tests/test_image_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ def test_rankfilter(
assert (minimum, med, maximum) == expected


@pytest.mark.parametrize("size", (1, 3, 5, 9, 25))
def test_rankfilter_l_matches_reference(size: int) -> None:
# Compare the 8-bit fast path against a brute-force rank on edge-padded windows
xsize, ysize = 23, 17
data = [
(x * 37 + y * 101 + (x * y) % 13) % 256
for y in range(ysize)
for x in range(xsize)
]
im = Image.new("L", (xsize, ysize))
im.putdata(data)
margin = size // 2

for rank in {0, size * size // 2, size * size - 1}:
result = im.filter(ImageFilter.RankFilter(size, rank))
for y in range(ysize):
for x in range(xsize):
window = sorted(
data[
min(max(y + j, 0), ysize - 1) * xsize
+ min(max(x + i, 0), xsize - 1)
]
for j in range(-margin, margin + 1)
for i in range(-margin, margin + 1)
)
assert result.getpixel((x, y)) == window[rank]


@pytest.mark.parametrize(
"filter", (ImageFilter.MinFilter, ImageFilter.MedianFilter, ImageFilter.MaxFilter)
)
Expand Down
57 changes: 55 additions & 2 deletions src/libImaging/RankFilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,57 @@

#include "Imaging.h"

/* Sliding histogram rank filter for 8-bit images (Huang, 1979).
Moving the window one pixel to the right removes one column and adds
another, so each step costs O(size) instead of the O(size^2) needed to
copy and select the whole window. "below" counts the window samples
less than v, the current output value, which moves only a little
between neighbouring pixels. */
static void
RankUINT8(Imaging imOut, Imaging im, int size, int rank) {
int x, y, i, v, below;
int hist[256];

if (imOut->xsize <= 0) {
return;
}

for (y = 0; y < imOut->ysize; y++) {
UINT8 *out = imOut->image8[y];
UINT8 **in = &im->image8[y];

memset(hist, 0, sizeof(hist));
for (i = 0; i < size; i++) {
for (x = 0; x < size; x++) {
hist[in[i][x]]++;
}
}
v = 0;
below = 0;
while (below + hist[v] <= rank) {
below += hist[v++];
}
out[0] = (UINT8)v;

for (x = 1; x < imOut->xsize; x++) {
for (i = 0; i < size; i++) {
UINT8 a = in[i][x - 1];
UINT8 b = in[i][x + size - 1];
hist[a]--;
hist[b]++;
below += (b < v) - (a < v);
}
while (below > rank) {
below -= hist[--v];
}
while (below + hist[v] <= rank) {
below += hist[v++];
}
out[x] = (UINT8)v;
}
}
}

/* Fast rank algorithm (due to Wirth), based on public domain code
by Nicolas Devillard, available at http://ndevilla.free.fr */

Expand Down Expand Up @@ -57,7 +108,7 @@
return a[k]; \
}

MakeRankFunction(UINT8) MakeRankFunction(INT32) MakeRankFunction(FLOAT32)
MakeRankFunction(INT32) MakeRankFunction(FLOAT32)

Imaging ImagingRankFilter(Imaging im, int size, int rank) {
Imaging imOut = NULL;
Expand Down Expand Up @@ -116,7 +167,9 @@ MakeRankFunction(UINT8) MakeRankFunction(INT32) MakeRankFunction(FLOAT32)
} while (0)

if (im->image8) {
RANK_BODY(UINT8);
ImagingSectionEnter(&cookie);
RankUINT8(imOut, im, size, rank);
ImagingSectionLeave(&cookie);
} else if (im->type == IMAGING_TYPE_INT32) {
RANK_BODY(INT32);
} else if (im->type == IMAGING_TYPE_FLOAT32) {
Expand Down