use of net.imglib2.Cursor in project imagej-ops by imagej.
the class DefaultLBP2D method compute.
@SuppressWarnings("unchecked")
@Override
public void compute(RandomAccessibleInterval<I> input, ArrayList<LongType> output) {
ArrayList<LongType> numberList = new ArrayList<>();
RandomAccess<I> raInput = Views.extendZero(input).randomAccess();
final Cursor<I> cInput = Views.flatIterable(input).cursor();
final ClockwiseDistanceNeighborhoodIterator<I> cNeigh = new ClockwiseDistanceNeighborhoodIterator<>(raInput, distance);
while (cInput.hasNext()) {
cInput.next();
double centerValue = cInput.get().getRealDouble();
int resultBinaryValue = 0;
cNeigh.reset();
while (cNeigh.hasNext()) {
double nValue = cNeigh.next().getRealDouble();
int pos = cNeigh.getIndex();
if (nValue >= centerValue) {
resultBinaryValue |= (1 << pos);
}
}
numberList.add(new LongType(resultBinaryValue));
}
Histogram1d<Integer> hist = histOp.calculate(numberList);
Iterator<LongType> c = hist.iterator();
while (c.hasNext()) {
output.add(new LongType(c.next().get()));
}
}
use of net.imglib2.Cursor in project imagej-ops by imagej.
the class DefaultBilateral method compute.
@Override
public void compute(final RandomAccessibleInterval<I> input, final RandomAccessibleInterval<O> output) {
final long[] size = new long[input.numDimensions()];
input.dimensions(size);
final RandomAccess<O> outputRA = output.randomAccess();
final Cursor<I> inputCursor = Views.iterable(input).localizingCursor();
final long[] currentPos = new long[input.numDimensions()];
final long[] neighborhoodPos = new long[input.numDimensions()];
final long[] neighborhoodMin = new long[input.numDimensions()];
final long[] neighborhoodMax = new long[input.numDimensions()];
Neighborhood<I> neighborhood;
Cursor<I> neighborhoodCursor;
final RectangleNeighborhoodFactory<I> fac = RectangleNeighborhood.factory();
while (inputCursor.hasNext()) {
inputCursor.fwd();
inputCursor.localize(currentPos);
double distance;
inputCursor.localize(neighborhoodMin);
inputCursor.localize(neighborhoodMax);
neighborhoodMin[0] = Math.max(0, neighborhoodMin[0] - radius);
neighborhoodMin[1] = Math.max(0, neighborhoodMin[1] - radius);
neighborhoodMax[0] = Math.min(input.max(0), neighborhoodMax[0] + radius);
neighborhoodMax[1] = Math.min(input.max(1), neighborhoodMax[1] + radius);
final Interval interval = new FinalInterval(neighborhoodMin, neighborhoodMax);
neighborhood = fac.create(currentPos, neighborhoodMin, neighborhoodMax, interval, input.randomAccess());
neighborhoodCursor = neighborhood.localizingCursor();
double weight, v = 0.0;
double w = 0.0;
do {
neighborhoodCursor.fwd();
neighborhoodCursor.localize(neighborhoodPos);
distance = getDistance(currentPos, neighborhoodPos);
// spatial kernel
weight = gauss(distance, sigmaS);
// intensity
distance = Math.abs(inputCursor.get().getRealDouble() - neighborhoodCursor.get().getRealDouble());
// difference
// range kernel, then exponent addition
weight *= gauss(distance, sigmaR);
v += weight * neighborhoodCursor.get().getRealDouble();
w += weight;
} while (neighborhoodCursor.hasNext());
outputRA.setPosition(currentPos);
outputRA.get().setReal(v / w);
}
}
use of net.imglib2.Cursor in project imagej-ops by imagej.
the class CentroidII method calculate.
@Override
public RealLocalizable calculate(final IterableInterval<?> input) {
int numDimensions = input.numDimensions();
double[] output = new double[numDimensions];
Cursor<?> c = input.localizingCursor();
double[] pos = new double[numDimensions];
while (c.hasNext()) {
c.fwd();
c.localize(pos);
for (int i = 0; i < output.length; i++) {
output[i] += pos[i];
}
}
for (int i = 0; i < output.length; i++) {
output[i] = output[i] / input.size();
}
return new RealPoint(output);
}
use of net.imglib2.Cursor in project imagej-ops by imagej.
the class DefaultCreateKernel2ndDerivBiGauss method calculate.
@Override
public RandomAccessibleInterval<T> calculate(final double[] sigmas, final Integer dimensionality) {
// both sigmas must be available
if (sigmas.length < 2)
throw new IllegalArgumentException("Two sigmas (for inner and outer Gauss)" + " must be supplied.");
// both sigmas must be reasonable
if (sigmas[0] <= 0 || sigmas[1] <= 0)
throw new IllegalArgumentException("Input sigmas must be both positive.");
// dimension as well...
if (dimensionality <= 0)
throw new IllegalArgumentException("Input dimensionality must both positive.");
// the size and center of the output image
final long[] dims = new long[dimensionality];
final long[] centre = new long[dimensionality];
// time-saver... (must hold now: dimensionality > 0)
// NB: size of the image is 2px wider than for 0th order BiGauss to have
// some space for smooth approach-to-zero at the kernel image borders
dims[0] = Math.max(3, (2 * (int) (sigmas[0] + 3 * sigmas[1] + 0.5) + 1)) + 2;
centre[0] = (int) (dims[0] / 2);
// fill the size and center arrays
for (int d = 1; d < dims.length; d++) {
dims[d] = dims[0];
centre[d] = centre[0];
}
// prepare some scaling constants
/*
//orig full math version:
final double k = (sigmas[1]/sigmas[0]) * (sigmas[1]/sigmas[0]); //eq. (6)
final double[] C = { 1.0/(2.50663*sigmas[0]*sigmas[0]*sigmas[0]), 1.0/(2.50663*sigmas[1]*sigmas[1]*sigmas[1]) };
//2.50663 = sqrt(2*PI)
*/
// less math version:
// note that originally there was C[0] for inner Gauss, k*C[1] for outer Gauss
// we get rid of k by using new C[0] and C[1]:
final double[] C = { 1.0 / (2.50663 * sigmas[0] * sigmas[0] * sigmas[0]), 1.0 / (2.50663 * sigmas[1] * sigmas[0] * sigmas[0]) };
// prepare squared input sigmas
final double[] sigmasSq = { sigmas[0] * sigmas[0], sigmas[1] * sigmas[1] };
// prepare the output image
final RandomAccessibleInterval<T> out = createImgOp.calculate(new FinalInterval(dims));
// fill the output image
final Cursor<T> cursor = Views.iterable(out).cursor();
while (cursor.hasNext()) {
cursor.fwd();
// obtain the current coordinate (use dims to store it)
cursor.localize(dims);
// calculate distance from the image centre
// TODO: can JVM reuse this var or is it allocated again and again (and multipling in the memory)?
double dist = 0.;
for (int d = 0; d < dims.length; d++) {
final double dx = dims[d] - centre[d];
dist += dx * dx;
}
// dist = Math.sqrt(dist); -- gonna work with squared distance
// which of the two Gaussians should we use?
double val = 0.;
if (dist < sigmasSq[0]) {
// the inner one
val = (dist / sigmasSq[0]) - 1.0;
val *= C[0] * Math.exp(-0.5 * dist / sigmasSq[0]);
} else {
// the outer one, get new distance first:
dist = Math.sqrt(dist) - (sigmas[0] - sigmas[1]);
dist *= dist;
val = (dist / sigmasSq[1]) - 1.0;
val *= C[1] * Math.exp(-0.5 * dist / sigmasSq[1]);
}
// compose the real value finally
cursor.get().setReal(val);
}
return out;
}
use of net.imglib2.Cursor in project imagej-ops by imagej.
the class DefaultCreateKernelLog method calculate.
@Override
public RandomAccessibleInterval<T> calculate(double[] sigmas) {
final double[] sigmaPixels = new double[sigmas.length];
for (int i = 0; i < sigmaPixels.length; i++) {
// Optimal sigma for LoG approach and dimensionality.
final double sigma_optimal = sigmas[i] / Math.sqrt(sigmas.length);
sigmaPixels[i] = sigma_optimal;
}
final int n = sigmaPixels.length;
final long[] dims = new long[n];
final long[] middle = new long[n];
for (int d = 0; d < n; ++d) {
// The half size of the kernel is 3 standard deviations (or a
// minimum half size of 2)
final int hksizes = Math.max(2, (int) (3 * sigmaPixels[d] + 0.5) + 1);
// add 3 border pixels to achieve smoother derivatives at the border
dims[d] = 3 + 2 * hksizes;
middle[d] = 1 + hksizes;
}
final RandomAccessibleInterval<T> output = createOp.calculate(new FinalInterval(dims));
final Cursor<T> c = Views.iterable(output).cursor();
final long[] coords = new long[sigmas.length];
/*
* The gaussian normalization factor, divided by a constant value. This
* is a fudge factor, that more or less put the quality values close to
* the maximal value of a blob of optimal radius.
*/
final double C = 1d / 20d * Math.pow(1d / sigmas[0] / Math.sqrt(2 * Math.PI), sigmas.length);
// Work in image coordinates
while (c.hasNext()) {
c.fwd();
c.localize(coords);
double mantissa = 0;
double exponent = 0;
for (int d = 0; d < coords.length; d++) {
final double x = (coords[d] - middle[d]);
mantissa += -C * (x * x / sigmas[0] / sigmas[0] - 1d);
exponent += -x * x / 2d / sigmas[0] / sigmas[0];
}
c.get().setReal(mantissa * Math.exp(exponent));
}
return output;
}
Aggregations