Java Code Examples for net.imglib2.view.Views#flatIterable()

The following examples show how to use net.imglib2.view.Views#flatIterable() . 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: Align.java    From BigStitcher with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute the partial derivative of source in a particular dimension.
 *
 * @param source
 *            source image, has to provide valid data in the interval of the
 *            gradient image plus a one pixel border in dimension.
 * @param target
 *            output image, the partial derivative of source in the
 *            specified dimension.
 * @param dimension
 *            along which dimension the partial derivatives are computed
 * @param <T> pixel type source
 * @param <S> pixel type target
 */
public static < T extends RealType< T >, S extends RealType< S > > void gradient(
		final RandomAccessible< T > source,
		final RandomAccessibleInterval< S > target,
		final int dimension )
{
	final Cursor< T > front = Views.flatIterable(
			Views.interval( source,
					Intervals.translate( target, 1, dimension ) ) ).cursor();
	final Cursor< T > back = Views.flatIterable(
			Views.interval( source,
					Intervals.translate( target, -1, dimension ) ) ).cursor();
	for( final S t : Views.flatIterable( target ) )
	{
		t.setReal( front.next().getRealDouble() - back.next().getRealDouble());
		t.mul( 0.5 );
	}
}
 
Example 2
Source File: AddLabelImage.java    From sciview with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void run() {

    // interpret the current image as a label image and convert it to ImgLabeling

    @SuppressWarnings("unchecked")
    Img<T> labelMap = ( Img<T> ) currentImage.getImgPlus();

    final Dimensions dims = labelMap;
    final IntType t = new IntType();
    final RandomAccessibleInterval<IntType> img = Util.getArrayOrCellImgFactory( dims, t ).create( dims, t );
    ImgLabeling<Integer, IntType> labeling = new ImgLabeling<>( img );

    final Cursor<LabelingType<Integer>> labelCursor = Views.flatIterable( labeling ).cursor();

    for( final T input : Views.flatIterable( labelMap ) ) {
        final LabelingType<Integer> element = labelCursor.next();
        if( input.getRealFloat() != 0 ) {
            element.add( ( int ) input.getRealFloat() );
        }
    }

    // take the regions, process them to meshes and put it in the viewer
    LabelRegions<Integer> labelRegions = new LabelRegions<>( labeling );

    Object[] regionsArr = labelRegions.getExistingLabels().toArray();
    for( int i = 0; i < labelRegions.getExistingLabels().size(); i++ ) {
        LabelRegion<Integer> lr = labelRegions.getLabelRegion( ( Integer ) regionsArr[i] );

        Mesh mesh = ops.geom().marchingCubes( lr );
        sciView.addMesh( mesh );
    }
}
 
Example 3
Source File: DefaultFillHoles.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void compute(final RandomAccessibleInterval<T> op,
	final RandomAccessibleInterval<T> r)
{
	final IterableInterval<T> iterOp = Views.flatIterable(op);
	final IterableInterval<T> iterR = Views.flatIterable(r);

	long[] dim = new long[r.numDimensions()];
	r.dimensions(dim);
	Cursor<T> rc = iterR.cursor();
	Cursor<T> opc = iterOp.localizingCursor();
	// Fill with non background marker
	while (rc.hasNext()) {
		rc.next().setOne();
	}

	rc.reset();
	boolean border;
	// Flood fill from every background border voxel
	while (rc.hasNext()) {
		rc.next();
		opc.next();
		if (rc.get().get() && !opc.get().get()) {
			border = false;
			for (int i = 0; i < r.numDimensions(); i++) {
				if (rc.getLongPosition(i) == 0 || rc.getLongPosition(i) == dim[i] -
					1)
				{
					border = true;
					break;
				}
			}
			if (border) {
				floodFillComp.compute(op, rc, r);
			}
		}
	}
}
 
Example 4
Source File: Align.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compute the steepest descent images of the template at the identity warp.
 * Each steepest descent image comprises the partial derivatives of template
 * intensities with respect to one parameter of the warp function.
 *
 * The result is stored in the <em>n+1</em> dimensional target
 * image. Dimension <em>n</em> is used to index the partial derivative. For
 * example, the partial derivative by the second parameter of the warp
 * function is stored in slice <em>n=1</em>.
 *
 * @param gradients
 *            n+1 dimensional image of partial derivatives of the template.
 *            Dimension n is used to index the partial derivative. For
 *            example, the partial derivative by Y is stored in slice n=1.
 * @param warpFunction
 *            The warp function to be applied to the template. The partial
 *            derivatives of template intensities with respect to the
 *            parameters of this warp function are computed.
 * @param target
 *            Image of <em>n+1</em> dimensions to store the steepest descent
 *            Dimension <em>n</em> is used to index the parameters of the
 *            warp function. For example, the partial derivative of the
 *            template image intensity by parameter 2 of the warp function
 *            at pixel <em>(x,y)</em> is stored at position <em>(x,y,1)</em>
 * @param <T>
 *            pixel type
 *            .
 */
public static <T extends NumericType< T >> void computeSteepestDescents(
		final RandomAccessibleInterval< T > gradients, final WarpFunction warpFunction,
		final RandomAccessibleInterval< T > target)
{
	final int n = gradients.numDimensions() - 1;
	final int numParameters = warpFunction.numParameters();
	final T tmp = Util.getTypeFromInterval( gradients ).createVariable();
	for ( int p = 0; p < numParameters; ++p )
	{
		for ( int d = 0; d < n; ++d )
		{
			final Cursor< T > gd = Views.flatIterable( Views.hyperSlice( gradients, n, d ) ).localizingCursor();
			for ( final T t : Views.flatIterable( Views.hyperSlice( target, n, p ) ) )
			{
				tmp.set( gd.next() );
				tmp.mul( warpFunction.partial( gd, d, p ) );
				t.add( tmp );
			}
		}
	}
}
 
Example 5
Source File: Align.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
double alignStep(final RandomAccessibleInterval< T > image, ExecutorService service)
{
	// compute error image = warped image - template
	computeDifference( Views.extendBorder( image ), currentTransform, template, error, service, Runtime.getRuntime().availableProcessors() * 2 );

	// compute transform parameter update
	final double[] gradient = new double[numParameters];

	ArrayList< Callable< Void > > calls =  new ArrayList< Callable<Void> >();
	for ( int p = 0; p < numParameters; ++p )
	{
		final int pInner = p;
		Callable< Void > callable = new Callable< Void >()
		{
			@Override
			public Void call() throws Exception
			{
				double gradT = 0;
				final Cursor< FloatType > err = Views.flatIterable( error ).cursor();
				for ( final FloatType t : Views.flatIterable( Views.hyperSlice( descent, n, pInner ) ) )
					gradT += t.getRealDouble() * err.next().getRealDouble();
				gradient[pInner] = gradT;
				return null;

			}
		};

		calls.add( callable );

	}

	List<Future<Void>> futures = null;

	try
	{
		futures = service.invokeAll( calls );
		for (Future<Void> f : futures)
			f.get();
	}
	catch ( InterruptedException | ExecutionException e)
	{
		e.printStackTrace();
	}

	final double[] dp = new double[numParameters];
	LinAlgHelpers.mult( Hinv, gradient, dp );

	// udpate transform
	currentTransform.preConcatenate( warpFunction.getAffine( dp ) );

	// return norm of parameter update vector
	return LinAlgHelpers.length( dp );
}
 
Example 6
Source File: DefaultFlatIterableView.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public IterableInterval<T> calculate(RandomAccessibleInterval<T> input) {
	return Views.flatIterable(input);
}