Search in sources :

Example 1 with Histogram

use of javax.media.jai.Histogram in project snap-idepix by bcdev.

the class Landsat8Utils method getHistogramBinAtNPercentOfMaximum.

/**
 * Computes the histogram bin where histogram value is N percent of histogram maximum value
 *
 * @param stx - the statistics object
 * @return - the bin value
 */
static double getHistogramBinAtNPercentOfMaximum(Stx stx) {
    final Histogram h = stx.getHistogram();
    final double highValue = h.getHighValue()[0];
    final double lowValue = h.getLowValue()[0];
    final int numBins = h.getNumBins(0);
    final double binWidth = (highValue - lowValue) / numBins;
    final double peakValue = getHistogramPeakValue(h);
    for (int i = numBins - 1; i >= 0; i--) {
        final double currValue = highValue - (numBins - i) * binWidth;
        if (h.getBins()[0][i] >= 3.0 * peakValue / 100.0) {
            return currValue;
        }
    }
    return peakValue;
}
Also used : Histogram(javax.media.jai.Histogram)

Example 2 with Histogram

use of javax.media.jai.Histogram in project imageio-ext by geosolutions-it.

the class JP2KakaduReadTest method displayStatistics.

public static void displayStatistics(boolean b, RenderedImage source) {
    PlanarImage img = JAI.create("extrema", source, null);
    double[] maximum = (double[]) img.getProperty("maximum");
    double[] minimum = (double[]) img.getProperty("minimum");
    ParameterBlock pb = (new ParameterBlock()).addSource(source);
    pb.add(null).add(1).add(1).add(new int[] { 65536 });
    pb.add(new double[] { minimum[0] }).add(new double[] { maximum[0] });
    PlanarImage dst = JAI.create("histogram", pb);
    Histogram h = (Histogram) dst.getProperty("hiStOgRam");
    JFrame frame = new HistogramFrame(h, b);
    frame.pack();
    frame.show();
}
Also used : Histogram(javax.media.jai.Histogram) ParameterBlock(java.awt.image.renderable.ParameterBlock) JFrame(javax.swing.JFrame) PlanarImage(javax.media.jai.PlanarImage)

Example 3 with Histogram

use of javax.media.jai.Histogram in project structr by structr.

