package org.bonej.wrapperPlugins;

import net.imagej.ImgPlus;
import net.imagej.axis.CalibratedAxis;
import net.imagej.ops.OpService;
import net.imglib2.img.Img;
import net.imglib2.type.logic.BitType;
import org.bonej.ops.connectivity.EulerContribution;
import org.bonej.ops.connectivity.EulerCharacteristic;
import org.bonej.utilities.AxisUtils;
import org.bonej.utilities.ElementUtil;
import org.bonej.utilities.ResultsInserter;
import org.scijava.command.Command;
import org.scijava.command.ContextCommand;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.UIService;

import static org.bonej.wrapperPlugins.CommonMessages.*;
import static org.scijava.ui.DialogPrompt.MessageType.WARNING_MESSAGE;

/**
 * A wrapper UI class for the Connectivity Ops
 *
 * @author Richard Domander 
 */
@Plugin(type = Command.class, menuPath = "Plugins>BoneJ>Connectivity", headless = true)
public class ConnectivityWrapper extends ContextCommand {
    @Parameter(initializer = "initializeImage")
    private ImgPlus inputImage;

    @Parameter
    private OpService opService;

    @Parameter
    private UIService uiService;

    @Override
    public void run() {
        final Img<BitType> bitImg = opService.convert().bit(inputImage);
        final int dimensions = inputImage.numDimensions();
        final ImgPlus<BitType> bitImgPlus = new ImgPlus<>(bitImg);
        for (int d = 0; d < dimensions; d++) {
            // Copy metadata
            final CalibratedAxis axis = (CalibratedAxis) inputImage.axis(d);
            bitImgPlus.setAxis(axis, d);
        }

        final double eulerCharacteristic = (Integer) opService.run(EulerCharacteristic.class, bitImgPlus);
        final double edgeCorrection = (Double) opService.run(EulerContribution.class, bitImgPlus);
        final double connectivity = 1 - eulerCharacteristic;
        final double connectivityDensity = calculateConnectivityDensity(connectivity);

        showResults(eulerCharacteristic + edgeCorrection, edgeCorrection, connectivity, connectivityDensity);
    }

    //region -- Helper methods --
    private void showResults(final double eulerCharacteristic, final double deltaEuler, final double connectivity,
            final double connectivityDensity) {
        final String label = inputImage.getName();
        final String unitHeader = WrapperUtils.getUnitHeader(inputImage, "³");

        if (unitHeader.isEmpty()) {
            uiService.showDialog(BAD_CALIBRATION, WARNING_MESSAGE);
        }

        final ResultsInserter inserter = ResultsInserter.getInstance();
        inserter.setMeasurementInFirstFreeRow(label, "Euler char. (χ)", eulerCharacteristic);
        inserter.setMeasurementInFirstFreeRow(label, "Edge correction (Δχ)", deltaEuler);
        inserter.setMeasurementInFirstFreeRow(label, "Connectivity", connectivity);
        inserter.setMeasurementInFirstFreeRow(label, "Conn. density " + unitHeader, connectivityDensity);
        inserter.updateResults();
    }

    private double calculateConnectivityDensity(final double connectivity) {
        final double elements = AxisUtils.spatialSpaceSize(inputImage);
        final double elementSize = AxisUtils.calibratedSpatialElementSize(inputImage);
        return connectivity / (elements * elementSize);
    }

    @SuppressWarnings("unused")
    private void initializeImage() {
        if (inputImage == null) {
            cancel(NO_IMAGE_OPEN);
            return;
        }

        if (AxisUtils.countSpatialDimensions(inputImage) < 3) {
            cancel(NOT_3D_IMAGE);
            return;
        }

        if (!ElementUtil.isColorsBinary(inputImage)) {
            cancel(NOT_BINARY);
        }
    }
    //endregion
}

@@ -205,52 +205,57 @@
 ort 
-org.bonej.ops.connectivity.EulerContribution
+net.imglib2.type.numeric.integer.UnsignedByteType
 ;%0Aim
@@ -307,16 +307,69 @@
 ristic;%0A
+import org.bonej.ops.connectivity.EulerContribution;%0A
 import o
@@ -1095,16 +1095,34 @@
  ImgPlus
+%3CUnsignedByteType%3E
  inputIm
@@ -1581,25 +1581,8 @@
 is =
- (CalibratedAxis)
  inp
@@ -1883,24 +1883,22 @@
 e co
-nnectivity = 1 -
+rrectedEuler =
  eul
@@ -1913,16 +1913,89 @@
 teristic
+ - edgeCorrection;%0A        final double connectivity = 1 - correctedEuler
 ;%0A      
@@ -2119,41 +2119,24 @@
 stic
- + edgeCorrection, edgeCorrection
+, correctedEuler
 , co

Fix results