Skip to content

Commit

Permalink
virtualstack
Browse files Browse the repository at this point in the history
  • Loading branch information
ekatrukha committed May 1, 2024
1 parent f258cd9 commit ecaf13f
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 57 deletions.
117 changes: 117 additions & 0 deletions src/main/java/averagingND/FloatTiffImgWrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package averagingND;

import ij.ImagePlus;
import net.imglib2.cache.img.CellLoader;
import net.imglib2.cache.img.ReadOnlyCachedCellImgFactory;
import net.imglib2.cache.img.ReadOnlyCachedCellImgOptions;
import net.imglib2.cache.img.SingleCellArrayImg;
import net.imglib2.img.Img;
import net.imglib2.type.numeric.real.FloatType;

public class FloatTiffImgWrap {


public static Img< FloatType > wrapVirtualFloat(final ImagePlus imp, String sDims_)
{
String sDims = sDims_;
final int nDims = sDims.length();
final long[] dimensions = new long[nDims];
final int nBitD = imp.getBitDepth();
for(int i=0;i<nDims;i++)
{
if(sDims.charAt(i)=='X')
{
dimensions[i]=imp.getWidth();
}
if(sDims.charAt(i)=='Y')
{
dimensions[i]=imp.getHeight();
}
if(sDims.charAt(i)=='Z')
{
dimensions[i]=imp.getNSlices();
}
if(sDims.charAt(i)=='C')
{
dimensions[i]=imp.getNChannels();
}
if(sDims.charAt(i)=='T')
{
dimensions[i]=imp.getNFrames();
}
}

// set up cell size such that one cell is one plane
final int[] cellDimensions = new int[] {
imp.getStack().getWidth(),
imp.getStack().getHeight(),
1
};
// make a CellLoader that copies one plane of data from the virtual stack
final CellLoader< FloatType > loader = new CellLoader< FloatType >()
{
@Override
public void load( final SingleCellArrayImg< FloatType , ? > cell ) throws Exception
{
//final int z = ( int ) cell.min( 2 );
int nCh = 1;
int nSl = 1;
int nTp = 1;
for(int i=2;i<nDims;i++)
{
if(sDims.charAt(i)=='C')
{
nCh = (int)cell.min(i)+1;
}
if(sDims.charAt(i)=='Z')
{
nSl = (int)cell.min(i)+1;
}
if(sDims.charAt(i)=='T')
{
nTp = (int)cell.min(i)+1;
}

}

final int stInd = imp.getStackIndex(nCh, nSl, nTp);
//final short[] impdata = ( short[] ) imp.getStack().getProcessor( 1 + z ).getPixels();
final float[] celldata = ( float[] ) cell.getStorageArray();
if(nBitD == 32)
{
final float[] impdata = ( float[] ) imp.getStack().getProcessor(stInd).getPixels();
System.arraycopy( impdata, 0, celldata, 0, celldata.length );
}
if(nBitD == 16)
{
final short[] impdata = ( short[] ) imp.getStack().getProcessor( stInd ).getPixels();
for(int i=0;i<celldata.length;i++)
{
celldata[i]=impdata[i]&0xffff;
}
}
if(nBitD == 8)
{
final byte[] impdata = ( byte[] ) imp.getStack().getProcessor( stInd ).getPixels();
for(int i=0;i<celldata.length;i++)
{
celldata[i]=impdata[i]&0xff;
}
}
//System.arraycopy( impdata, 0, celldata, 0, celldata.length );
}
};
// create a CellImg with that CellLoader
// final Img< FloatType > img = new ReadOnlyCachedCellImgFactory().create(
// dimensions,
// new FloatType(),
// loader,
// ReadOnlyCachedCellImgOptions.options().cellDimensions( cellDimensions ) );
return new ReadOnlyCachedCellImgFactory().create(
dimensions,
new FloatType(),
loader,
ReadOnlyCachedCellImgOptions.options().cellDimensions( cellDimensions ) );
}
//static long[]
}
23 changes: 16 additions & 7 deletions src/main/java/averagingND/GenNormCC.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.fft2.FFT;
import net.imglib2.algorithm.fft2.FFTMethods;
import net.imglib2.converter.Converter;
import net.imglib2.converter.Converters;
import net.imglib2.img.Img;
import net.imglib2.img.ImgFactory;
import net.imglib2.img.ImgView;
Expand All @@ -17,6 +19,9 @@
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.img.basictypeaccess.array.FloatArray;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.img.imageplus.ImagePlusImg;
import net.imglib2.img.imageplus.ImagePlusImgFactory;
import net.imglib2.type.Type;
import net.imglib2.type.numeric.complex.ComplexFloatType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.Intervals;
Expand Down Expand Up @@ -170,12 +175,13 @@ public boolean caclulateGenNormCC(final RandomAccessibleInterval< FloatType > im
//ImageJFunctions.show(padUnitTem).setTitle( "unipadded2" );

//squared image values
Img< FloatType > sqImg = ImgView.wrap(image).copy();
Img< FloatType > sqTem = ImgView.wrap(template).copy();

sqImg.forEach(t-> t.mul(t.get()));
sqTem.forEach(t-> t.mul(t.get()));

//Img< FloatType > sqImg = ImgView.wrap(image).copy();
//Img< FloatType > sqTem = ImgView.wrap(template).copy();
//sqImg.forEach(t-> t.mul(t.get()));
//sqTem.forEach(t-> t.mul(t.get()));
RandomAccessibleInterval< FloatType > sqImg = Converters.convert(image, ( in,out )-> {out.set(in.get()*in.get());}, new FloatType());
RandomAccessibleInterval< FloatType > sqTem = Converters.convert(template, ( in,out )-> {out.set(in.get()*in.get());}, new FloatType());

//padded squared images
IntervalView< FloatType > padSqImg = Views.interval(Views.extendZero(sqImg),imgIntPad);
IntervalView< FloatType > padSqTem = Views.interval(Views.extendZero(sqTem),temIntPad);
Expand All @@ -200,12 +206,14 @@ public boolean caclulateGenNormCC(final RandomAccessibleInterval< FloatType > im
FFTMethods.complexConjugate(sqTemplateFFT2);

//multiplications
final Img< ComplexFloatType > I1F2F2 = multCompl(unImageFFT2,sqTemplateFFT2);

final Img< ComplexFloatType > deNOM = multCompl(unImageFFT2,unTemplateFFT2);
final Img< ComplexFloatType > F1F2 = multCompl(imageFFT2,templateFFT2);
final Img< ComplexFloatType > F1I2 = multCompl(imageFFT2,unTemplateFFT2);
final Img< ComplexFloatType > I1F2 = multCompl(unImageFFT2,templateFFT2);
final Img< ComplexFloatType > F1F1I2 = multCompl(sqImageFFT2,unTemplateFFT2);
final Img< ComplexFloatType > I1F2F2 = multCompl(unImageFFT2,sqTemplateFFT2);


//inverse FFT
final Img< FloatType > invdeNOM = FFT.complexToReal(deNOM, factoryFloat, new FloatType());
Expand Down Expand Up @@ -458,4 +466,5 @@ public static Img< ComplexFloatType > multCompl(final Img< ComplexFloatType > im

return output;
}

}
Loading

0 comments on commit ecaf13f

Please sign in to comment.