Skip to content

Commit

Permalink
average non-masked
Browse files Browse the repository at this point in the history
  • Loading branch information
ekatrukha committed Apr 30, 2024
1 parent 8086d3b commit df67458
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import net.imglib2.view.IntervalView;
import net.imglib2.view.Views;

public class AverageWithoutZero implements PlugIn {
public class FinalOutput implements PlugIn {

public boolean multiCh = false;
@Override
Expand Down Expand Up @@ -211,102 +211,5 @@ public static IntervalView<FloatType> stdArray(final ArrayList<RandomAccessibleI
return stdImg;

}
/** Provided with an ArrayList of RAIs, returns a new ArrayList with two RAIs:
* the first contains cumulative sum of all intensities at a current voxel/pixel location,
* the second contains an integer value equal to how many RAIs have a pixel at this location.
* Since input RAIs could be of different sizes/locations, the output size is made to include
* all of them, i.e. a hyper box that includes them all **/

public static ArrayList<IntervalView<FloatType>> sumAndCountArray(ArrayList<RandomAccessibleInterval< FloatType >> imgs)
{
int i;

FinalInterval intervalMax = getIntervalAverageArray(imgs);

ArrayList<IntervalView< FloatType >> interv = new ArrayList<IntervalView< FloatType >>();

for(i=0;i<imgs.size();i++)
{
interv.add(Views.interval( Views.extendZero(imgs.get(i)),intervalMax));
}

ArrayList<Cursor< FloatType >> cursors = new ArrayList<Cursor< FloatType >>();
for(i=0;i<interv.size();i++)
{
cursors.add(interv.get(i).cursor());
}

final Img<FloatType> sumImgArr = ArrayImgs.floats(intervalMax.dimensionsAsLongArray());
final Img<FloatType> countImgArr = ArrayImgs.floats(intervalMax.dimensionsAsLongArray());

long [] originCoord = intervalMax.minAsLongArray();

final IntervalView<FloatType> sumImg = Views.translate(sumImgArr, originCoord);
final IntervalView<FloatType> countImg = Views.translate(countImgArr, originCoord);

Cursor<FloatType> sumC = sumImg.cursor();
Cursor<FloatType> cntC = countImg.cursor();
Cursor<FloatType> imgC;
long nNumVal;
double nSumVal;
float nValCur;

while(sumC.hasNext())
{
sumC.fwd();
cntC.fwd();
nNumVal = 0;
nSumVal = 0;
for(i=0;i<cursors.size();i++)
{
imgC=cursors.get(i);
imgC.fwd();
nValCur=imgC.get().get();
if(nValCur > 0.0000001)
{
nNumVal++;
nSumVal += nValCur;
}
}
if(nNumVal > 0)
{
sumC.get().set((float)nSumVal);
cntC.get().set((float)nNumVal);
}
}

ArrayList<IntervalView<FloatType>> finalSumCnt = new ArrayList<IntervalView<FloatType>>();
finalSumCnt.add(sumImg);
finalSumCnt.add(countImg);

return finalSumCnt;

}
/** returns an average RAI. It is assumed provided ArrayList contains two RAIs:
* first being cumulative intensity values and the second is count number.
* The returned RAI origin is always at zero (zeroMin) **/
public static IntervalView<FloatType> averageFromSumAndCount(ArrayList<IntervalView<FloatType>> alSumCnt)
{

long [] origin = alSumCnt.get(0).minAsLongArray();
final Img<FloatType> avrgImgArr = ArrayImgs.floats(alSumCnt.get(0).dimensionsAsLongArray());
final IntervalView<FloatType> avrgImg = Views.translate(avrgImgArr, origin );
Cursor<FloatType> avrgC = avrgImg.cursor();
Cursor<FloatType> sumC = alSumCnt.get(0).cursor();
Cursor<FloatType> cntC = alSumCnt.get(1).cursor();
float fCnt;
while(avrgC.hasNext())
{
avrgC.fwd();
sumC.fwd();
cntC.fwd();
fCnt=cntC.get().get();
if(fCnt>0.0f)
{
avrgC.get().set(sumC.get().get()/fCnt);
}
}

return Views.zeroMin(avrgImg);
}
}
83 changes: 30 additions & 53 deletions src/main/java/averagingND/IterativeAveraging.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public void run(String paramString) {
}

// calculate new img array with applied displacements
cumShift = buildShiftedIntervals(imageSet.imgs, imgs_shift,shifts);
cumShift = buildShiftedIntervals(imageSet.imgs, imgs_shift, shifts);

//sumAndCount = AverageWithoutZero.sumAndCountArray(imgs_shift);

Expand Down Expand Up @@ -371,13 +371,14 @@ public void run(String paramString) {

imgs_avrg_out = getAlignedRAIs(shifts, true);

IntervalView<FloatType> finalAver = AverageWithoutZero.averageArray(imgs_avrg_out, bIgnoreZeroInAveraging);

IntervalView<FloatType> finalAver = FinalOutput.averageArray(imgs_avrg_out, bIgnoreZeroInAveraging);

MiscUtils.wrapFloatImgCal(finalAver,"final_average_"+Integer.toString(nIterMax),imageSet.cal,imageSet.bMultiCh).show();
IJ.log("...done.");
//calculate STD image
IJ.log("calculating final standard deviation image..");
IntervalView<FloatType> finalSTD = AverageWithoutZero.stdArray(imgs_avrg_out, finalAver, bIgnoreZeroInAveraging);
IntervalView<FloatType> finalSTD = FinalOutput.stdArray(imgs_avrg_out, finalAver, bIgnoreZeroInAveraging);
MiscUtils.wrapFloatImgCal(finalSTD,"final_std_"+Integer.toString(nIterMax),imageSet.cal,imageSet.bMultiCh).show();
IJ.log("...done.");

Expand Down Expand Up @@ -427,50 +428,6 @@ ArrayList<RandomAccessibleInterval< FloatType >> getMultiChAligned(final ArrayLi
return imgs_multiCh_reg;
}

/** given Sum and Count images alSumCnt, this function subtracts removedImage from Sum,
* reduces corresponding Count voxels and returns averaged image (with coordinate origin at (0, 0, ..0) **/
IntervalView<FloatType> removeOneAverage(ArrayList<IntervalView<FloatType>> alSumCnt, RandomAccessibleInterval< FloatType > removedImage)
{

long [] origin = alSumCnt.get(0).minAsLongArray();
final Img<FloatType> avrgImgArr = ArrayImgs.floats(alSumCnt.get(0).dimensionsAsLongArray());
final IntervalView<FloatType> avrgImg = Views.translate(avrgImgArr, origin );
final IntervalView<FloatType> removeInt = Views.interval(Views.extendZero(removedImage), avrgImg);
Cursor<FloatType> avrgC = avrgImg.cursor();
Cursor<FloatType> remC = removeInt.cursor();
Cursor<FloatType> sumC = alSumCnt.get(0).cursor();
Cursor<FloatType> cntC = alSumCnt.get(1).cursor();
float fCnt, fRem, fSum;
while(avrgC.hasNext())
{

avrgC.fwd();
remC.fwd();
sumC.fwd();
cntC.fwd();
fSum = sumC.get().get();
fRem = remC.get().get();
fCnt = cntC.get().get();
if(fRem>0.0f)
{
fSum-=fRem;
fCnt--;
}

if (fCnt>0.5f)
{
avrgC.get().set(fSum/fCnt);
}
else
{
avrgC.get().set(0.0f);
}
}

//return Views.zeroMin(avrgImg);
return avrgImg;
}


/** given input image array imgs_in and shifts, generates corresponding array of applied shifted interval views imgs_out **/
public double buildShiftedIntervals(final ArrayList<RandomAccessibleInterval< FloatType >> imgs_in, final ArrayList<RandomAccessibleInterval< FloatType >> imgs_out, final ArrayList<long []> shifts)
Expand Down Expand Up @@ -557,9 +514,24 @@ public void medianCorrectShifts(final long [][] shifts_in)
public void processIntermediate(final int nIt)
{
ArrayList<RandomAccessibleInterval< FloatType >> imgs_avrg_out = getAlignedRAIs(shifts, true);
String sName = "intermediate_average_"+Integer.toString(nIt);
String sName ="";

switch(nAveragingAim)
{
case TemplateAveraging.AVERAGE:
sName="it_avg_";
break;
case TemplateAveraging.MEDIAN:
sName="it_med_";
break;
case TemplateAveraging.MASKED_AVERAGE:
sName="it_mask_avg_";
break;
}

sName = sName+Integer.toString(nIt);
ImagePlus temp;
temp = MiscUtils.wrapFloatImgCal(AverageWithoutZero.averageArray(imgs_avrg_out, bIgnoreZeroInAveraging),sName,imageSet.cal,imageSet.bMultiCh);
temp = MiscUtils.wrapFloatImgCal(FinalOutput.averageArray(imgs_avrg_out, bIgnoreZeroInAveraging),sName,imageSet.cal,imageSet.bMultiCh);
if(bSaveIntermediate)
{
IJ.saveAsTiff(temp, sPathIntermediate +temp.getTitle());
Expand Down Expand Up @@ -665,6 +637,15 @@ public boolean dialogSettings()
IJ.log("Initial template: "+ sIniTemplate[nIniTemplate]);

IJ.log("Template calculation: "+ sAveragingAim[nAveragingAim]);
if(nAveragingAim == TemplateAveraging.AVERAGE)
{
bIgnoreZeroInAveraging = false;
}
if(nAveragingAim == TemplateAveraging.MASKED_AVERAGE)
{
bIgnoreZeroInAveraging = true;
}


if(bZeroMask)
{
Expand Down Expand Up @@ -752,10 +733,6 @@ public boolean dialogSettings()
{
theDir.mkdirs();
}
//DirectoryChooser dc = new DirectoryChooser ( "Choose a folder to save intermediate averages..." );
//sPathIntermediate = dc.getDirectory();
//if(sPathIntermediate == null)
//return false;

}
if(bOutputInput)
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/averagingND/RegisterSingleND.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,10 @@ public static void main( final String[] args ) throws ImgIOException, Incompatib

//IJ.open("/home/eugene/Desktop/projects/RegisterNDFFT/4d/HyperStack.tif");
//IJ.open("/home/eugene/Desktop/projects/RegisterNDFFT/4d/HyperStack-1.tif");
IJ.open("/home/eugene/Desktop/projects/RegisterNDFFT/center/full.tif");
IJ.open("/home/eugene/Desktop/projects/RegisterNDFFT/center/center.tif");
//IJ.open("/home/eugene/Desktop/projects/AveragingND/center/full.tif");
//IJ.open("/home/eugene/Desktop/projects/AveragingND/center/center.tif");
IJ.open("/home/eugene/Desktop/projects/AveragingND/centered/full.tif");
IJ.open("/home/eugene/Desktop/projects/AveragingND/centered/center.tif");


RegisterSingleND test = new RegisterSingleND();
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/averagingND/TemplateAveraging.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.Intervals;
import net.imglib2.view.IntervalView;
Expand Down Expand Up @@ -102,6 +103,7 @@ IntervalView<FloatType> getTemplateForImageAverage(final RandomAccessibleInterva
{
initCurrentTemplate();
}
final float nIm = nImgN-1.0f;

final IntervalView<FloatType> removeInt = Views.interval(Views.extendZero(currentImage), currentTemplate);
final Cursor<FloatType> avrgC = currentTemplate.cursor();
Expand All @@ -113,10 +115,9 @@ IntervalView<FloatType> getTemplateForImageAverage(final RandomAccessibleInterva
avrgC.fwd();
remC.fwd();
sumC.fwd();
avrgC.get().set((sumC.get().get()-remC.get().get())/((float)nImgN));

avrgC.get().set((sumC.get().get()-remC.get().get())/nIm);
}
//ImageJFunctions.show(currentTemplate, "Xffs");
//return Views.zeroMin(avrgImg);
return currentTemplate;
}
Expand Down

0 comments on commit df67458

Please sign in to comment.