Java Code Examples for net.imglib2.Dimensions#numDimensions()

The following examples show how to use net.imglib2.Dimensions#numDimensions() . 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: PhaseCorrelation2.java    From BigStitcher with GNU General Public License v2.0 6 votes vote down vote up
public static <T extends RealType<T>, S extends RealType<S>, R extends RealType<R>, C extends ComplexType<C>> RandomAccessibleInterval<R> calculatePCM(
		RandomAccessibleInterval<T> img1, RandomAccessibleInterval<S> img2, int[] extension,
		ImgFactory<R> factory, R type, ImgFactory<C> fftFactory, C fftType, ExecutorService service){

	
	// TODO: Extension absolute per dimension in pixels, i.e. int[] extension
	// TODO: not bigger than the image dimension because the second mirroring is identical to the image
	
	Dimensions extSize = PhaseCorrelation2Util.getExtendedSize(img1, img2, extension);
	long[] paddedDimensions = new long[extSize.numDimensions()];
	long[] fftSize = new long[extSize.numDimensions()];
	FFTMethods.dimensionsRealToComplexFast(extSize, paddedDimensions, fftSize);
	
	RandomAccessibleInterval<C> fft1 = fftFactory.create(fftSize, fftType);
	RandomAccessibleInterval<C> fft2 = fftFactory.create(fftSize, fftType);
	
	FFT.realToComplex(Views.interval(PhaseCorrelation2Util.extendImageByFactor(img1, extension), 
			FFTMethods.paddingIntervalCentered(img1, new FinalInterval(paddedDimensions))), fft1, service);
	FFT.realToComplex(Views.interval(PhaseCorrelation2Util.extendImageByFactor(img2, extension), 
			FFTMethods.paddingIntervalCentered(img2, new FinalInterval(paddedDimensions))), fft2, service);
	
	RandomAccessibleInterval<R> pcm = calculatePCMInPlace(fft1, fft2, factory, type, service);
	return pcm;
	
}
 
Example 2
Source File: DefaultComputeFFTSize.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public long[][] calculate(Dimensions inputDimensions) {

	long[][] size = new long[2][];
	size[0] = new long[inputDimensions.numDimensions()];
	size[1] = new long[inputDimensions.numDimensions()];

	for (int i = 0; i < inputDimensions.numDimensions(); i++) {
		// real size
		if (powerOfTwo) {
			size[0][i] = NextPowerOfTwo.nextPowerOfTwo(inputDimensions.dimension(i));
		} else {
			size[0][i] = (long) NextSmoothNumber.nextSmooth((int) inputDimensions.dimension(i));
		}
		// complex size
		if (i == 0) {
			size[1][i] = (size[0][i] / 2 + 1);
		} else {
			size[1][i] = size[0][i];
		}
	}

	return size;

}
 
Example 3
Source File: PainteraAlerts.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
private static int argMaxDim(final Dimensions dims) {
	long max = -1;
	int argMax = -1;
	for (int d = 0; d < dims.numDimensions(); ++d) {
		if (dims.dimension(d) > max) {
			max = dims.dimension(d);
			argMax = d;
		}
	}
	return argMax;
}
 
Example 4
Source File: PainteraCommandLineArgs.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
private static int argMaxDim(final Dimensions dims) {
	long max = -1;
	int argMax = -1;
	for (int d = 0; d < dims.numDimensions(); ++d) {
		if (dims.dimension(d) > max) {
			max = dims.dimension(d);
			argMax = d;
		}
	}
	return argMax;
}
 
Example 5
Source File: PhaseCorrelation2Util.java    From BigStitcher with GNU General Public License v2.0 5 votes vote down vote up
/**
 * calculate the size difference of two Dimensions objects (dim2-dim1)
 * @param dim1 first Dimensions
 * @param dim2 second Dimensions
 * @return int array of difference
 */	
public static int[] getSizeDifference(Dimensions dim1, Dimensions dim2) {
	int[] diff = new int[dim1.numDimensions()];
	for (int i = 0; i < dim1.numDimensions(); i++){
		diff[i] = (int) (dim2.dimension(i) - dim1.dimension(i));
	}		
	return diff;
}
 
Example 6
Source File: PhaseCorrelation2Util.java    From BigStitcher with GNU General Public License v2.0 5 votes vote down vote up
/**
 * calculate the size of an extended image big enough to hold dim1 and dim2
 * with each dimension also enlarged by extension pixels on each side (but at most by the original image size)
 * @param dim1 first Dimensions
 * @param dim2 second Dimensions
 * @param extension: number of pixels to add at each side in each dimension
 * @return extended dimensions
 */
