Skip to content

Commit

Permalink
Add DefaultSmooth and DefaultSharpen Ops
Browse files Browse the repository at this point in the history
Adapted from the implementations created by Barry Dezonia
  • Loading branch information
gselzer committed Jan 18, 2019
1 parent e8b7cb5 commit 9579493
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/main/java/net/imagej/ops/filter/FilterNamespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand Down Expand Up @@ -136,7 +136,7 @@ public <I extends RealType<I>, O extends RealType<O>> O addPoissonNoise(

/**
* Executes a bilateral filter on the given arguments.
*
*
* @param in
* @param out
* @param sigmaR
Expand Down Expand Up @@ -757,7 +757,7 @@ RandomAccessibleInterval<V> dog(final RandomAccessibleInterval<T> in,

/**
* Executes the "Frangi Vesselness" filter operation on the given arguments.
*
*
* @param in - input image
* @param out - output image
* @param spacing - n-dimensional array indicating the physical distance
Expand Down Expand Up @@ -1245,6 +1245,19 @@ public <T extends RealType<T>> RandomAccessibleInterval<T> partialDerivative(
return result;
}

// -- Sharpen

/** Executes the "sharpen" filter operation on the given arguments. */
@OpMethod(op = net.imagej.ops.filter.sharpen.DefaultSharpen.class)
public <T extends RealType<T>> RandomAccessibleInterval<T> sharpen(
final RandomAccessibleInterval<T> in)
{
@SuppressWarnings("unchecked")
final RandomAccessibleInterval<T> result =
(RandomAccessibleInterval<T>) ops().run(Ops.Filter.Sharpen.class, in);
return result;
}

/** Executes the "sigma" filter operation on the given arguments. */
@OpMethod(op = net.imagej.ops.filter.sigma.DefaultSigmaFilter.class)
public <T extends RealType<T>> IterableInterval<T> sigma(
Expand All @@ -1271,6 +1284,19 @@ public <T extends RealType<T>> IterableInterval<T> sigma(
return result;
}

// -- Smooth

/** Executes the "sharpen" filter operation on the given arguments. */
@OpMethod(op = net.imagej.ops.filter.smooth.DefaultSmooth.class)
public <T extends RealType<T>> RandomAccessibleInterval<T> smooth(
final RandomAccessibleInterval<T> in)
{
@SuppressWarnings("unchecked")
final RandomAccessibleInterval<T> result =
(RandomAccessibleInterval<T>) ops().run(Ops.Filter.Smooth.class, in);
return result;
}

// -- Sobel

@OpMethod(op = net.imagej.ops.filter.sobel.SobelRAI.class)
Expand Down
132 changes: 132 additions & 0 deletions src/main/java/net/imagej/ops/filter/sharpen/DefaultSharpen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@

package net.imagej.ops.filter.sharpen;

import net.imagej.Extents;
import net.imagej.Position;
import net.imagej.ops.Ops;
import net.imagej.ops.special.function.AbstractUnaryFunctionOp;
import net.imglib2.Cursor;
import net.imglib2.FinalInterval;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.neighborhood.Neighborhood;
import net.imglib2.algorithm.neighborhood.RectangleNeighborhood;
import net.imglib2.algorithm.neighborhood.RectangleNeighborhoodFactory;
import net.imglib2.algorithm.neighborhood.RectangleShape.NeighborhoodsAccessible;
import net.imglib2.type.numeric.RealType;
import net.imglib2.util.Util;
import net.imglib2.view.Views;

import org.scijava.plugin.Plugin;

/**
* Op rendition of the SharpenDataValues plugin written by Barry Dezonia
*
* @author Gabe Selzer
*/
@Plugin(type = Ops.Filter.Sharpen.class)
public class DefaultSharpen<T extends RealType<T>> extends
AbstractUnaryFunctionOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>>
implements Ops.Filter.Sharpen
{

final double[] kernel = { -1, -1, -1, -1, 12, -1, -1, -1, -1 };
double scale;

@Override
public RandomAccessibleInterval<T> calculate(
final RandomAccessibleInterval<T> input)
{
final RandomAccessibleInterval<T> output = ops().copy().rai(input);

final long[] planeDims = new long[input.numDimensions() - 2];
for (int i = 0; i < planeDims.length; i++)
planeDims[i] = input.dimension(i + 2);
final Extents extents = new Extents(planeDims);
final Position planePos = extents.createPosition();
if (planeDims.length == 0) {
computePlanar(planePos, input, output);
}
else {
while (planePos.hasNext()) {
planePos.fwd();
computePlanar(planePos, input, output);
}

}
return output;
}

private void computePlanar(final Position planePos,
final RandomAccessibleInterval<T> input,
final RandomAccessibleInterval<T> output)
{
// TODO can we just set scale to 4?
scale = 0;
for (final double d : kernel)
scale += d;

final T type = Util.getTypeFromInterval(input);

final long[] imageDims = new long[input.numDimensions()];
input.dimensions(imageDims);

// create all objects needed for NeighborhoodsAccessible
RandomAccessibleInterval<T> slicedInput = ops().copy().rai(input);
for (int i = planePos.numDimensions() - 1; i >= 0; i--) {
slicedInput = Views.hyperSlice(slicedInput, input.numDimensions() - 1 - i,
planePos.getLongPosition(i));
}

final RandomAccessible<T> refactoredInput = Views.extendMirrorSingle(
slicedInput);
final RectangleNeighborhoodFactory<T> factory = RectangleNeighborhood
.factory();
final FinalInterval neighborhoodSpan = new FinalInterval(new long[] { -1,
-1 }, new long[] { 1, 1 });

final NeighborhoodsAccessible<T> neighborhoods =
new NeighborhoodsAccessible<>(refactoredInput, neighborhoodSpan, factory);

// create cursors and random accesses for loop.
final Cursor<T> cursor = Views.iterable(input).localizingCursor();
final RandomAccess<T> outputRA = output.randomAccess();
for (int i = 0; i < planePos.numDimensions(); i++) {
outputRA.setPosition(planePos.getLongPosition(i), i + 2);
}
final RandomAccess<Neighborhood<T>> neighborhoodsRA = neighborhoods
.randomAccess();

int algorithmIndex = 0;
double sum;
final double[] n = new double[9];
while (cursor.hasNext()) {
cursor.fwd();
if (cursor.getLongPosition(0) == 14 && cursor.getLongPosition(1) == 0)
System.out.println("Hit 14");
neighborhoodsRA.setPosition(cursor);
final Neighborhood<T> current = neighborhoodsRA.get();
final Cursor<T> neighborhoodCursor = current.cursor();

algorithmIndex = 0;
sum = 0;
while (algorithmIndex < n.length) {
neighborhoodCursor.fwd();
n[algorithmIndex++] = neighborhoodCursor.get().getRealDouble();
}

for (int i = 0; i < kernel.length; i++) {
sum += kernel[i] * n[i];
}

double value = sum / scale;

outputRA.setPosition(cursor.getLongPosition(0), 0);
outputRA.setPosition(cursor.getLongPosition(1), 1);
if (value > type.getMaxValue()) value = type.getMaxValue();
if (value < type.getMinValue()) value = type.getMinValue();
outputRA.get().setReal(value);
}
}
}
131 changes: 131 additions & 0 deletions src/main/java/net/imagej/ops/filter/smooth/DefaultSmooth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

package net.imagej.ops.filter.smooth;

import net.imagej.Extents;
import net.imagej.Position;
import net.imagej.ops.Ops;
import net.imagej.ops.special.function.AbstractUnaryFunctionOp;
import net.imglib2.Cursor;
import net.imglib2.FinalInterval;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.neighborhood.Neighborhood;
import net.imglib2.algorithm.neighborhood.RectangleNeighborhood;
import net.imglib2.algorithm.neighborhood.RectangleNeighborhoodFactory;
import net.imglib2.algorithm.neighborhood.RectangleShape.NeighborhoodsAccessible;
import net.imglib2.type.numeric.RealType;
import net.imglib2.util.Util;
import net.imglib2.view.Views;

import org.scijava.plugin.Plugin;

/**
* Op rendition of the SmoothDataValues plugin written by Barry Dezonia
*
* @author Gabe Selzer
*/
@Plugin(type = Ops.Filter.Smooth.class)
public class DefaultSmooth<T extends RealType<T>> extends
AbstractUnaryFunctionOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>>
implements Ops.Filter.Smooth
{

final double[] kernel = { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
double scale;

@Override
public RandomAccessibleInterval<T> calculate(
final RandomAccessibleInterval<T> input)
{
final RandomAccessibleInterval<T> output = ops().copy().rai(input);

final long[] planeDims = new long[input.numDimensions() - 2];
for (int i = 0; i < planeDims.length; i++)
planeDims[i] = input.dimension(i + 2);
final Extents extents = new Extents(planeDims);
final Position planePos = extents.createPosition();
if (planeDims.length == 0) {
computePlanar(planePos, input, output);
}
else {
while (planePos.hasNext()) {
planePos.fwd();
computePlanar(planePos, input, output);
}

}
return output;
}

private void computePlanar(final Position planePos,
final RandomAccessibleInterval<T> input,
final RandomAccessibleInterval<T> output)
{
// TODO can we just set scale to 4?
scale = 0;
for (final double d : kernel)
scale += d;

final T type = Util.getTypeFromInterval(input);

final long[] imageDims = new long[input.numDimensions()];
input.dimensions(imageDims);

// create all objects needed for NeighborhoodsAccessible
RandomAccessibleInterval<T> slicedInput = ops().copy().rai(input);
for (int i = planePos.numDimensions() - 1; i >= 0; i--) {
slicedInput = Views.hyperSlice(slicedInput, input.numDimensions() - 1 - i,
planePos.getLongPosition(i));
}

final RandomAccessible<T> refactoredInput = Views.extendMirrorSingle(
slicedInput);
final RectangleNeighborhoodFactory<T> factory = RectangleNeighborhood
.factory();
final FinalInterval neighborhoodSpan = new FinalInterval(new long[] { -1,
-1 }, new long[] { 1, 1 });

final NeighborhoodsAccessible<T> neighborhoods =
new NeighborhoodsAccessible<>(refactoredInput, neighborhoodSpan, factory);

// create cursors and random accesses for loop.
final Cursor<T> cursor = Views.iterable(input).localizingCursor();
final RandomAccess<T> outputRA = output.randomAccess();
for (int i = 0; i < planePos.numDimensions(); i++) {
outputRA.setPosition(planePos.getLongPosition(i), i + 2);
}
final RandomAccess<Neighborhood<T>> neighborhoodsRA = neighborhoods
.randomAccess();

int algorithmIndex = 0;
double sum;
final double[] n = new double[9];
while (cursor.hasNext()) {

cursor.fwd();
neighborhoodsRA.setPosition(cursor);
final Neighborhood<T> current = neighborhoodsRA.get();
final Cursor<T> neighborhoodCursor = current.cursor();

algorithmIndex = 0;
sum = 0;
while (algorithmIndex < n.length) {
neighborhoodCursor.fwd();
n[algorithmIndex++] = neighborhoodCursor.get().getRealDouble();
}

for (int i = 0; i < kernel.length; i++) {
sum += kernel[i] * n[i];
}

double value = sum / scale;

outputRA.setPosition(cursor.getLongPosition(0), 0);
outputRA.setPosition(cursor.getLongPosition(1), 1);
if (value > type.getMaxValue()) value = type.getMaxValue();
if (value < type.getMinValue()) value = type.getMinValue();
outputRA.get().setReal(value);
}
}
}
2 changes: 2 additions & 0 deletions src/main/templates/net/imagej/ops/Ops.list
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ namespaces = ```
[name: "paddingIntervalCentered", iface: "PaddingIntervalCentered"],
[name: "paddingIntervalOrigin", iface: "PaddingIntervalOrigin"],
[name: "tubeness", iface: "Tubeness"],
[name: "sharpen", iface: "Sharpen"],
[name: "sigma", iface: "Sigma", aliases: ["sigmaFilter", "filterSigma"]],
[name: "smooth", iface: "Smooth"],
[name: "sobel", iface: "Sobel"],
[name: "variance", iface: "Variance", aliases: ["varianceFilter", "filterVariance", "var", "varFilter", "filterVar"]],
[name: "frangiVesselness", iface: "FrangiVesselness"],
Expand Down

0 comments on commit 9579493

Please sign in to comment.