Skip to content

Commit df67458

Browse files
committed
average non-masked
1 parent 8086d3b commit df67458

File tree

4 files changed

+39
-156
lines changed

4 files changed

+39
-156
lines changed

src/main/java/averagingND/AverageWithoutZero.java renamed to src/main/java/averagingND/FinalOutput.java

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import net.imglib2.view.IntervalView;
1818
import net.imglib2.view.Views;
1919

20-
public class AverageWithoutZero implements PlugIn {
20+
public class FinalOutput implements PlugIn {
2121

2222
public boolean multiCh = false;
2323
@Override
@@ -211,102 +211,5 @@ public static IntervalView<FloatType> stdArray(final ArrayList<RandomAccessibleI
211211
return stdImg;
212212

213213
}
214-
/** Provided with an ArrayList of RAIs, returns a new ArrayList with two RAIs:
215-
* the first contains cumulative sum of all intensities at a current voxel/pixel location,
216-
* the second contains an integer value equal to how many RAIs have a pixel at this location.
217-
* Since input RAIs could be of different sizes/locations, the output size is made to include
218-
* all of them, i.e. a hyper box that includes them all **/
219-
220-
public static ArrayList<IntervalView<FloatType>> sumAndCountArray(ArrayList<RandomAccessibleInterval< FloatType >> imgs)
221-
{
222-
int i;
223-
224-
FinalInterval intervalMax = getIntervalAverageArray(imgs);
225-
226-
ArrayList<IntervalView< FloatType >> interv = new ArrayList<IntervalView< FloatType >>();
227-
228-
for(i=0;i<imgs.size();i++)
229-
{
230-
interv.add(Views.interval( Views.extendZero(imgs.get(i)),intervalMax));
231-
}
232-
233-
ArrayList<Cursor< FloatType >> cursors = new ArrayList<Cursor< FloatType >>();
234-
for(i=0;i<interv.size();i++)
235-
{
236-
cursors.add(interv.get(i).cursor());
237-
}
238-
239-
final Img<FloatType> sumImgArr = ArrayImgs.floats(intervalMax.dimensionsAsLongArray());
240-
final Img<FloatType> countImgArr = ArrayImgs.floats(intervalMax.dimensionsAsLongArray());
241-
242-
long [] originCoord = intervalMax.minAsLongArray();
243-
244-
final IntervalView<FloatType> sumImg = Views.translate(sumImgArr, originCoord);
245-
final IntervalView<FloatType> countImg = Views.translate(countImgArr, originCoord);
246-
247-
Cursor<FloatType> sumC = sumImg.cursor();
248-
Cursor<FloatType> cntC = countImg.cursor();
249-
Cursor<FloatType> imgC;
250-
long nNumVal;
251-
double nSumVal;
252-
float nValCur;
253-
254-
while(sumC.hasNext())
255-
{
256-
sumC.fwd();
257-
cntC.fwd();
258-
nNumVal = 0;
259-
nSumVal = 0;
260-
for(i=0;i<cursors.size();i++)
261-
{
262-
imgC=cursors.get(i);
263-
imgC.fwd();
264-
nValCur=imgC.get().get();
265-
if(nValCur > 0.0000001)
266-
{
267-
nNumVal++;
268-
nSumVal += nValCur;
269-
}
270-
}
271-
if(nNumVal > 0)
272-
{
273-
sumC.get().set((float)nSumVal);
274-
cntC.get().set((float)nNumVal);
275-
}
276-
}
277-
278-
ArrayList<IntervalView<FloatType>> finalSumCnt = new ArrayList<IntervalView<FloatType>>();
279-
finalSumCnt.add(sumImg);
280-
finalSumCnt.add(countImg);
281-
282-
return finalSumCnt;
283-
284-
}
285-
/** returns an average RAI. It is assumed provided ArrayList contains two RAIs:
286-
* first being cumulative intensity values and the second is count number.
287-
* The returned RAI origin is always at zero (zeroMin) **/
288-
public static IntervalView<FloatType> averageFromSumAndCount(ArrayList<IntervalView<FloatType>> alSumCnt)
289-
{
290214

291-
long [] origin = alSumCnt.get(0).minAsLongArray();
292-
final Img<FloatType> avrgImgArr = ArrayImgs.floats(alSumCnt.get(0).dimensionsAsLongArray());
293-
final IntervalView<FloatType> avrgImg = Views.translate(avrgImgArr, origin );
294-
Cursor<FloatType> avrgC = avrgImg.cursor();
295-
Cursor<FloatType> sumC = alSumCnt.get(0).cursor();
296-
Cursor<FloatType> cntC = alSumCnt.get(1).cursor();
297-
float fCnt;
298-
while(avrgC.hasNext())
299-
{
300-
avrgC.fwd();
301-
sumC.fwd();
302-
cntC.fwd();
303-
fCnt=cntC.get().get();
304-
if(fCnt>0.0f)
305-
{
306-
avrgC.get().set(sumC.get().get()/fCnt);
307-
}
308-
}
309-
310-
return Views.zeroMin(avrgImg);
311-
}
312215
}

src/main/java/averagingND/IterativeAveraging.java

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public void run(String paramString) {
308308
}
309309

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

313313
//sumAndCount = AverageWithoutZero.sumAndCountArray(imgs_shift);
314314

@@ -371,13 +371,14 @@ public void run(String paramString) {
371371

372372
imgs_avrg_out = getAlignedRAIs(shifts, true);
373373

374-
IntervalView<FloatType> finalAver = AverageWithoutZero.averageArray(imgs_avrg_out, bIgnoreZeroInAveraging);
374+
375+
IntervalView<FloatType> finalAver = FinalOutput.averageArray(imgs_avrg_out, bIgnoreZeroInAveraging);
375376

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

@@ -427,50 +428,6 @@ ArrayList<RandomAccessibleInterval< FloatType >> getMultiChAligned(final ArrayLi
427428
return imgs_multiCh_reg;
428429
}
429430

430-
/** given Sum and Count images alSumCnt, this function subtracts removedImage from Sum,
431-
* reduces corresponding Count voxels and returns averaged image (with coordinate origin at (0, 0, ..0) **/
432-
IntervalView<FloatType> removeOneAverage(ArrayList<IntervalView<FloatType>> alSumCnt, RandomAccessibleInterval< FloatType > removedImage)
433-
{
434-
435-
long [] origin = alSumCnt.get(0).minAsLongArray();
436-
final Img<FloatType> avrgImgArr = ArrayImgs.floats(alSumCnt.get(0).dimensionsAsLongArray());
437-
final IntervalView<FloatType> avrgImg = Views.translate(avrgImgArr, origin );
438-
final IntervalView<FloatType> removeInt = Views.interval(Views.extendZero(removedImage), avrgImg);
439-
Cursor<FloatType> avrgC = avrgImg.cursor();
440-
Cursor<FloatType> remC = removeInt.cursor();
441-
Cursor<FloatType> sumC = alSumCnt.get(0).cursor();
442-
Cursor<FloatType> cntC = alSumCnt.get(1).cursor();
443-
float fCnt, fRem, fSum;
444-
while(avrgC.hasNext())
445-
{
446-
447-
avrgC.fwd();
448-
remC.fwd();
449-
sumC.fwd();
450-
cntC.fwd();
451-
fSum = sumC.get().get();
452-
fRem = remC.get().get();
453-
fCnt = cntC.get().get();
454-
if(fRem>0.0f)
455-
{
456-
fSum-=fRem;
457-
fCnt--;
458-
}
459-
460-
if (fCnt>0.5f)
461-
{
462-
avrgC.get().set(fSum/fCnt);
463-
}
464-
else
465-
{
466-
avrgC.get().set(0.0f);
467-
}
468-
}
469-
470-
//return Views.zeroMin(avrgImg);
471-
return avrgImg;
472-
}
473-
474431

475432
/** given input image array imgs_in and shifts, generates corresponding array of applied shifted interval views imgs_out **/
476433
public double buildShiftedIntervals(final ArrayList<RandomAccessibleInterval< FloatType >> imgs_in, final ArrayList<RandomAccessibleInterval< FloatType >> imgs_out, final ArrayList<long []> shifts)
@@ -557,9 +514,24 @@ public void medianCorrectShifts(final long [][] shifts_in)
557514
public void processIntermediate(final int nIt)
558515
{
559516
ArrayList<RandomAccessibleInterval< FloatType >> imgs_avrg_out = getAlignedRAIs(shifts, true);
560-
String sName = "intermediate_average_"+Integer.toString(nIt);
517+
String sName ="";
518+
519+
switch(nAveragingAim)
520+
{
521+
case TemplateAveraging.AVERAGE:
522+
sName="it_avg_";
523+
break;
524+
case TemplateAveraging.MEDIAN:
525+
sName="it_med_";
526+
break;
527+
case TemplateAveraging.MASKED_AVERAGE:
528+
sName="it_mask_avg_";
529+
break;
530+
}
531+
532+
sName = sName+Integer.toString(nIt);
561533
ImagePlus temp;
562-
temp = MiscUtils.wrapFloatImgCal(AverageWithoutZero.averageArray(imgs_avrg_out, bIgnoreZeroInAveraging),sName,imageSet.cal,imageSet.bMultiCh);
534+
temp = MiscUtils.wrapFloatImgCal(FinalOutput.averageArray(imgs_avrg_out, bIgnoreZeroInAveraging),sName,imageSet.cal,imageSet.bMultiCh);
563535
if(bSaveIntermediate)
564536
{
565537
IJ.saveAsTiff(temp, sPathIntermediate +temp.getTitle());
@@ -665,6 +637,15 @@ public boolean dialogSettings()
665637
IJ.log("Initial template: "+ sIniTemplate[nIniTemplate]);
666638

667639
IJ.log("Template calculation: "+ sAveragingAim[nAveragingAim]);
640+
if(nAveragingAim == TemplateAveraging.AVERAGE)
641+
{
642+
bIgnoreZeroInAveraging = false;
643+
}
644+
if(nAveragingAim == TemplateAveraging.MASKED_AVERAGE)
645+
{
646+
bIgnoreZeroInAveraging = true;
647+
}
648+
668649

669650
if(bZeroMask)
670651
{
@@ -752,10 +733,6 @@ public boolean dialogSettings()
752733
{
753734
theDir.mkdirs();
754735
}
755-
//DirectoryChooser dc = new DirectoryChooser ( "Choose a folder to save intermediate averages..." );
756-
//sPathIntermediate = dc.getDirectory();
757-
//if(sPathIntermediate == null)
758-
//return false;
759736

760737
}
761738
if(bOutputInput)

src/main/java/averagingND/RegisterSingleND.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,10 @@ public static void main( final String[] args ) throws ImgIOException, Incompatib
367367

368368
//IJ.open("/home/eugene/Desktop/projects/RegisterNDFFT/4d/HyperStack.tif");
369369
//IJ.open("/home/eugene/Desktop/projects/RegisterNDFFT/4d/HyperStack-1.tif");
370-
IJ.open("/home/eugene/Desktop/projects/RegisterNDFFT/center/full.tif");
371-
IJ.open("/home/eugene/Desktop/projects/RegisterNDFFT/center/center.tif");
370+
//IJ.open("/home/eugene/Desktop/projects/AveragingND/center/full.tif");
371+
//IJ.open("/home/eugene/Desktop/projects/AveragingND/center/center.tif");
372+
IJ.open("/home/eugene/Desktop/projects/AveragingND/centered/full.tif");
373+
IJ.open("/home/eugene/Desktop/projects/AveragingND/centered/center.tif");
372374

373375

374376
RegisterSingleND test = new RegisterSingleND();

src/main/java/averagingND/TemplateAveraging.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.imglib2.RandomAccessibleInterval;
88
import net.imglib2.img.Img;
99
import net.imglib2.img.array.ArrayImgs;
10+
import net.imglib2.img.display.imagej.ImageJFunctions;
1011
import net.imglib2.type.numeric.real.FloatType;
1112
import net.imglib2.util.Intervals;
1213
import net.imglib2.view.IntervalView;
@@ -102,6 +103,7 @@ IntervalView<FloatType> getTemplateForImageAverage(final RandomAccessibleInterva
102103
{
103104
initCurrentTemplate();
104105
}
106+
final float nIm = nImgN-1.0f;
105107

106108
final IntervalView<FloatType> removeInt = Views.interval(Views.extendZero(currentImage), currentTemplate);
107109
final Cursor<FloatType> avrgC = currentTemplate.cursor();
@@ -113,10 +115,9 @@ IntervalView<FloatType> getTemplateForImageAverage(final RandomAccessibleInterva
113115
avrgC.fwd();
114116
remC.fwd();
115117
sumC.fwd();
116-
avrgC.get().set((sumC.get().get()-remC.get().get())/((float)nImgN));
117-
118+
avrgC.get().set((sumC.get().get()-remC.get().get())/nIm);
118119
}
119-
120+
//ImageJFunctions.show(currentTemplate, "Xffs");
120121
//return Views.zeroMin(avrgImg);
121122
return currentTemplate;
122123
}

0 commit comments

Comments
 (0)