public static FinalDimensions getExtendedSize(Dimensions dim1, Dimensions dim2, int [] extension) {
	long[] extDims = new long[dim1.numDimensions()];
	for (int i = 0; i <dim1.numDimensions(); i++){
		extDims[i] = dim1.dimension(i) > dim2.dimension(i) ? dim1.dimension(i) : dim2.dimension(i);
		long extBothSides = extDims[i] < extension[i] ? extDims[i] * 2 : extension[i] * 2;
		extDims[i] += extBothSides;
	}
	return new FinalDimensions(extDims);		
}
 
Example 7
Source File: PhaseCorrelation2Util.java    From BigStitcher with GNU General Public License v2.0 5 votes vote down vote up
public static int[] extensionByFactor(Dimensions dims, double extensionFactor){
	int[] res = new int[dims.numDimensions()];
	for (int i = 0; i< dims.numDimensions(); i++){
		res[i] = (int) (dims.dimension(i)*extensionFactor);
	}
	return res;
}
 
Example 8
Source File: FFTMethodsUtility.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Calculates padding size size for real to complex FFT
 * 
 * @param fast if true calculate size for fast FFT
 * @param inputDimensions original real dimensions
 * @return padded real dimensions
 */
public static Dimensions getPaddedInputDimensionsRealToComplex(
	final boolean fast, final Dimensions inputDimensions)
{
	final long[] paddedSize = new long[inputDimensions.numDimensions()];
	final long[] fftSize = new long[inputDimensions.numDimensions()];

	dimensionsRealToComplex(fast, inputDimensions, paddedSize, fftSize);

	return new FinalDimensions(paddedSize);

}
 
Example 9
Source File: FFTMethodsUtility.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Calculates complex FFT size for real to complex FFT
 * 
 * @param fast if true calculate size for fast FFT
 * @param inputDimensions original real dimensions
 * @return complex FFT dimensions
 */
public static Dimensions getFFTDimensionsRealToComplex(final boolean fast,
	final Dimensions inputDimensions)
{
	final long[] paddedSize = new long[inputDimensions.numDimensions()];
	final long[] fftSize = new long[inputDimensions.numDimensions()];

	dimensionsRealToComplex(fast, inputDimensions, paddedSize, fftSize);

	return new FinalDimensions(fftSize);

}
 
Example 10
Source File: PaddingIntervalCentered.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public O calculate(final I input, final Dimensions paddedDimensions) {

	final long[] paddedSize = new long[paddedDimensions.numDimensions()];
	paddedDimensions.dimensions(paddedSize);

	O inputInterval = (O) FFTMethods.paddingIntervalCentered(input,
		FinalDimensions.wrap(paddedSize));

	return inputInterval;
}
 
Example 11
Source File: LinkOverlay.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
public static void drawViewOutlines( final Graphics2D g, final Dimensions dims, final AffineTransform3D transfrom, final Color color )
{
	final int n = dims.numDimensions();

	final Queue< List< Boolean > > worklist = new LinkedList<>();
	// add 0,0,..
	final List< Boolean > origin = new ArrayList<>();
	for (int d = 0; d < n; d++)
		origin.add( false );
	worklist.add( origin );

	while ( worklist.size() > 0 )
	{
		final List< Boolean > vertex1 = worklist.poll();
		final List< List< Boolean > > neighbors = getHigherVertices( vertex1 );

		worklist.addAll( neighbors );

		for (final List<Boolean> vertex2 : neighbors)
		{
			final double[] v1Pos = new double[ n ];
			final double[] v2Pos = new double[ n ];

			for (int d = 0; d < n; d++)
			{
				// the outline goes from -0.5 to dimension(d) - 0.5 (compared to the actual range of (0, dim(d)-1))
				// this is because BDV (correctly) draws pixels with center at pixel location 
				v1Pos[d] = vertex1.get( d ) ? dims.dimension( d ) - 0.5 : -0.5;
				v2Pos[d] = vertex2.get( d ) ? dims.dimension( d ) - 0.5 : -0.5;
			}

			transfrom.apply( v1Pos, v1Pos );
			transfrom.apply( v2Pos, v2Pos );

			g.setColor( color );
			g.setStroke( new BasicStroke( 1.0f ) );
			g.drawLine((int) v1Pos[0],(int) v1Pos[1],(int) v2Pos[0],(int) v2Pos[1] );
		}
		
	}
	
}
 
