Example usage for java.awt.image WritableRaster getNumBands

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

Introduction

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

Prototype

public final int getNumBands() 

Source Link

Document

Returns the number of bands (samples per pixel) in this Raster.

Usage

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage invert(BufferedImage inImg) {
    if (inImg == null) {
        return null;
    }/* w ww  .ja v  a  2  s .  c o  m*/

    int width = inImg.getWidth();
    int height = inImg.getHeight();
    BufferedImage outImg = new BufferedImage(width, height, inImg.getType());
    WritableRaster outRaster = outImg.getRaster();
    WritableRaster inRaster = inImg.getRaster();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            for (int i = 0; i < outRaster.getNumBands(); i++) {
                outRaster.setSample(x, y, i, 255 - inRaster.getSample(x, y, i));
            }
        }
    }

    return outImg;
}

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 w w .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;
}