the class GetWCSHistogramFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        assertArrayHasMinLengthAndTypes(sources, 3, String.class, String.class, Geometry.class, Number.class, Number.class, Number.class);
        final String baseUrl = (String) sources[0];
        final String coverageId = (String) sources[1];
        final Geometry geometry = (Geometry) sources[2];
        final GridCoverage2D coverage = getWCSCoverage(baseUrl, coverageId, geometry);
        final CoverageProcessor processor = CoverageProcessor.getInstance();
        final Operation operation = processor.getOperation("Histogram");
        final ParameterValueGroup params = operation.getParameters();
        final int numBins = sources.length > 3 ? parseInt(sources[3], 256) : 256;
        final double lowValue = sources.length > 4 ? parseDouble(sources[4], 0.0) : 0.0;
        final double cutoffPercentage = sources.length > 5 ? parseDouble(sources[5], 0.005) : 0.005;
        final double[] extrema = getExtrema(coverage, 0);
        params.parameter("source0").setValue(coverage);
        params.parameter("numBins").setValue(new int[] { numBins });
        params.parameter("lowValue").setValue(new double[] { lowValue });
        params.parameter("highValue").setValue(new double[] { extrema[1] });
        final NumberFormat format = new DecimalFormat("#,###,##0.00");
        final GridCoverage2D result = (GridCoverage2D) processor.doOperation(params);
        final Histogram histogram = (Histogram) result.getProperty("histogram");
        final int[] bins = histogram.getBins(0);
        final Double binWidth = histogram.getHighValue()[0] / (double) numBins;
        final Map<String, Object> map = new LinkedHashMap<>();
        List<String> binNames = new LinkedList<>();
        List<Integer> binData = new LinkedList<>();
        int lastIndex = 0;
        int restBin = 0;
        double maxCount = 0;
        for (int i = 0; i < bins.length; i++) {
            final double d = i;
            binNames.add(format.format(binWidth * d));
            binData.add(bins[i]);
            if (bins[i] > maxCount) {
                maxCount = bins[i];
            }
        }
        // combine all bins whose value is below a given threshold (maxCount * cutoffPercentage)
        int threshold = Double.valueOf(maxCount * cutoffPercentage).intValue();
        for (int i = 0; i < bins.length; i++) {
            // find index of last bin with more than x elements
            if (bins[i] > threshold) {
                lastIndex = i;
            }
        }
        // collect the sum of all the bins below the threshold
        for (int i = lastIndex; i < bins.length; i++) {
            restBin += bins[i];
        }
        binNames = binNames.subList(0, lastIndex);
        binData = binData.subList(0, lastIndex);
        // add a single bin with all the rest
        binNames.add("> " + format.format(binWidth * lastIndex));
        binData.add(restBin);
        // remove all bins that have less than threshold elements
        map.put("names", binNames);
        map.put("bins", binData);
        return map;
    } catch (ArgumentNullException pe) {
        // silently ignore null arguments
        return "";
    } catch (ArgumentCountException pe) {
        logParameterError(caller, sources, pe.getMessage(), ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    } catch (ArgumentTypeException te) {
        logParameterError(caller, sources, te.getMessage(), ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) Histogram(javax.media.jai.Histogram) CoverageProcessor(org.geotools.coverage.processing.CoverageProcessor) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) DecimalFormat(java.text.DecimalFormat) Operation(org.opengis.coverage.processing.Operation) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) Geometry(com.vividsolutions.jts.geom.Geometry) ArgumentNullException(org.structr.common.error.ArgumentNullException) ArgumentCountException(org.structr.common.error.ArgumentCountException) NumberFormat(java.text.NumberFormat) ArgumentTypeException(org.structr.common.error.ArgumentTypeException)

Example 4 with Histogram

use of javax.media.jai.Histogram in project geowave by locationtech.

the class GeoWaveRasterReader method loadTiles.

/**
 * @param backgroundColor the background color
 * @param outputTransparentColor the transparent color
 * @param pixelDimension
 * @return the gridcoverage as the final result
 * @throws IOException
 */