Example 12
Source File: PhaseCorrelation2Util.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
public static List<PhaseCorrelationPeak2> expandPeakToPossibleShifts(
		PhaseCorrelationPeak2 peak, Dimensions pcmDims, Dimensions img1Dims, Dimensions img2Dims)
{
	int n = pcmDims.numDimensions();
	double[] subpixelDiff = new double[n];
	
	if (peak.getSubpixelPcmLocation() != null)
		for (int i = 0; i < n; i++)
			subpixelDiff[i] = peak.getSubpixelPcmLocation().getDoublePosition( i ) - peak.getPcmLocation().getIntPosition( i );

	int[] originalPCMPeakWithOffset = new int[n];
	peak.getPcmLocation().localize(originalPCMPeakWithOffset);

	int[] extensionImg1 = getSizeDifference(img1Dims, pcmDims);
	int[] extensionImg2 = getSizeDifference(img2Dims, pcmDims);
	int[] offset = new int[pcmDims.numDimensions()];
	for(int i = 0; i < offset.length; i++){
		offset[i] = (extensionImg2[i] - extensionImg1[i] ) / 2;
		originalPCMPeakWithOffset[i] += offset[i];
		originalPCMPeakWithOffset[i] %= pcmDims.dimension(i); 
	}		
	
	List<PhaseCorrelationPeak2> shiftedPeaks = new ArrayList<PhaseCorrelationPeak2>();
	for (int i = 0; i < Math.pow(2, pcmDims.numDimensions()); i++){
		int[] possibleShift = originalPCMPeakWithOffset.clone();
		PhaseCorrelationPeak2 peakWithShift = new PhaseCorrelationPeak2(peak);
		for (int d = 0; d < pcmDims.numDimensions(); d++){
			/*
			 * mirror the shift around the origin in dimension d if (i / 2^d) is even
			 * --> all possible shifts
			 */
			if ((i / (int) Math.pow(2, d) % 2) == 0){
				possibleShift[d] = possibleShift[d] < 0 ? possibleShift[d] + (int) pcmDims.dimension(d) : possibleShift[d] - (int) pcmDims.dimension(d);
			}
		}
		peakWithShift.setShift(new Point(possibleShift));

		if (peakWithShift.getSubpixelPcmLocation() != null)
		{
			double[] subpixelShift = new double[n];
			for (int j =0; j<n;j++){
				subpixelShift[j] = possibleShift[j] + subpixelDiff[j];
			}

			peakWithShift.setSubpixelShift( new RealPoint( subpixelShift ) );
		}
		shiftedPeaks.add(peakWithShift);
	}		
	return shiftedPeaks;
}
 
Example 13
Source File: PhaseCorrelation2Util.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
public static Pair<Interval, Interval> getOverlapIntervals(Dimensions img1, Dimensions img2, Localizable shift){
	
	final int numDimensions = img1.numDimensions();
	final long[] offsetImage1 = new long[ numDimensions ];
	final long[] offsetImage2 = new long[ numDimensions ];
	final long[] maxImage1 = new long[ numDimensions ];
	final long[] maxImage2 = new long[ numDimensions ];
	
	long overlapSize;
	
	for ( int d = 0; d < numDimensions; ++d )
	{
		if ( shift.getLongPosition(d) >= 0 )
		{
			// two possiblities
			//
			//               shift=start              end
			//                 |					   |
			// A: Image 1 ------------------------------
			//    Image 2      ----------------------------------
			//
			//               shift=start	    end
			//                 |			     |
			// B: Image 1 ------------------------------
			//    Image 2      -------------------
			
			// they are not overlapping ( this might happen due to fft zeropadding and extension )
			if ( shift.getLongPosition(d) >= img1.dimension( d ) )
			{
				return null;
			}
			
			offsetImage1[ d ] = shift.getLongPosition(d);
			offsetImage2[ d ] = 0;
			overlapSize = Math.min( img1.dimension( d ) - shift.getLongPosition(d),  img2.dimension( d ) );
			maxImage1[ d ] = offsetImage1[d] + overlapSize -1;
			maxImage2[ d ] = offsetImage2[d] + overlapSize -1;
		}
		else
		{
			// two possiblities
			//
			//          shift start                	  end
			//            |	   |			`		   |
			// A: Image 1      ------------------------------
			//    Image 2 ------------------------------
			//
			//          shift start	     end
			//            |	   |          |
			// B: Image 1      ------------
			//    Image 2 -------------------
			
			// they are not overlapping ( this might happen due to fft zeropadding and extension
			if ( shift.getLongPosition(d) <= -img2.dimension( d ) )
			{
				return null;
			}

			offsetImage1[ d ] = 0;
			offsetImage2[ d ] = -shift.getLongPosition(d);
			overlapSize =  Math.min( img2.dimension( d ) + shift.getLongPosition(d),  img1.dimension( d ) );
			maxImage1[ d ] = offsetImage1[d] + overlapSize -1;
			maxImage2[ d ] = offsetImage2[d] + overlapSize -1;
		}
		
	}		
	
	FinalInterval img1Interval = new FinalInterval(offsetImage1, maxImage1);
	FinalInterval img2Interval = new FinalInterval(offsetImage2, maxImage2);
	
	Pair<Interval, Interval> res = new ValuePair<Interval, Interval>(img1Interval, img2Interval);		
	return res;		
}
 
