All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scijava.ops.image.threshold.ApplyThresholdMethodLocal Maven / Gradle / Ivy

The newest version!
/*
 * #%L
 * Image processing operations for SciJava Ops.
 * %%
 * Copyright (C) 2014 - 2024 SciJava 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 org.scijava.ops.image.threshold;

import java.util.function.Function;

import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.neighborhood.Shape;
import net.imglib2.histogram.Histogram1d;
import net.imglib2.outofbounds.OutOfBoundsBorderFactory;
import net.imglib2.outofbounds.OutOfBoundsFactory;
import net.imglib2.type.logic.BitType;
import net.imglib2.type.numeric.RealType;

import org.scijava.function.Computers;
import org.scijava.ops.spi.Nullable;
import org.scijava.ops.spi.OpDependency;

/**
 * Ops which compute and apply a local threshold to an image.
 *
 * @author Stefan Helfrich (University of Konstanz)
 */
public final class ApplyThresholdMethodLocal {

	private ApplyThresholdMethodLocal() {
		// NB: Prevent instantiation of utility class.
	}

	/**
	 * @implNote op names='threshold.huang'
	 */
	public static class LocalHuang> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.huang")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.ij1'
	 */
	public static class LocalIJ1> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.ij1")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.intermodes'
	 */
	public static class LocalIntermodes> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.intermodes")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.isoData'
	 */
	public static class LocalIsoData> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.isoData")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.li'
	 */
	public static class LocalLi> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.li")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.maxEntropy'
	 */
	public static class LocalMaxEntropy> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.maxEntropy")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.maxLikelihood'
	 */
	public static class LocalMaxLikelihood> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.maxLikelihood")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.mean'
	 */
	public static class LocalHistogramBasedMean> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.mean")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.minError'
	 */
	public static class LocalMinError> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.minError")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.minimum'
	 */
	public static class LocalMinimum> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.minimum")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.moments'
	 */
	public static class LocalMoments> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.moments")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.otsu'
	 */
	public static class LocalOtsu> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.otsu")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.percentile'
	 */
	public static class LocalPercentile> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.percentile")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.renyiEntropy'
	 */
	public static class LocalRenyiEntropy> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.renyiEntropy")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.rosin'
	 */
	public static class LocalRosin> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.rosin")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.shanbhag'
	 */
	public static class LocalShanbhag> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.shanbhag")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.triangle'
	 */
	public static class LocalTriangle> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.triangle")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	/**
	 * @implNote op names='threshold.yen'
	 */
	public static class LocalYen> extends
		AbstractApplyLocalHistogramBasedThreshold
	{

		@OpDependency(name = "threshold.yen")
		private Computers.Arity1, T> computeThresholdOp;

		@Override
		protected Computers.Arity1, T> getComputeThresholdOp() {
			return computeThresholdOp;
		}
	}

	private abstract static class AbstractApplyLocalHistogramBasedThreshold>
		implements
		Computers.Arity3, Shape, OutOfBoundsFactory>, //
				RandomAccessibleInterval> {

		@OpDependency(name = "image.histogram")
		private Function, Histogram1d> createHistogramOp;

		// TODO: Would be cool if Computers.Arity2 would be matched.
		@OpDependency(name = "threshold.apply")
		private Computers.Arity2, T, BitType> applyThresholdOp;

		@OpDependency(name = "filter.applyCenterAware")
		private Computers.Arity4, Computers.Arity2, T, BitType>, Shape, OutOfBoundsFactory>, RandomAccessibleInterval> applyFilterOp;

		private Computers.Arity2, T, BitType> thresholdOp;

		/**
		 * TODO
		 *
		 * @param input
		 * @param inputNeighborhoodShape
		 * @param outOfBoundsFactory
		 * @param output
		 */
		@Override
		public void compute(final RandomAccessibleInterval input,
			final Shape inputNeighborhoodShape,
			@Nullable OutOfBoundsFactory> outOfBoundsFactory,
			final RandomAccessibleInterval output)
		{
			if (outOfBoundsFactory == null) outOfBoundsFactory =
				new OutOfBoundsBorderFactory<>();
			if (thresholdOp == null) thresholdOp = getThresholdOp();
			applyFilterOp.compute(input, thresholdOp, inputNeighborhoodShape,
				outOfBoundsFactory, output);
		}

		private Computers.Arity2, T, BitType> getThresholdOp() {
			final Computers.Arity1, T> computeThresholdOp =
				getComputeThresholdOp();
			return new Computers.Arity2, T, BitType>() {

				@Override
				public void compute(final Iterable inputNeighborhood,
					final T inputCenterPixel, final BitType output)
				{
					final Histogram1d histogram = createHistogramOp.apply(
						inputNeighborhood);
					final T threshold = inputNeighborhood.iterator().next()
						.createVariable();
					computeThresholdOp.compute(histogram, threshold);
					applyThresholdOp.compute(inputCenterPixel, threshold, output);
				}
			};
		}

		protected abstract Computers.Arity1, T>
			getComputeThresholdOp();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy