-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DefaultSmooth and DefaultSharpen Ops
Adapted from the implementations created by Barry Dezonia
- Loading branch information
Showing
4 changed files
with
295 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
src/main/java/net/imagej/ops/filter/sharpen/DefaultSharpen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
131
src/main/java/net/imagej/ops/filter/smooth/DefaultSmooth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters