use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsGradientIM method processCell.
@Override
protected void processCell(int readCol, int readRow, int writeCol, int writeRow, int readCols, int readRows, int writeCols, int writeRows) {
RandomIter elevIter = inRasterIterators.get(0);
double gradient;
switch(pMode) {
case 1:
gradient = OmsGradient.doGradientHornOnCell(elevIter, readCol, readRow, xRes, yRes, doDegrees);
break;
case 2:
gradient = OmsGradient.doGradientEvansOnCell(elevIter, readCol, readRow, xRes, yRes, doDegrees);
break;
default:
gradient = OmsGradient.doGradientDiffOnCell(elevIter, readCol, readRow, xRes, yRes, doDegrees);
break;
}
WritableRandomIter outDataIter = outRasterIterators.get(0);
outDataIter.setSample(writeCol, writeRow, 0, gradient);
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class WatershedAlgorithm method process.
@Execute
public void process() throws Exception {
checkNull(inRaster, outRaster);
GridCoverage2D inRasterGC = getRaster(inRaster);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRasterGC);
int nCols = regionMap.getCols();
int nRows = regionMap.getRows();
byte[] input = CoverageUtilities.renderedImage2ByteArray(inRasterGC.getRenderedImage(), true);
/**
* First step : the pixels are sorted according to increasing grey values *
*/
WatershedStructure watershedStructure = new WatershedStructure(input, nCols, nRows, pm);
/**
* Start flooding *
*/
WatershedFIFO queue = new WatershedFIFO();
int curlab = 0;
int heightIndex1 = 0;
int heightIndex2 = 0;
for (int h = 0; h < 256; h++) /*Geodesic SKIZ of level h-1 inside level h */
{
for (int pixelIndex = heightIndex1; pixelIndex < watershedStructure.size(); pixelIndex++) /*mask all pixels at level h*/
{
WatershedPixel p = watershedStructure.get(pixelIndex);
if (p.getIntHeight() != h) {
/**
* This pixel is at level h+1 *
*/
heightIndex1 = pixelIndex;
break;
}
p.setLabelToMASK();
List<WatershedPixel> neighbours = p.getNeighbours();
for (int i = 0; i < neighbours.size(); i++) {
WatershedPixel q = (WatershedPixel) neighbours.get(i);
if (q.getLabel() >= 0) {
/*Initialise queue with neighbours at level h of current basins or watersheds*/
p.setDistance(1);
queue.fifo_add(p);
break;
}
// end if
}
// end for
}
// end for
int curdist = 1;
queue.fifo_add_FICTITIOUS();
while (true) /**
* extend basins *
*/
{
WatershedPixel p = queue.fifo_remove();
if (p.isFICTITIOUS())
if (queue.fifo_empty())
break;
else {
queue.fifo_add_FICTITIOUS();
curdist++;
p = queue.fifo_remove();
}
List<WatershedPixel> neighbours = p.getNeighbours();
for (int i = 0; i < neighbours.size(); i++) /* Labelling p by inspecting neighbours */
{
WatershedPixel q = (WatershedPixel) neighbours.get(i);
/* Original algorithm :
if( (q.getDistance() < curdist) &&
(q.getLabel()>0 || q.isLabelWSHED()) ) {*/
if ((q.getDistance() <= curdist) && (q.getLabel() >= 0)) {
if (q.getLabel() > 0) {
if (p.isLabelMASK())
// Removed from original algorithm || p.isLabelWSHED() )
p.setLabel(q.getLabel());
else if (p.getLabel() != q.getLabel())
p.setLabelToWSHED();
} else // end if lab>0
if (p.isLabelMASK())
p.setLabelToWSHED();
} else if (q.isLabelMASK() && (q.getDistance() == 0)) {
q.setDistance(curdist + 1);
queue.fifo_add(q);
}
}
// end for, end processing neighbours
}
/* Detect and process new minima at level h */
for (int pixelIndex = heightIndex2; pixelIndex < watershedStructure.size(); pixelIndex++) {
WatershedPixel p = watershedStructure.get(pixelIndex);
if (p.getIntHeight() != h) {
/**
* This pixel is at level h+1 *
*/
heightIndex2 = pixelIndex;
break;
}
p.setDistance(0);
if (p.isLabelMASK()) {
/* the pixel is inside a new minimum */
curlab++;
p.setLabel(curlab);
queue.fifo_add(p);
while (!queue.fifo_empty()) {
WatershedPixel q = queue.fifo_remove();
List<WatershedPixel> neighbours = q.getNeighbours();
for (int i = 0; i < neighbours.size(); i++) /* inspect neighbours of p2*/
{
WatershedPixel r = (WatershedPixel) neighbours.get(i);
if (r.isLabelMASK()) {
r.setLabel(curlab);
queue.fifo_add(r);
}
}
}
// end while
}
// end if
}
// end for
}
/**
* End of flooding *
*/
WritableRaster outWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
pm.beginTask("Setting watersheds...", watershedStructure.size());
for (int pixelIndex = 0; pixelIndex < watershedStructure.size(); pixelIndex++) {
WatershedPixel p = watershedStructure.get(pixelIndex);
int c = p.getX();
int r = p.getY();
if (p.isLabelWSHED() && !p.allNeighboursAreWSHED()) {
if (doBorders)
outIter.setSample(c, r, 0, 1.0);
} else {
if (!doBorders)
outIter.setSample(c, r, 0, 1.0);
}
pm.worked(1);
}
pm.done();
outIter.done();
GridCoverage2D outRasterGC = CoverageUtilities.buildCoverage("normalized", outWR, regionMap, inRasterGC.getCoordinateReferenceSystem());
dumpRaster(outRasterGC, outRaster);
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class HMTestMaps method createGC.
private static GridCoverage2D createGC(double[][] data, Class<?> dataClass) {
RegionMap rm = getEnvelopeparams();
int nRows = rm.getRows();
int nCols = rm.getCols();
WritableRaster pitRaster = CoverageUtilities.createWritableRaster(nCols, nRows, dataClass, null, null);
WritableRandomIter iter = CoverageUtilities.getWritableRandomIterator(pitRaster);
for (int r = 0; r < nRows; r++) {
for (int c = 0; c < nCols; c++) {
double value = data[r][c];
iter.setSample(c, r, 0, value);
}
}
iter.done();
return CoverageUtilities.buildCoverage("gc", pitRaster, rm, crs);
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsExtractNetwork method extractNetMode2.
/**
* this method the network is extracted by considering only concave points
* as being part of the channel network.
* @throws Exception
*/
private WritableRaster extractNetMode2(RenderedImage flowRI, int novalue, RenderedImage tcaRI, RenderedImage classRI, RenderedImage slopeRI) throws Exception {
RandomIter flowRandomIter = RandomIterFactory.create(flowRI, null);
RandomIter tcaRandomIter = RandomIterFactory.create(tcaRI, null);
RandomIter classRandomIter = RandomIterFactory.create(classRI, null);
RandomIter slopeRandomIter = RandomIterFactory.create(slopeRI, null);
WritableRaster netImage = CoverageUtilities.createWritableRaster(cols, rows, Short.class, null, shortNovalue);
// try the operation!!
WritableRandomIter netRandomIter = RandomIterFactory.createWritable(netImage, null);
try {
// $NON-NLS-1$
pm.beginTask(msg.message("extractnetwork.extracting"), rows * cols);
processGrid(cols, rows, false, (c, r) -> {
if (pm.isCanceled()) {
return;
}
double tcaValue = tcaRandomIter.getSample(c, r, 0);
double slopeValue = slopeRandomIter.getSampleDouble(c, r, 0);
if (!isNovalue(tcaValue) && !isNovalue(slopeValue)) {
tcaValue = pow(tcaValue, pExp) * slopeValue;
if (tcaValue >= pThres && classRandomIter.getSample(c, r, 0) == 15) {
netRandomIter.setSample(c, r, 0, NETVALUE);
FlowNode flowNode = new FlowNode(flowRandomIter, cols, rows, c, r, novalue);
FlowNode runningNode = flowNode;
while ((runningNode = runningNode.goDownstream()) != null) {
int rCol = runningNode.col;
int rRow = runningNode.row;
short tmpNetValue = (short) netRandomIter.getSample(rCol, rRow, 0);
if (!isNovalue(tmpNetValue)) {
break;
}
if (runningNode.isMarkedAsOutlet()) {
netRandomIter.setSample(rCol, rRow, 0, NETVALUE);
break;
} else if (runningNode.touchesBound()) {
Node goDownstream = runningNode.goDownstream();
if (goDownstream == null || !goDownstream.isValid()) {
netRandomIter.setSample(rCol, rRow, 0, NETVALUE);
break;
}
}
netRandomIter.setSample(rCol, rRow, 0, NETVALUE);
}
}
}
pm.worked(1);
});
pm.done();
return netImage;
} finally {
flowRandomIter.done();
tcaRandomIter.done();
classRandomIter.done();
slopeRandomIter.done();
netRandomIter.done();
}
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsExtractNetwork method extractNetTcaThreshold.
/**
* this method calculates the network using a threshold value on the
* contributing areas or on magnitudo
* @throws Exception
*/
private WritableRaster extractNetTcaThreshold(RenderedImage tcaRI) throws Exception {
RandomIter tcaIter = RandomIterFactory.create(tcaRI, null);
WritableRaster netWR = CoverageUtilities.createWritableRaster(cols, rows, Short.class, null, shortNovalue);
WritableRandomIter netIter = RandomIterFactory.createWritable(netWR, null);
try {
// $NON-NLS-1$
pm.beginTask(msg.message("extractnetwork.extracting"), rows * cols);
processGrid(cols, rows, false, (c, r) -> {
if (pm.isCanceled()) {
return;
}
int tcaValue = tcaIter.getSample(c, r, 0);
if (!isNovalue(tcaValue)) {
if (tcaValue >= pThres) {
// FIXME needs power here?
netIter.setSample(c, r, 0, NETVALUE);
}
}
pm.worked(1);
});
pm.done();
return netWR;
} finally {
netIter.done();
tcaIter.done();
}
}
Aggregations