Example usage for java.awt.image WritableRaster getSampleDouble

List of usage examples for java.awt.image WritableRaster getSampleDouble

Introduction

In this page you can find the example usage for java.awt.image WritableRaster getSampleDouble.

Prototype

public double getSampleDouble(int x, int y, int b) 

Source Link

Document

Returns the sample in a specified band for a pixel located at (x,y) as a double.

Usage

From source file:qupath.lib.gui.panels.classify.RandomTrainingRegionSelector.java

public static Map<Integer, List<PathObject>> objectClusterer(final Collection<PathObject> pathObjects,
        final BufferedImage imgThumbnail, final double thumbScaleX, final double thumbScaleY,
        final int nClusters) {

    Map<Integer, List<PathObject>> map = new HashMap<>();
    if (pathObjects.isEmpty())
        return map;

    if (nClusters <= 1 || pathObjects.size() == 1) {
        map.put(Integer.valueOf(0), new ArrayList<>(pathObjects));
        return map;
    }//from   w  ww  .j  a va  2 s  . co m

    //      int maxIterations = 100;

    KMeansPlusPlusClusterer<ClusterableObject> km = new KMeansPlusPlusClusterer<>(nClusters);
    List<ClusterableObject> clusterableObjects = new ArrayList<>();
    WritableRaster raster = imgThumbnail.getRaster();
    int nChannels = raster.getNumBands();
    double[] valueBuffer = new double[nChannels];
    int w = imgThumbnail.getWidth();
    int h = imgThumbnail.getHeight();
    boolean isRGB = imgThumbnail.getSampleModel().getNumBands() == 3
            && imgThumbnail.getSampleModel().getSampleSize(0) == 8;

    for (PathObject pathObject : pathObjects) {
        // Get pixel values for the ROI centroid
        // CIE LAB is used rather than RGB where possible, due to better suitability for Euclidean distances
        ROI roi = pathObject.getROI();
        if (roi == null)
            continue;
        int x = (int) (roi.getCentroidX() * thumbScaleX + 0.5);
        int y = (int) (roi.getCentroidY() * thumbScaleY + 0.5);
        if (x < 0 || x >= w || y < 0 || y >= h)
            continue;

        if (isRGB)
            valueBuffer = makeCIELAB(imgThumbnail.getRGB(x, y), valueBuffer);
        else {
            for (int c = 0; c < nChannels; c++)
                valueBuffer[c] = raster.getSampleDouble(x, y, c);
        }

        clusterableObjects.add(new ClusterableObject(pathObject, valueBuffer));
    }
    List<CentroidCluster<ClusterableObject>> results = km.cluster(clusterableObjects);

    int i = 0;
    for (CentroidCluster<ClusterableObject> centroidCluster : results) {
        Integer label = Integer.valueOf(i);
        List<PathObject> objects = new ArrayList<>();
        for (ClusterableObject co : centroidCluster.getPoints())
            objects.add(co.getPathObject());
        map.put(label, objects);
        i++;
    }

    return map;
}