package org.bonej.utilities;

import ij.ImagePlus;
import ij.measure.Calibration;
import org.jetbrains.annotations.Contract;

import javax.annotation.Nullable;
import java.util.Arrays;

/**
 * Utility methods for checking ImagePlus properties
 *
 * @author Richard Domander 
 */
public class ImagePlusCheck {

    @Contract("null -> false")
    public static boolean is3D(@Nullable final ImagePlus image) {
        return image != null && image.getNSlices() > 1;
    }

    @Contract("null -> false")
    public static boolean isBinaryColour(@Nullable final ImagePlus image) {
        return image != null && Arrays.stream(image.getStatistics().histogram).filter(p -> p > 0).count() <= 2;
    }


    /**
     * Calculates the degree of anisotropy in the image, i.e. the maximum difference in the ratios of the dimensions
     *
     * @return Anisotropy fraction [0.0, Double.MAX_VALUE], an isotropic image returns 0.0
     *         Returns Double.NaN if image == null
     */
    public static double anisotropy(@Nullable final ImagePlus image) {
        if (image == null) {
            return Double.NaN;
        }

        final Calibration cal = image.getCalibration();
        final double w = cal.pixelWidth;
        final double h = cal.pixelHeight;
        final double widthHeightRatio = w > h ? w / h : h / w;
        final double widthHeightAnisotropy = Math.abs(1.0 - widthHeightRatio);

        if (!is3D(image)) {
            return widthHeightAnisotropy;
        }

        final double d = cal.pixelDepth;
        final double widthDepthRatio = w > d ? w / d : d / w;
        final double heightDepthRatio = h > d ? h / d : d / h;
        final double widthDepthAnisotropy = Math.abs(1.0 - widthDepthRatio);
        final double heightDepthAnisotropy = Math.abs(1.0 - heightDepthRatio);

        return Math.max(widthHeightAnisotropy, Math.max(widthDepthAnisotropy, heightDepthAnisotropy));
    }
}

@@ -311,163 +311,541 @@
     
-@Contract(%22null -%3E false%22)%0A    public static boolean is3D(@Nullable final ImagePlus image) %7B%0A        return image != null && image.getNSlices() %3E 1;%0A    %7D%0A
+/**%0A     * Checks if the image is 3D%0A     *%0A     * @return true if the image has more than one slice,%0A     *         false if not, or image is null%0A     */%0A    @Contract(%22null -%3E false%22)%0A    public static boolean is3D(@Nullable final ImagePlus image) %7B%0A        return image != null && image.getNSlices() %3E 1;%0A    %7D%0A%0A    /**%0A     * Checks if the image has only two different colours%0A     *%0A     * @return true if there are only two distinct pixel values present in the image,%0A     *         false if more, or the image is null%0A     */
 %0A   
@@ -1067,17 +1067,16 @@
 %0A    %7D%0A%0A
-%0A
     /**%0A

Add comments