Search in sources :

Example 1 with ImageOp

use of qupath.opencv.ops.ImageOp in project qupath by qupath.

the class SimpleThresholdCommand method updateClassification.

private void updateClassification() {
    // for (var viewer : qupath.getViewers()) {
    // var imageData = viewer.getImageData();
    // if (imageData == null) {
    // selectedOverlay.set(null);
    // viewer.resetCustomPixelLayerOverlay();
    // }
    // }
    var channel = selectedChannel.get();
    var thresholdValue = threshold.get();
    var resolution = selectedResolution.get();
    if (channel == null || thresholdValue == null || resolution == null) {
        resetOverlays();
        return;
    }
    var feature = selectedPrefilter.get();
    double sigmaValue = sigma.get();
    PixelClassifier classifier;
    List<ImageOp> ops = new ArrayList<>();
    if (feature != null && sigmaValue > 0) {
        ops.add(feature.buildOp(sigmaValue));
    }
    ops.add(ImageOps.Threshold.threshold(threshold.get()));
    Map<Integer, PathClass> classifications = new LinkedHashMap<>();
    classifications.put(0, classificationsBelow.getSelectionModel().getSelectedItem());
    classifications.put(1, classificationsAbove.getSelectionModel().getSelectedItem());
    var op = ImageOps.Core.sequential(ops);
    var transformer = ImageOps.buildImageDataOp(channel).appendOps(op);
    classifier = PixelClassifiers.createClassifier(transformer, resolution.getPixelCalibration(), classifications);
    // Create classifier
    var overlay = PixelClassificationOverlay.create(qupath.getOverlayOptions(), classifier);
    overlay.setLivePrediction(true);
    var previousOverlay = selectedOverlay.get();
    if (previousOverlay != null)
        previousOverlay.stop();
    selectedOverlay.set(overlay);
    this.currentClassifier.set(classifier);
    ensureOverlays();
}
Also used : PathClass(qupath.lib.objects.classes.PathClass) ImageOp(qupath.opencv.ops.ImageOp) PixelClassifier(qupath.lib.classifiers.pixel.PixelClassifier) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ImageOp

use of qupath.opencv.ops.ImageOp in project qupath by qupath.

the class DensityMapDataOp method buildOpAndChannels.

