Skip to content

Commit

Permalink
#343 Improved border processing in denoise filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ishubin committed Nov 14, 2015
1 parent 28cd9d8 commit eb6fb05
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Expand Up @@ -30,12 +30,15 @@ public DenoiseFilter(int radius) {
public void apply(byte[] bytes, int width, int height, Rectangle area) {
radius = Math.min(radius, Math.min(width / 2, height / 2));

int maximumPossiblePixels = (radius * 2 + 1) * (radius * 2 + 1);

if (radius > 0) {
for (int yc = 0; yc < height; yc++) {
for (int xc = 0; xc < width; xc++) {

int blackPixels = 0;
int whitePixels = 0;
int total = 0;

int startY = Math.max(yc - radius, 0);
int startX = Math.max(xc - radius, 0);
Expand All @@ -55,12 +58,18 @@ public void apply(byte[] bytes, int width, int height, Rectangle area) {
else {
whitePixels ++;
}

total++;
}
}

double amountRatio = ((double) total) / ((double) maximumPossiblePixels);

int k = yc * width * ImageHandler.BLOCK_SIZE + xc * ImageHandler.BLOCK_SIZE;
if (whitePixels > 0) {
if (blackPixels / whitePixels > 3) {
if ((amountRatio > 0.6 && blackPixels / whitePixels > 3) // matching normal pixels
|| (amountRatio <= 0.6 && blackPixels / whitePixels >= 2) // matching pixels that are on border
) {
bytes[k] = 0;
bytes[k + 1] = 0;
bytes[k + 2] = 0;
Expand Down
Expand Up @@ -395,10 +395,9 @@ public void shouldApply_denoiseFilter_andRemoveAllNoisePixels() throws IOExcepti
}

List<Integer> expectedPixels = asList(
840, 545, 537, 536, 543, 524, 526, 428, 432
840, 534, 531, 536, 543, 524, 526, 428, 432
);

// Assert that denoise 1 removes only some of them
for (int size = 1; size < 10; size++) {
ComparisonOptions options = new ComparisonOptions();
options.setTolerance(25);
Expand Down

0 comments on commit eb6fb05

Please sign in to comment.