private GridCoverage2D loadTiles(final String coverageName, final Color backgroundColor, final Color outputTransparentColor, Interpolation interpolation, final Rectangle pixelDimension, final GeoWaveRasterReaderState state, final CoordinateReferenceSystem crs, final GeneralEnvelope originalEnvelope) throws IOException {
    transformRequestEnvelope(state, crs);
    // /////////////////////////////////////////////////////////////////////
    if (!state.getRequestEnvelopeXformed().intersects(originalEnvelope, true)) {
        LOGGER.warn("The requested envelope does not intersect the envelope of this mosaic");
        LOGGER.warn(state.getRequestEnvelopeXformed().toString());
        LOGGER.warn(originalEnvelope.toString());
        return null;
    }
    final ImageReadParam readP = new ImageReadParam();
    final Integer imageChoice;
    final RasterDataAdapter adapter = (RasterDataAdapter) geowaveAdapterStore.getAdapter(getAdapterId(coverageName)).getAdapter();
    if (pixelDimension != null) {
        try {
            synchronized (this) {
                if (!setupResolutions(coverageName)) {
                    LOGGER.warn("Cannot find the overview statistics for the requested coverage name");
                    return coverageFactory.create(coverageName, RasterUtils.getEmptyImage((int) pixelDimension.getWidth(), (int) pixelDimension.getHeight(), backgroundColor, outputTransparentColor, adapter.getColorModel()), state.getRequestedEnvelope());
                }
                imageChoice = setReadParams(state.getCoverageName(), OverviewPolicy.getDefaultPolicy(), readP, state.getRequestEnvelopeXformed(), pixelDimension);
            }
            readP.setSourceSubsampling(1, 1, 0, 0);
        } catch (final TransformException e) {
            LOGGER.error(e.getLocalizedMessage(), e);
            return coverageFactory.create(coverageName, RasterUtils.getEmptyImage((int) pixelDimension.getWidth(), (int) pixelDimension.getHeight(), backgroundColor, outputTransparentColor, adapter.getColorModel()), state.getRequestedEnvelope());
        }
    } else {
        imageChoice = Integer.valueOf(0);
    }
    final double[][] resolutionLevels = getResolutionLevels(coverageName);
    final Histogram histogram;
    boolean equalizeHistogram;
    if (config.isEqualizeHistogramOverrideSet()) {
        equalizeHistogram = config.isEqualizeHistogramOverride();
    } else {
        equalizeHistogram = adapter.isEqualizeHistogram();
    }
    if (equalizeHistogram) {
        histogram = getHistogram(coverageName, resolutionLevels[imageChoice.intValue()][0], resolutionLevels[imageChoice.intValue()][1]);
    } else {
        histogram = null;
    }
    // default to always scale to 8-bit
    boolean scaleTo8Bit = true;
    final boolean scaleTo8BitSet = config.isScaleTo8BitSet();
    if (scaleTo8BitSet) {
        scaleTo8Bit = config.isScaleTo8Bit();
    }
    try (final CloseableIterator<GridCoverage> gridCoverageIt = queryForTiles(pixelDimension, state.getRequestEnvelopeXformed(), resolutionLevels[imageChoice.intValue()][0], resolutionLevels[imageChoice.intValue()][1], adapter)) {
        // allow the config to override the WMS request
        if (config.isInterpolationOverrideSet()) {
            interpolation = config.getInterpolationOverride();
        } else // WMS request
        if (interpolation == null) {
            interpolation = adapter.getInterpolation();
        }
        final GridCoverage2D result = RasterUtils.mosaicGridCoverages(gridCoverageIt, backgroundColor, outputTransparentColor, pixelDimension, state.getRequestEnvelopeXformed(), resolutionLevels[imageChoice.intValue()][0], resolutionLevels[imageChoice.intValue()][1], adapter.getNoDataValuesPerBand(), state.isAxisSwapped(), coverageFactory, state.getCoverageName(), interpolation, histogram, scaleTo8BitSet, scaleTo8Bit, adapter.getColorModel());
        return transformResult(result, pixelDimension, state);
    }
}
Also used : RasterDataAdapter(org.locationtech.geowave.adapter.raster.adapter.RasterDataAdapter) ImageReadParam(javax.imageio.ImageReadParam) Histogram(javax.media.jai.Histogram) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GridCoverage(org.opengis.coverage.grid.GridCoverage) TransformException(org.opengis.referencing.operation.TransformException)

Aggregations

Histogram (javax.media.jai.Histogram)4 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 ParameterBlock (java.awt.image.renderable.ParameterBlock)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 ImageReadParam (javax.imageio.ImageReadParam)1 PlanarImage (javax.media.jai.PlanarImage)1 JFrame (javax.swing.JFrame)1 CoverageProcessor (org.geotools.coverage.processing.CoverageProcessor)1 RasterDataAdapter (org.locationtech.geowave.adapter.raster.adapter.RasterDataAdapter)1 GridCoverage (org.opengis.coverage.grid.GridCoverage)1 Operation (org.opengis.coverage.processing.Operation)1 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)1 TransformException (org.opengis.referencing.operation.TransformException)1 ArgumentCountException (org.structr.common.error.ArgumentCountException)1 ArgumentNullException (org.structr.common.error.ArgumentNullException)1 ArgumentTypeException (org.structr.common.error.ArgumentTypeException)1