Search in sources :

Example 1 with Padding

use of qupath.lib.regions.Padding in project qupath by qupath.

the class DensityMapDataOp method apply.

@Override
public Mat apply(ImageData<BufferedImage> imageData, RegionRequest request) throws IOException {
    ensureInitialized();
    logger.trace("Applying density map op for {}", request);
    // Calculate how much padding we need
    var padding = op.getPadding();
    if (!padding.isEmpty()) {
        // Add padding to the request
        double downsample = request.getDownsample();
        var padding2 = Padding.getPadding((int) Math.round(padding.getX1() * downsample), (int) Math.round(padding.getX2() * downsample), (int) Math.round(padding.getY1() * downsample), (int) Math.round(padding.getY2() * downsample));
        request = request.pad2D(padding2);
    }
    // Get all objects within the padded region
    var allPathObjects = imageData.getHierarchy().getObjectsForRegion(null, request, null).stream().filter(allObjects).collect(Collectors.toList());
    if (allPathObjects.size() == 1)
        logger.trace("Generating counts tile for 1 object");
    else
        logger.trace("Generating counts tile for {} objects", allPathObjects.size());
    // Create an output mat
    int nChannels = getChannelCount();
    int width = (int) Math.round(request.getWidth() / request.getDownsample());
    int height = (int) Math.round(request.getHeight() / request.getDownsample());
    var mat = new Mat(height, width, opencv_core.CV_64FC(nChannels), Scalar.ZERO);
    DoubleIndexer idx = mat.createIndexer();
    // Get points representing all the centroids of each subpopulation of object
    // Use these to increment pixel values in a counts image
    int c = 0;
    for (var entry : primaryObjects.entrySet()) {
        var predicate = entry.getValue();
        var primaryROIs = allPathObjects.stream().filter(predicate).map(p -> PathObjectTools.getROI(p, true)).collect(Collectors.toList());
        var points = objectsToPoints(primaryROIs);
        incrementCounts(idx, points, request, width, height, c);
        c++;
    }
    // Get points for all objects, if we need them
    if (c < getChannelCount()) {
        var allObjectROIs = allPathObjects.stream().map(p -> PathObjectTools.getROI(p, true)).collect(Collectors.toList());
        var points = objectsToPoints(allObjectROIs);
        incrementCounts(idx, points, request, width, height, c);
        c++;
    }
    idx.close();
    // Now apply the op
    var output = this.op.apply(mat);
    return output;
}
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) Mat(org.bytedeco.opencv.opencv_core.Mat) DoubleIndexer(org.bytedeco.javacpp.indexer.DoubleIndexer)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)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 DensityMapType (qupath.lib.analysis.heatmaps.DensityMaps.DensityMapType)1