Skip to content

Commit

Permalink
Add (P)SNR, RMSE, and MAE implementations
Browse files Browse the repository at this point in the history
Adds various metrics for comparing reference images with (noisy) test
images.
  • Loading branch information
stelfrich committed Mar 29, 2018
1 parent feeb13e commit c888b0a
Show file tree
Hide file tree
Showing 7 changed files with 556 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/main/java/net/imagej/ops/image/ImageNamespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import net.imglib2.type.numeric.IntegerType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.IntType;
import net.imglib2.type.numeric.real.DoubleType;

import org.scijava.plugin.Plugin;

Expand Down Expand Up @@ -469,6 +470,80 @@ <T extends RealType<T>> IterableInterval<T> normalize(
return result;
}

// -- quality --

@OpMethod(op = net.imagej.ops.image.quality.DefaultMAE.class)
public <T extends RealType<T>> DoubleType mae(final DoubleType out,
final IterableInterval<T> reference, final IterableInterval<T> test)
{
final DoubleType result = (DoubleType) ops().run(
net.imagej.ops.image.quality.DefaultMAE.class, out, reference, test);
return result;
}

@OpMethod(op = net.imagej.ops.image.quality.DefaultMAE.class)
public <T extends RealType<T>> DoubleType mae(
final IterableInterval<T> reference, final IterableInterval<T> test)
{
final DoubleType result = (DoubleType) ops().run(
net.imagej.ops.image.quality.DefaultMAE.class, reference, test);
return result;
}

@OpMethod(op = net.imagej.ops.image.quality.DefaultPSNR.class)
public <T extends RealType<T>> DoubleType psnr(final DoubleType out,
final IterableInterval<T> reference, final IterableInterval<T> test)
{
final DoubleType result = (DoubleType) ops().run(
net.imagej.ops.image.quality.DefaultPSNR.class, out, reference, test);
return result;
}

@OpMethod(op = net.imagej.ops.image.quality.DefaultPSNR.class)
public <T extends RealType<T>> DoubleType psnr(
final IterableInterval<T> reference, final IterableInterval<T> test)
{
final DoubleType result = (DoubleType) ops().run(
net.imagej.ops.image.quality.DefaultPSNR.class, reference, test);
return result;
}

@OpMethod(op = net.imagej.ops.image.quality.DefaultRMSE.class)
public <T extends RealType<T>> DoubleType rmse(final DoubleType out,
final IterableInterval<T> reference, final IterableInterval<T> test)
{
final DoubleType result = (DoubleType) ops().run(
net.imagej.ops.image.quality.DefaultRMSE.class, out, reference, test);
return result;
}

@OpMethod(op = net.imagej.ops.image.quality.DefaultRMSE.class)
public <T extends RealType<T>> DoubleType rmse(
final IterableInterval<T> reference, final IterableInterval<T> test)
{
final DoubleType result = (DoubleType) ops().run(
net.imagej.ops.image.quality.DefaultRMSE.class, reference, test);
return result;
}

@OpMethod(op = net.imagej.ops.image.quality.DefaultSNR.class)
public <T extends RealType<T>> DoubleType snr(final DoubleType out,
final IterableInterval<T> reference, final IterableInterval<T> test)
{
final DoubleType result = (DoubleType) ops().run(
net.imagej.ops.image.quality.DefaultSNR.class, out, reference, test);
return result;
}

@OpMethod(op = net.imagej.ops.image.quality.DefaultSNR.class)
public <T extends RealType<T>> DoubleType snr(
final IterableInterval<T> reference, final IterableInterval<T> test)
{
final DoubleType result = (DoubleType) ops().run(
net.imagej.ops.image.quality.DefaultSNR.class, reference, test);
return result;
}

// -- watershed --

/** Executes the "watershed" operation on the given arguments. */
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/net/imagej/ops/image/quality/DefaultMAE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2014 - 2017 ImageJ developers.
* %%
* 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
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imagej.ops.image.quality;

import net.imagej.ops.Contingent;
import net.imagej.ops.Ops;
import net.imagej.ops.map.Maps;
import net.imagej.ops.special.hybrid.AbstractBinaryHybridCF;
import net.imglib2.Cursor;
import net.imglib2.IterableInterval;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.util.Intervals;

import org.scijava.plugin.Plugin;

