Java Code Examples for net.imglib2.Cursor#localize()
The following examples show how to use
net.imglib2.Cursor#localize() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java From sciview with BSD 2-Clause "Simplified" License | 6 votes |
static public Img<ARGBType> convertToARGB(Img<UnsignedByteType> screenshot) { Img<ARGBType> out = ArrayImgs.argbs(screenshot.dimension(0), screenshot.dimension(1)); long[] pos = new long[3]; Cursor<ARGBType> outCur = Views.iterable(out).cursor(); RandomAccess<UnsignedByteType> sRA = screenshot.randomAccess(); while( outCur.hasNext() ) { outCur.fwd(); outCur.localize(pos); pos[2] = 0; sRA.setPosition(pos); int r = sRA.get().get(); pos[2] = 1; sRA.setPosition(pos); int g = sRA.get().get(); pos[2] = 2; sRA.setPosition(pos); int b = sRA.get().get(); int a = 255;// FIXME outCur.get().set(ARGBType.rgba(r, g, b, a)); } return out; }
Example 2
Source File: Outline.java From imagej-ops with BSD 2-Clause "Simplified" License | 6 votes |
/** * Creates a task that finds and copies outline pixels to the output. * * @param inputCursor Cursor to the input image * @param inputAccess Random access to the extended input image * @param output Access to the output image * @param start Which pixel thread should start from * @param elements Number of pixels thread processes * @param <B> Type of elements in the input image * @return a task for parallel processing. */ // region -- Helper methods -- private static <B extends BooleanType<B>> Runnable createTask( final Cursor<B> inputCursor, final OutOfBounds<B> inputAccess, final RandomAccess<BitType> output, final long start, final long elements) { return () -> { final long[] coordinates = new long[inputAccess.numDimensions()]; inputCursor.jumpFwd(start); for (long j = 0; j < elements; j++) { inputCursor.localize(coordinates); inputAccess.setPosition(coordinates); if (isOutline(inputAccess, coordinates)) { output.setPosition(coordinates); output.get().set(inputCursor.get().get()); } inputCursor.fwd(); } }; }
Example 3
Source File: DefaultCoordinatesEquation.java From imagej-ops with BSD 2-Clause "Simplified" License | 6 votes |
@Override public void compute(final UnaryFunctionOp<long[], N> op, final IterableInterval<T> output) { final Cursor<T> c = output.localizingCursor(); final long[] pos = new long[output.numDimensions()]; // loop through all pixels, set the current position and call the op while (c.hasNext()) { c.fwd(); c.localize(pos); for (int i = 0; i < output.numDimensions(); i++) { op.calculate(pos); c.get().setReal(op.calculate(pos).floatValue()); } } }
Example 4
Source File: CentroidII.java From imagej-ops with BSD 2-Clause "Simplified" License | 6 votes |
@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); }
Example 5
Source File: TransformInput.java From SPIM_Registration with GNU General Public License v2.0 | 5 votes |
private static final void loop( final Cursor< FloatType > cursor, final RealRandomAccess< FloatType > ir, final AffineTransform3D transform, final float[] s, final float[] t, final int offsetX, final int offsetY, final int offsetZ, final int imgSizeX, final int imgSizeY, final int imgSizeZ ) { // move img cursor forward any get the value (saves one access) final FloatType v = cursor.next(); cursor.localize( s ); s[ 0 ] += offsetX; s[ 1 ] += offsetY; s[ 2 ] += offsetZ; transform.applyInverse( t, s ); if ( FusionHelper.intersects( t[ 0 ], t[ 1 ], t[ 2 ], imgSizeX, imgSizeY, imgSizeZ ) ) { ir.setPosition( t ); // do not accept 0 values in the data where image data is present, 0 means no image data is available // (used in MVDeconvolution.computeQuotient) v.set( Math.max( MVDeconvolution.minValue, ir.get().get() ) ); } }
Example 6
Source File: ShowSegmentationDemo.java From sciview with BSD 2-Clause "Simplified" License | 5 votes |
private RandomAccessibleInterval<UnsignedByteType> generateDemo(int w, int h, int d, int numSegments) { double radiusSq = 36; List<RealPoint> points = new ArrayList<>(); for( int k = 0; k < numSegments; k++ ) { points.add( new RealPoint(rng.nextFloat() * w, rng.nextFloat() * h, rng.nextFloat() * d) ); } long[] pos = new long[3]; RandomAccessibleInterval<UnsignedByteType> img = ArrayImgs.unsignedBytes(w, h, d); Cursor<UnsignedByteType> cur = Views.iterable(img).cursor(); while(cur.hasNext()) { cur.fwd(); cur.localize(pos); cur.get().set(0); for( int k = 0; k < points.size(); k++ ) { double dt = dist(pos, points.get(k)); //System.out.println(dt + " " + Arrays.toString(pos) + " " + points.get(k)); if( dt < radiusSq ) { cur.get().set(k+1); } } } return img; }
Example 7
Source File: Block.java From SPIM_Registration with GNU General Public License v2.0 | 5 votes |
private static final void copy( final long start, final long loopSize, final RandomAccessible< FloatType > source, final RandomAccessibleInterval< FloatType > block, final long[] offset ) { final int numDimensions = source.numDimensions(); final Cursor< FloatType > cursor = Views.iterable( block ).localizingCursor(); // define where we will query the RandomAccess on the source // (we say it is the entire block, although it is just a part of it, // but which part depends on the underlying container) final long[] min = new long[ numDimensions ]; final long[] max = new long[ numDimensions ]; for ( int d = 0; d < numDimensions; ++d ) { min[ d ] = offset[ d ]; max[ d ] = offset[ d ] + block.dimension( d ) - 1; } final RandomAccess< FloatType > randomAccess = source.randomAccess( new FinalInterval( min, max ) ); cursor.jumpFwd( start ); final long[] tmp = new long[ numDimensions ]; for ( long l = 0; l < loopSize; ++l ) { cursor.fwd(); cursor.localize( tmp ); for ( int d = 0; d < numDimensions; ++d ) tmp[ d ] += offset[ d ]; randomAccess.setPosition( tmp ); cursor.get().set( randomAccess.get() ); } }
Example 8
Source File: ExtractPSF.java From SPIM_Registration with GNU General Public License v2.0 | 5 votes |
/** * Make image the same size as defined, center it * * @param img * @return */ public static < T extends RealType< T > > Img< T > makeSameSize( final Img< T > img, final long[] sizeIn ) { final long[] size = sizeIn.clone(); double min = Double.MAX_VALUE; for ( final T f : img ) min = Math.min( min, f.getRealDouble() ); final Img< T > square = img.factory().create( size, img.firstElement() ); final Cursor< T > squareCursor = square.localizingCursor(); final T minT = img.firstElement().createVariable(); minT.setReal( min ); final RandomAccess< T > inputCursor = Views.extendValue( img, minT ).randomAccess(); while ( squareCursor.hasNext() ) { squareCursor.fwd(); squareCursor.localize( size ); for ( int d = 0; d < img.numDimensions(); ++d ) size[ d ] = size[ d ] - square.dimension( d )/2 + img.dimension( d )/2; inputCursor.setPosition( size ); squareCursor.get().set( inputCursor.get() ); } return square; }
Example 9
Source File: ContentBased.java From SPIM_Registration with GNU General Public License v2.0 | 5 votes |
final private static Img< FloatType > createGaussianKernel( final double[] sigmas ) { final int numDimensions = sigmas.length; final long[] imageSize = new long[ numDimensions ]; final double[][] kernel = new double[ numDimensions ][]; for ( int d = 0; d < numDimensions; ++d ) { kernel[ d ] = Util.createGaussianKernel1DDouble( sigmas[ d ], true ); imageSize[ d ] = kernel[ d ].length; } final Img< FloatType > kernelImg = ArrayImgs.floats( imageSize ); final Cursor< FloatType > cursor = kernelImg.localizingCursor(); final int[] position = new int[ numDimensions ]; while ( cursor.hasNext() ) { cursor.fwd(); cursor.localize( position ); double value = 1; for ( int d = 0; d < numDimensions; ++d ) value *= kernel[ d ][ position[ d ] ]; cursor.get().set( ( float ) value ); } return kernelImg; }
Example 10
Source File: DefaultCreateKernelBiGauss.java From imagej-ops with BSD 2-Clause "Simplified" License | 4 votes |
@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) dims[0]=Math.max(3, (2 * (int)(sigmas[0] + 2*sigmas[1] + 0.5) + 1)); 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 final double k = (sigmas[1]/sigmas[0]) * (sigmas[1]/sigmas[0]); //eq. (6) final double c0 = 0.24197 * ((sigmas[1]/sigmas[0]) - 1.0) / sigmas[0]; //eq. (9) //0.24197 = 1/sqrt(2*PI*e) = 1/sqrt(2*PI) * exp(-0.5) final double[] C = { 1.0/(2.50663*sigmas[0]), 1.0/(2.50663*sigmas[1]) }; //2.50663 = sqrt(2*PI) //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 double dist = 0.; //TODO: can JVM reuse this var or is it allocated again and again (and multipling in the memory)? 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 = C[0] * Math.exp(-0.5 * dist / sigmasSq[0]) + c0; } else { //the outer one, get new distance first: dist = Math.sqrt(dist) - (sigmas[0]-sigmas[1]); dist *= dist; val = k * C[1] * Math.exp(-0.5 * dist / sigmasSq[1]); } //compose the real value finally cursor.get().setReal(val); } return out; }
Example 11
Source File: ProcessSequentialPortionWeights.java From SPIM_Registration with GNU General Public License v2.0 | 4 votes |
@Override public String call() throws Exception { final int numViews = imgs.size(); // make the interpolators, weights and get the transformations final ArrayList< RealRandomAccess< T > > interpolators = new ArrayList< RealRandomAccess< T > >( numViews ); final ArrayList< ArrayList< RealRandomAccess< FloatType > > > weightAccess = new ArrayList< ArrayList< RealRandomAccess< FloatType > > >(); final int[][] imgSizes = new int[ numViews ][ 3 ]; for ( int i = 0; i < numViews; ++i ) { final RandomAccessibleInterval< T > img = imgs.get( i ); imgSizes[ i ] = new int[]{ (int)img.dimension( 0 ), (int)img.dimension( 1 ), (int)img.dimension( 2 ) }; interpolators.add( Views.interpolate( Views.extendMirrorSingle( img ), interpolatorFactory ).realRandomAccess() ); final ArrayList< RealRandomAccess< FloatType > > list = new ArrayList< RealRandomAccess< FloatType > >(); for ( final RealRandomAccessible< FloatType > rra : weights.get( i ) ) list.add( rra.realRandomAccess() ); weightAccess.add( list ); } final Cursor< T > cursor = fusedImg.localizingCursor(); final Cursor< FloatType > cursorW = weightImg.cursor(); final float[] s = new float[ 3 ]; final float[] t = new float[ 3 ]; cursor.jumpFwd( portion.getStartPosition() ); cursorW.jumpFwd( portion.getStartPosition() ); for ( int j = 0; j < portion.getLoopSize(); ++j ) { // move img cursor forward any get the value (saves one access) final T v = cursor.next(); cursor.localize( s ); // move weight cursor forward and get the value final FloatType w = cursorW.next(); if ( doDownSampling ) { s[ 0 ] *= downSampling; s[ 1 ] *= downSampling; s[ 2 ] *= downSampling; } s[ 0 ] += bb.min( 0 ); s[ 1 ] += bb.min( 1 ); s[ 2 ] += bb.min( 2 ); double sum = 0; double sumW = 0; for ( int i = 0; i < numViews; ++i ) { transforms[ i ].applyInverse( t, s ); if ( FusionHelper.intersects( t[ 0 ], t[ 1 ], t[ 2 ], imgSizes[ i ][ 0 ], imgSizes[ i ][ 1 ], imgSizes[ i ][ 2 ] ) ) { final RealRandomAccess< T > r = interpolators.get( i ); r.setPosition( t ); double w1 = 1; for ( final RealRandomAccess< FloatType > weight : weightAccess.get( i ) ) { weight.setPosition( t ); w1 *= weight.get().get(); } sum += r.get().getRealDouble() * w1; sumW += w1; } } if ( sumW > 0 ) { v.setReal( v.getRealFloat() + sum ); w.set( w.get() + (float)sumW ); } } return portion + " finished successfully (many weights)."; }
Example 12
Source File: Coloc_2.java From Colocalisation_Analysis with GNU General Public License v3.0 | 4 votes |
/** * A method to get the bounding box from the data in the given image that is * above zero. Those values are interpreted as a mask. It will return null if * no mask information was found. * * @param mask The image to look for "on" values in * @return a new MaskInfo object or null */ protected MaskInfo getBoundingBoxOfMask(final Img<T> mask) { final Cursor<T> cursor = mask.localizingCursor(); final int numMaskDims = mask.numDimensions(); // the "off type" of the mask final T offType = mask.firstElement().createVariable(); offType.setZero(); // the corners of the bounding box final long[][] minMax = new long[2][]; // indicates if mask data has been found boolean maskFound = false; // a container for temporary position information final long[] pos = new long[numMaskDims]; // walk over the mask while (cursor.hasNext()) { cursor.fwd(); final T data = cursor.get(); // test if the current mask data represents on or off if (data.compareTo(offType) > 0) { // get current position cursor.localize(pos); if (!maskFound) { // we found mask data, first time maskFound = true; // init min and max with the current position minMax[0] = Arrays.copyOf(pos, numMaskDims); minMax[1] = Arrays.copyOf(pos, numMaskDims); } else { /* Is is at least second hit, compare if it * has new "extreme" positions, i.e. does * is make the BB bigger? */ for (int d = 0; d < numMaskDims; d++) { if (pos[d] < minMax[0][d]) { // is it smaller than min minMax[0][d] = pos[d]; } else if (pos[d] > minMax[1][d]) { // is it larger than max minMax[1][d] = pos[d]; } } } } } if (!maskFound) return null; // calculate size final long[] size = new long[numMaskDims]; for (int d = 0; d < numMaskDims; d++) size[d] = minMax[1][d] - minMax[0][d] + 1; // create and add bounding box final BoundingBox bb = new BoundingBox(minMax[0], size); return new MaskInfo(bb, mask); }
Example 13
Source File: DefaultCreateKernelLog.java From imagej-ops with BSD 2-Clause "Simplified" License | 4 votes |
@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; }
Example 14
Source File: ProcessParalellPortionWeight.java From SPIM_Registration with GNU General Public License v2.0 | 4 votes |
@Override public String call() throws Exception { final int numViews = imgs.size(); // make the interpolators, weights and get the transformations final ArrayList< RealRandomAccess< T > > interpolators = new ArrayList< RealRandomAccess< T > >( numViews ); final ArrayList< RealRandomAccess< FloatType > > weightAccess = new ArrayList< RealRandomAccess< FloatType > >(); final int[][] imgSizes = new int[ numViews ][ 3 ]; for ( int i = 0; i < numViews; ++i ) { final RandomAccessibleInterval< T > img = imgs.get( i ); imgSizes[ i ] = new int[]{ (int)img.dimension( 0 ), (int)img.dimension( 1 ), (int)img.dimension( 2 ) }; interpolators.add( Views.interpolate( Views.extendMirrorSingle( img ), interpolatorFactory ).realRandomAccess() ); weightAccess.add( weights.get( i ).realRandomAccess() ); } final Cursor< T > cursor = fusedImg.localizingCursor(); final float[] s = new float[ 3 ]; final float[] t = new float[ 3 ]; cursor.jumpFwd( portion.getStartPosition() ); for ( int j = 0; j < portion.getLoopSize(); ++j ) { // move img cursor forward any get the value (saves one access) final T v = cursor.next(); cursor.localize( s ); if ( doDownSampling ) { s[ 0 ] *= downSampling; s[ 1 ] *= downSampling; s[ 2 ] *= downSampling; } s[ 0 ] += bb.min( 0 ); s[ 1 ] += bb.min( 1 ); s[ 2 ] += bb.min( 2 ); double sum = 0; double sumW = 0; for ( int i = 0; i < numViews; ++i ) { transforms[ i ].applyInverse( t, s ); if ( FusionHelper.intersects( t[ 0 ], t[ 1 ], t[ 2 ], imgSizes[ i ][ 0 ], imgSizes[ i ][ 1 ], imgSizes[ i ][ 2 ] ) ) { final RealRandomAccess< T > r = interpolators.get( i ); r.setPosition( t ); final RealRandomAccess< FloatType > weight = weightAccess.get( i ); weight.setPosition( t ); final double w = weight.get().get(); sum += r.get().getRealDouble() * w; sumW += w; } } if ( sumW > 0 ) v.setReal( sum / sumW ); } return portion + " finished successfully (one weight)."; }
Example 15
Source File: MaskAndRoiTest.java From Colocalisation_Analysis with GNU General Public License v3.0 | 4 votes |
/** * This test makes sure that a mask that is based on a lower dimension * image has the correct dimensionality. */ @Test public void irregularRoiDimensionTest() { // load a 3D test image RandomAccessibleInterval<UnsignedByteType> img = positiveCorrelationImageCh1; final long width = img.dimension(0); final long height = img.dimension(1); final long slices = img.dimension(2); final long[] dimImg = new long[ img.numDimensions() ]; img.dimensions(dimImg); // create a random noise 2D image -- set roiWidh/roiSize accordingly RandomAccessibleInterval<UnsignedByteType> maskSlice = TestImageAccessor.produceSticksNoiseImage( (int) width, (int) height, 50, 2, 10); RandomAccessibleInterval<BitType> mask = MaskFactory.createMask(dimImg, maskSlice); final long[] dimMask = new long[ mask.numDimensions() ]; mask.dimensions(dimMask); // check the dimensions of the mask org.junit.Assert.assertArrayEquals(dimImg, dimMask); // make sure the mask actually got the same content on every slice final double[] offset = new double[ mask.numDimensions() ]; Arrays.fill(offset, 0); double[] size = new double[ mask.numDimensions() ]; size[0] = width; size[1] = height; size[2] = 1; RandomAccess<BitType> maskCursor = mask.randomAccess(); RectangleRegionOfInterest roi = new RectangleRegionOfInterest( offset, size); Cursor<BitType> firstSliceCursor = roi.getIterableIntervalOverROI(mask).cursor(); final long[] pos = new long[ mask.numDimensions() ]; while (firstSliceCursor.hasNext()) { firstSliceCursor.fwd(); firstSliceCursor.localize(pos); BitType maskValue = firstSliceCursor.get(); // go through all slices for (int i=1; i<slices; ++i) { pos[2] = i; maskCursor.setPosition(pos); // compare the values and assume they are the same int cmp = maskCursor.get().compareTo(maskValue); assertEquals(0, cmp); } } }
Example 16
Source File: MaskAndRoiTest.java From Colocalisation_Analysis with GNU General Public License v3.0 | 4 votes |
/** * Tests if a masked walk over an image refers to the correct data * by copying the data to a separate image and then compare it with * the original image data. The position data in the original image * is calculated based on the ROI offset and the relative position * in the copied ROI image. * @throws MissingPreconditionException */ @Test public void maskContentTest() throws MissingPreconditionException { // load a 3D test image final RandomAccessibleInterval<UnsignedByteType> img = positiveCorrelationImageCh1; final long[] roiOffset = createRoiOffset(img); final long[] roiSize = createRoiSize(img); final long[] dim = new long[ img.numDimensions() ]; img.dimensions(dim); final RandomAccessibleInterval<BitType> mask = MaskFactory.createMask(dim, roiOffset, roiSize); // create cursor to walk an image with respect to a mask TwinCursor<UnsignedByteType> cursor = new TwinCursor<UnsignedByteType>( img.randomAccess(), img.randomAccess(), Views.iterable(mask).localizingCursor()); // create an image for the "clipped ROI" ImgFactory<UnsignedByteType> maskFactory = new ArrayImgFactory<UnsignedByteType>(); RandomAccessibleInterval<UnsignedByteType> clippedRoiImage = maskFactory.create( roiSize, new UnsignedByteType() ); // "Clipped ROI" ); RandomAccess<UnsignedByteType> outputCursor = clippedRoiImage.randomAccess(); // copy ROI data to new image long[] pos = new long[ clippedRoiImage.numDimensions() ]; while (cursor.hasNext()) { cursor.fwd(); cursor.localize(pos); // shift position by offset for (int i=0; i<pos.length; i++) { pos[i] = pos[i] - roiOffset[i]; } outputCursor.setPosition(pos); outputCursor.get().set( cursor.getFirst() ); } /* go through the clipped ROI and compare the date to offset values * of the original data. */ Cursor<UnsignedByteType> roiCopyCursor = Views.iterable(clippedRoiImage).localizingCursor(); RandomAccess<UnsignedByteType> imgCursor = img.randomAccess(); // create variable for summing up and set it to zero double sum = 0; pos = new long [ clippedRoiImage.numDimensions() ]; while (roiCopyCursor.hasNext()) { roiCopyCursor.fwd(); roiCopyCursor.localize(pos); // shift position by offset for (int i=0; i<pos.length; i++) { pos[i] = pos[i] + roiOffset[i]; } // set position in original image imgCursor.setPosition(pos); // get ROI and original image data double roiData = roiCopyCursor.get().getRealDouble(); double imgData = imgCursor.get().getRealDouble(); // sum up the difference double diff = roiData - imgData; sum += diff * diff; } // check if sum is zero assertTrue("The sum of squared differences was " + sum + ".", Math.abs(sum) < 0.00001); }
Example 17
Source File: AbstractThin.java From imagej-ops with BSD 2-Clause "Simplified" License | 4 votes |
@Override public void compute(final RandomAccessibleInterval<BitType> input, final RandomAccessibleInterval<BitType> output) { // Create a new image as a buffer to store the thinning image in each // iteration. // This image and output are swapped each iteration since we need to work on // the image // without changing it. final Img<BitType> buffer = ops().create().img(input, new BitType()); final IterableInterval<BitType> it1 = Views.iterable(buffer); final IterableInterval<BitType> it2 = Views.iterable(output); // Extend the buffer in order to be able to iterate care-free later. final RandomAccessible<BitType> ra1 = Views.extendBorder(buffer); final RandomAccessible<BitType> ra2 = Views.extendBorder(output); // Used only in first iteration. RandomAccessible<BitType> currRa = Views.extendBorder(input); // Create cursors. final Cursor<BitType> firstCursor = it1.localizingCursor(); Cursor<BitType> currentCursor = Views.iterable(input).localizingCursor(); final Cursor<BitType> secondCursor = it2.localizingCursor(); // Create pointers to the current and next cursor and set them to Buffer and // output respectively. Cursor<BitType> nextCursor; nextCursor = secondCursor; // The main loop. boolean changes = true; int i = 0; // Until no more changes, do: final long[] coordinates = new long[currentCursor.numDimensions()]; while (changes) { changes = false; // This For-Loop makes sure, that iterations only end on full cycles (as // defined by the strategies). for (int j = 0; j < m_strategy.getIterationsPerCycle(); ++j) { // For each pixel in the image. while (currentCursor.hasNext()) { // Move both cursors currentCursor.fwd(); nextCursor.fwd(); // Get the position of the current cursor. currentCursor.localize(coordinates); // Copy the value of the image currently operated upon. final boolean curr = currentCursor.get().get(); nextCursor.get().set(curr); // Only foreground pixels may be thinned if (curr) { // Ask the strategy whether to flip the foreground pixel or not. final boolean flip = m_strategy.removePixel(coordinates, currRa, j); // If yes - change and keep track of the change. if (flip) { nextCursor.get().set(false); changes = true; } } } // One step of the cycle is finished, notify the strategy. m_strategy.afterCycle(); // Reset the cursors to the beginning and assign pointers for the next // iteration. currentCursor.reset(); nextCursor.reset(); // Keep track of the most recent image. Needed for output. if (currRa == ra2) { currRa = ra1; currentCursor = firstCursor; nextCursor = secondCursor; } else { currRa = ra2; currentCursor = secondCursor; nextCursor = firstCursor; } // Keep track of iterations. ++i; } } // Depending on the iteration count, the final image is either in ra1 or // ra2. Copy it to output. if (i % 2 == 0) { // Ra1 points to img1, ra2 points to output. copy(buffer, output); } }
Example 18
Source File: ProcessSequentialPortionWeight.java From SPIM_Registration with GNU General Public License v2.0 | 4 votes |
@Override public String call() throws Exception { final int numViews = imgs.size(); // make the interpolators, weights and get the transformations final ArrayList< RealRandomAccess< T > > interpolators = new ArrayList< RealRandomAccess< T > >( numViews ); final ArrayList< RealRandomAccess< FloatType > > weightAccess = new ArrayList< RealRandomAccess< FloatType > >(); final int[][] imgSizes = new int[ numViews ][ 3 ]; for ( int i = 0; i < numViews; ++i ) { final RandomAccessibleInterval< T > img = imgs.get( i ); imgSizes[ i ] = new int[]{ (int)img.dimension( 0 ), (int)img.dimension( 1 ), (int)img.dimension( 2 ) }; interpolators.add( Views.interpolate( Views.extendMirrorSingle( img ), interpolatorFactory ).realRandomAccess() ); weightAccess.add( weights.get( i ).realRandomAccess() ); } final Cursor< T > cursor = fusedImg.localizingCursor(); final Cursor< FloatType > cursorW = weightImg.cursor(); final float[] s = new float[ 3 ]; final float[] t = new float[ 3 ]; cursor.jumpFwd( portion.getStartPosition() ); cursorW.jumpFwd( portion.getStartPosition() ); for ( int j = 0; j < portion.getLoopSize(); ++j ) { // move img cursor forward any get the value (saves one access) final T v = cursor.next(); cursor.localize( s ); // move weight cursor forward and get the value final FloatType w = cursorW.next(); if ( doDownSampling ) { s[ 0 ] *= downSampling; s[ 1 ] *= downSampling; s[ 2 ] *= downSampling; } s[ 0 ] += bb.min( 0 ); s[ 1 ] += bb.min( 1 ); s[ 2 ] += bb.min( 2 ); double sum = 0; double sumW = 0; for ( int i = 0; i < numViews; ++i ) { transforms[ i ].applyInverse( t, s ); if ( FusionHelper.intersects( t[ 0 ], t[ 1 ], t[ 2 ], imgSizes[ i ][ 0 ], imgSizes[ i ][ 1 ], imgSizes[ i ][ 2 ] ) ) { final RealRandomAccess< T > r = interpolators.get( i ); r.setPosition( t ); final RealRandomAccess< FloatType > weight = weightAccess.get( i ); weight.setPosition( t ); final double w1 = weight.get().get(); sum += r.get().getRealDouble() * w1; sumW += w1; } } if ( sumW > 0 ) { v.setReal( v.getRealFloat() + sum ); w.set( w.get() + (float)sumW ); } } return portion + " finished successfully (one weight)."; }
Example 19
Source File: ProcessIndependentPortion.java From SPIM_Registration with GNU General Public License v2.0 | 4 votes |
@Override public String call() throws Exception { // make the interpolators and get the transformations final RealRandomAccess< T > r = Views.interpolate( Views.extendMirrorSingle( img ), interpolatorFactory ).realRandomAccess(); final int[] imgSize = new int[]{ (int)img.dimension( 0 ), (int)img.dimension( 1 ), (int)img.dimension( 2 ) }; final Cursor< T > cursor = fusedImg.localizingCursor(); final float[] s = new float[ 3 ]; final float[] t = new float[ 3 ]; cursor.jumpFwd( portion.getStartPosition() ); for ( int j = 0; j < portion.getLoopSize(); ++j ) { // move img cursor forward any get the value (saves one access) final T v = cursor.next(); cursor.localize( s ); if ( doDownSampling ) { s[ 0 ] *= downSampling; s[ 1 ] *= downSampling; s[ 2 ] *= downSampling; } s[ 0 ] += bb.min( 0 ); s[ 1 ] += bb.min( 1 ); s[ 2 ] += bb.min( 2 ); transform.applyInverse( t, s ); if ( FusionHelper.intersects( t[ 0 ], t[ 1 ], t[ 2 ], imgSize[ 0 ], imgSize[ 1 ], imgSize[ 2 ] ) ) { r.setPosition( t ); v.setReal( r.get().getRealFloat() ); } } return portion + " finished successfully (individual fusion, no weights)."; }
Example 20
Source File: DefaultCoarsenessFeature.java From imagej-ops with BSD 2-Clause "Simplified" License | 4 votes |
/** * * For every point calculate differences between the not overlapping * neighborhoods on opposite sides of the point in horizontal and vertical * direction. At each point take the highest difference value when * considering all directions together. * * @param input * Input image * @param meanImages * Mean images * @return Array containing all leadding difference values */ private ArrayList<Double> sizedLeadDiffValues(final RandomAccessibleInterval<I> input, final HashMap<Integer, Img<I>> meanImages) { long[] pos = new long[input.numDimensions()]; long[] dim = new long[input.numDimensions()]; input.dimensions(dim); ArrayList<Double> maxDifferences = new ArrayList<>(); Cursor<I> cursor = meanImages.get(1).cursor(); while (cursor.hasNext()) { cursor.next(); // NB: the smallest possible value for maxDiff is 0 double maxDiff = 0; for (int i = 1; i <= 5; i++) { RandomAccess<I> ra1 = meanImages.get(i).randomAccess(); RandomAccess<I> ra2 = meanImages.get(i).randomAccess(); for (int d = 0; d < input.numDimensions(); d++) { cursor.localize(pos); if ((pos[d] + 2 * i + 1) < dim[d]) { ra1.setPosition(pos); double val1 = ra1.get().getRealDouble(); pos[d] += 2 * i + 1; ra2.setPosition(pos); double val2 = ra2.get().getRealDouble(); double diff = Math.abs(val2 - val1); maxDiff = diff >= maxDiff ? diff : maxDiff; } } } maxDifferences.add(maxDiff); } return maxDifferences; }