Example 14
Source File: MVDeconvolution.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
protected static Img< FloatType > loadInitialImage(
		final String fileName,
		final boolean checkNumbers,
		final float minValue,
		final Dimensions dimensions,
		final ImgFactory< FloatType > imageFactory )
{
	IOFunctions.println( "Loading image '" + fileName + "' as start for iteration." );

	final ImagePlus impPSI = LegacyStackImgLoaderIJ.open( new File( fileName ) );

	if ( impPSI == null )
	{
		IOFunctions.println( "Could not load image '" + fileName + "'." );
		return null;
	}

	final long[] dimPsi = impPSI.getStack().getSize() == 1 ? 
			new long[]{ impPSI.getWidth(), impPSI.getHeight() } : new long[]{ impPSI.getWidth(), impPSI.getHeight(), impPSI.getStack().getSize() };
	final Img< FloatType > psi = imageFactory.create( dimPsi, new FloatType() );
	LegacyStackImgLoaderIJ.imagePlus2ImgLib2Img( impPSI, psi, false );

	if ( psi == null )
	{
		IOFunctions.println( "Could not load image '" + fileName + "'." );
		return null;
	}
	else
	{
		boolean dimensionsMatch = true;

		final long dim[] = new long[ dimensions.numDimensions() ];

		for ( int d = 0; d < psi.numDimensions(); ++d )
		{
			if ( psi.dimension( d ) != dimensions.dimension( d ) )
				dimensionsMatch = false;

			dim[ d ] = dimensions.dimension( d );
		}

		if ( !dimensionsMatch )
		{
			IOFunctions.println(
					"Dimensions of '" + fileName + "' do not match: " +
					Util.printCoordinates( dimPsi ) + " != " + Util.printCoordinates( dim ) );
			return null;
		}

		if ( checkNumbers )
		{
			IOFunctions.println(
					"Checking values of '" + fileName + "' you can disable this check by setting " +
					"spim.process.fusion.deconvolution.MVDeconvolution.checkNumbers = false;" );

			boolean smaller = false;
			boolean hasZerosOrNeg = false;
			
			for ( final FloatType v : psi )
			{
				if ( v.get() < minValue )
					smaller = true;

				if ( v.get() <= 0 )
				{
					hasZerosOrNeg = true;
					v.set( minValue );
				}
			}

			if ( smaller )
				IOFunctions.println(
						"Some values '" + fileName + "' are smaller than the minimal value of " +
						minValue + ", this can lead to instabilities." );

			if ( hasZerosOrNeg )
				IOFunctions.println(
						"Some values '" + fileName + "' were smaller or equal to zero," +
						"they have been replaced with the min value of " + minValue );
		}
	}

	return psi;
}
 
Example 15
Source File: ComputeFFTMethodsSize.java    From imagej-ops with BSD 2-Clause "Simplified" License 3 votes vote down vote up
@Override
public long[][] calculate(Dimensions inputDimensions) {

	long[][] size = new long[2][];
	size[0] = new long[inputDimensions.numDimensions()];
	size[1] = new long[inputDimensions.numDimensions()];

	if (fast && forward) {

		FFTMethods.dimensionsRealToComplexFast(inputDimensions, size[0], size[1]);

	}
	else if (!fast && forward) {
		FFTMethods.dimensionsRealToComplexSmall(inputDimensions, size[0],
			size[1]);

	}
	if (fast && !forward) {

		FFTMethods.dimensionsComplexToRealFast(inputDimensions, size[0], size[1]);

	}
	else if (!fast && !forward) {

		FFTMethods.dimensionsComplexToRealSmall(inputDimensions, size[0],
			size[1]);
	}

	return size;

}