/**
* Computes the mean absolute error (MAE) between a reference image and a
* (noisy) test image.
* <p>
* Computations are based on the definitions of Gonzalez (R.C. Gonzalez and R.E.
* Woods, "Digital Image Processing," Prentice Hall 2008).
* </p>
*
* @author Stefan Helfrich (University of Konstanz)
* @param <I> type of input elements
*/
@Plugin(type = Ops.Image.MAE.class)
public class DefaultMAE<I extends RealType<I>> extends
AbstractBinaryHybridCF<IterableInterval<I>, IterableInterval<I>, DoubleType>
implements Ops.Image.MAE, Contingent
{

@Override
public void compute(final IterableInterval<I> input1,
final IterableInterval<I> input2, final DoubleType output)
{
Cursor<I> cursor = input1.cursor();
Cursor<I> cursor2 = input2.cursor();

double denominatorSum = 0d;
while (cursor.hasNext()) {
double r = cursor.next().getRealDouble();
double t = cursor2.next().getRealDouble();
double abs = Math.abs(r - t);
denominatorSum += abs;
}

denominatorSum *= 1d / Intervals.numElements(input1);
output.setReal(denominatorSum);
}

@Override
public boolean conforms() {
return Intervals.equalDimensions(in1(), in2()) && //
Maps.compatible(in1(), in2());
}

@Override
public DoubleType createOutput(IterableInterval<I> input1,
IterableInterval<I> input2)
{
return new DoubleType();
}

}
101 changes: 101 additions & 0 deletions src/main/java/net/imagej/ops/image/quality/DefaultPSNR.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2014 - 2017 ImageJ developers.
* %%
* 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
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imagej.ops.image.quality;

import net.imagej.ops.Contingent;
import net.imagej.ops.OpService;
import net.imagej.ops.Ops;
import net.imagej.ops.map.Maps;
import net.imagej.ops.special.hybrid.AbstractBinaryHybridCF;
import net.imglib2.Cursor;
import net.imglib2.IterableInterval;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.util.Intervals;

import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

/**
* Computes peak signal-to-noise ratio (PSNR) between a reference image and a
* (noisy) test image. The resulting PSNR is expressed in decibel.
* <p>
* Computations are based on the definitions of Gonzalez (R.C. Gonzalez and R.E.
* Woods, "Digital Image Processing," Prentice Hall 2008).
* </p>
*
* @author Stefan Helfrich (University of Konstanz)
* @param <I> type of input elements
*/
@Plugin(type = Ops.Image.PSNR.class)
public class DefaultPSNR<I extends RealType<I>> extends
AbstractBinaryHybridCF<IterableInterval<I>, IterableInterval<I>, DoubleType>
implements Ops.Image.PSNR, Contingent
{

@Parameter
private OpService opService;

@Override
public void compute(final IterableInterval<I> input1,
final IterableInterval<I> input2, final DoubleType output)
{
Cursor<I> cursor = input1.cursor();
Cursor<I> cursor2 = input2.cursor();
double max = opService.stats().max(input1).getRealDouble();
max *= max;

double denominatorSum = 0d;
while (cursor.hasNext()) {
double r = cursor.next().getRealDouble();
double t = cursor2.next().getRealDouble();
denominatorSum += Math.pow(r - t, 2);
}

denominatorSum *= 1d / Intervals.numElements(input1);
double psnr = 10 * Math.log10(max/denominatorSum);

output.setReal(psnr);
}

@Override
public boolean conforms() {
return Intervals.equalDimensions(in1(), in2()) && //
Maps.compatible(in1(), in2());
}

@Override
public DoubleType createOutput(IterableInterval<I> input1,
IterableInterval<I> input2)
{
return new DoubleType();
}

}
95 changes: 95 additions & 0 deletions src/main/java/net/imagej/ops/image/quality/DefaultRMSE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2014 - 2017 ImageJ developers.
* %%
* 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
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imagej.ops.image.quality;

import net.imagej.ops.Contingent;
import net.imagej.ops.Ops;
import net.imagej.ops.map.Maps;
import net.imagej.ops.special.hybrid.AbstractBinaryHybridCF;
import net.imglib2.Cursor;
import net.imglib2.IterableInterval;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.util.Intervals;

import org.scijava.plugin.Plugin;

/**
* Computes the root mean square error (RMSE) between a reference image and a
* (noisy) test image.
* <p>
* Computations are based on the definitions of Gonzalez (R.C. Gonzalez and R.E.
* Woods, "Digital Image Processing," Prentice Hall 2008).
* </p>
*
* @author Stefan Helfrich (University of Konstanz)
* @param <I> type of input elements
*/
@Plugin(type = Ops.Image.RMSE.class)
public class DefaultRMSE<I extends RealType<I>> extends
AbstractBinaryHybridCF<IterableInterval<I>, IterableInterval<I>, DoubleType>
implements Ops.Image.RMSE, Contingent
{

@Override
public void compute(final IterableInterval<I> input1,
final IterableInterval<I> input2, final DoubleType output)
{
Cursor<I> cursor = input1.cursor();
Cursor<I> cursor2 = input2.cursor();

double denominatorSum = 0d;
while (cursor.hasNext()) {
double r = cursor.next().getRealDouble();
double t = cursor2.next().getRealDouble();
denominatorSum += Math.pow(r - t, 2);
}

denominatorSum *= 1d / Intervals.numElements(input1);

double rmse = Math.sqrt(denominatorSum);

output.setReal(rmse);
}

@Override
public boolean conforms() {
return Intervals.equalDimensions(in1(), in2()) && //
Maps.compatible(in1(), in2());
}

@Override
public DoubleType createOutput(IterableInterval<I> input1,
IterableInterval<I> input2)
{
return new DoubleType();
}

}
Loading

0 comments on commit c888b0a

Please sign in to comment.