private void buildOpAndChannels() {
    logger.trace("Building density map op with type {}", densityType);
    String baseChannelName;
    ImageChannel lastChannel = null;
    List<ImageOp> sequentialOps = new ArrayList<>();
    switch(densityType) {
        case GAUSSIAN:
            if (radius > 0) {
                double sigma = radius;
                sequentialOps.add(ImageOps.Filters.gaussianBlur(sigma));
                // Scale so that central value ~1 - this is a closer match to the alternative sum filter
                sequentialOps.add(ImageOps.Core.multiply(2 * Math.PI * sigma * sigma));
            }
            baseChannelName = "Gaussian weighted counts ";
            break;
        case PERCENT:
            int[] extractInds = IntStream.range(0, primaryObjects.size()).toArray();
            if (radius > 0) {
                sequentialOps.add(ImageOps.Filters.sum(radius));
                sequentialOps.add(ImageOps.Core.round());
            }
            if (extractInds.length > 0) {
                // Duplicate the image, then
                // - On the first duplicate, remove the last channel and divide all the other channels by its values
                // - On the second duplicate, extract the last channel (so as to retain its values)
                // Finally merge the last channel back
                // The outcome should be that the last channel is unchanged, while the other channels are divided by the last
                // channel elementwise.
                sequentialOps.addAll(Arrays.asList(ImageOps.Core.splitMerge(ImageOps.Core.sequential(ImageOps.Core.splitDivide(ImageOps.Channels.extract(extractInds), ImageOps.Core.sequential(ImageOps.Channels.extract(extractInds.length), ImageOps.Channels.repeat(extractInds.length))), // Convert to percent
                ImageOps.Core.multiply(100.0)), ImageOps.Channels.extract(extractInds.length))));
            } else {
                // Values will be 1 and NaN (a possibly-zero value being divided by itself)
                sequentialOps.add(ImageOps.Core.splitDivide(null, null));
            }
            baseChannelName = "";
            if (!primaryObjects.isEmpty())
                lastChannel = ImageChannel.getInstance(DensityMaps.CHANNEL_ALL_OBJECTS, null);
            break;
        case SUM:
        default:
            if (radius > 0) {
                sequentialOps.add(ImageOps.Filters.sum(radius));
                sequentialOps.add(ImageOps.Core.round());
            }
            baseChannelName = "";
    }
    var channelNames = primaryObjects.keySet().stream().map(n -> baseChannelName + n).toArray(String[]::new);
    var channels = new ArrayList<>(ImageChannel.getChannelList(channelNames));
    if (lastChannel != null)
        channels.add(lastChannel);
    if (channels.isEmpty())
        this.channels = Collections.singletonList(ImageChannel.getInstance(DensityMaps.CHANNEL_ALL_OBJECTS, null));
    else
        this.channels = Collections.unmodifiableList(channels);
    sequentialOps.add(ImageOps.Core.ensureType(PixelType.FLOAT32));
    this.op = ImageOps.Core.sequential(sequentialOps);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) org.bytedeco.opencv.global.opencv_core(org.bytedeco.opencv.global.opencv_core) LoggerFactory(org.slf4j.LoggerFactory) ImageChannel(qupath.lib.images.servers.ImageChannel) ImageDataOp(qupath.opencv.ops.ImageDataOp) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ImageOps(qupath.opencv.ops.ImageOps) Point2(qupath.lib.geom.Point2) DensityMapType(qupath.lib.analysis.heatmaps.DensityMaps.DensityMapType) Map(java.util.Map) Mat(org.bytedeco.opencv.opencv_core.Mat) Scalar(org.bytedeco.opencv.opencv_core.Scalar) URI(java.net.URI) DoubleIndexer(org.bytedeco.javacpp.indexer.DoubleIndexer) ImageData(qupath.lib.images.ImageData) Logger(org.slf4j.Logger) BufferedImage(java.awt.image.BufferedImage) RegionRequest(qupath.lib.regions.RegionRequest) PixelType(qupath.lib.images.servers.PixelType) Collection(java.util.Collection) ImageOp(qupath.opencv.ops.ImageOp) IOException(java.io.IOException) Padding(qupath.lib.regions.Padding) Collectors(java.util.stream.Collectors) PathObjectTools(qupath.lib.objects.PathObjectTools) Objects(java.util.Objects) ROI(qupath.lib.roi.interfaces.ROI) List(java.util.List) PathObjectPredicate(qupath.lib.objects.PathObjectPredicates.PathObjectPredicate) Collections(java.util.Collections) ImageOp(qupath.opencv.ops.ImageOp) ImageChannel(qupath.lib.images.servers.ImageChannel) ArrayList(java.util.ArrayList)

Example 3 with ImageOp

use of qupath.opencv.ops.ImageOp in project qupath by qupath.

the class DensityMapDataOp method appendOps.

@Override
public ImageDataOp appendOps(ImageOp... ops) {
    if (ops.length == 0)
        return this;
    ImageOp opNew;
    if (ops.length > 1)
        opNew = ImageOps.Core.sequential(ops);
    else
        opNew = ops[0];
    var dataOp = new DensityMapDataOp(radius, primaryObjects, allObjects, densityType);
    dataOp.op = ImageOps.Core.sequential(dataOp.op, opNew);
    dataOp.channels = opNew.getChannels(dataOp.channels);
    return dataOp;
}
Also used : ImageOp(qupath.opencv.ops.ImageOp)

Example 4 with ImageOp

use of qupath.opencv.ops.ImageOp in project qupath by qupath.

the class OpenCVDnn method createBlobFunction.

private BlobFunction<Mat> createBlobFunction() {
    var ops = new ArrayList<ImageOp>();
    if (mean != null) {
        // TODO: Trim scalar array if needed
        double[] scalarArray = new double[4];
        mean.get(scalarArray);
        ops.add(ImageOps.Core.subtract(scalarArray));
    }
    if (scale != 1)
        ops.add(ImageOps.Core.multiply(scale));
    ImageOp preprocess;
    if (ops.isEmpty())
        preprocess = null;
    else if (ops.size() == 1)
        preprocess = ops.get(0);
    else
        preprocess = ImageOps.Core.sequential(ops);
    return new DefaultBlobFunction(preprocess, size, crop);
}
Also used : ImageOp(qupath.opencv.ops.ImageOp) ArrayList(java.util.ArrayList)

Aggregations

ImageOp (qupath.opencv.ops.ImageOp)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)2 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 DoubleIndexer (org.bytedeco.javacpp.indexer.DoubleIndexer)1 org.bytedeco.opencv.global.opencv_core (org.bytedeco.opencv.global.opencv_core)1 Mat (org.bytedeco.opencv.opencv_core.Mat)1 Scalar (org.bytedeco.opencv.opencv_core.Scalar)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1