Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (P)SNR, RMSE, and MAE #536

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ -42,6 +42,7 @@
import net.imglib2.type.Type;
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 @@ -456,6 +457,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be ops().run(Ops.Image.MAE.class, out, reference, test);. That way if someone creates a more specific/specialized MAE op, it could be matched. Otherwise, DefaultMAE would always be used.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be ops().run(Ops.Image.PSNR.class, out, reference, test), and same idea for the remaining namespace methods you added. They should be run(...) with an interface not a concrete implementation.

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 - 2018 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)
{
final Cursor<I> cursor = input1.cursor();
final Cursor<I> cursor2 = input2.cursor();

double denominatorSum = 0d;
while (cursor.hasNext()) {
final double r = cursor.next().getRealDouble();
final double t = cursor2.next().getRealDouble();
final 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(final IterableInterval<I> input1,
final 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 - 2018 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)
{
final Cursor<I> cursor = input1.cursor();
final Cursor<I> cursor2 = input2.cursor();
double max = opService.stats().max(input1).getRealDouble();
max *= max;

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

denominatorSum *= 1d / Intervals.numElements(input1);
final 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(final IterableInterval<I> input1,
final 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 - 2018 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)
{
final Cursor<I> cursor = input1.cursor();
final Cursor<I> cursor2 = input2.cursor();

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

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

final 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(final IterableInterval<I> input1,
final IterableInterval<I> input2)
{
return new DoubleType();
}

}
Loading