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;
}
Aggregations