Skip to content

Commit

Permalink
Added debug routines
Browse files Browse the repository at this point in the history
  • Loading branch information
CellDynamics committed Jan 12, 2018
1 parent 24a9f10 commit c53dca4
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
Expand Up @@ -2,6 +2,8 @@

import java.awt.Color;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -22,6 +24,7 @@

import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.plugin.ImageCalculator;
import ij.process.BinaryProcessor;
import ij.process.ByteProcessor;
Expand Down Expand Up @@ -968,6 +971,7 @@ protected RealMatrix getMeanSeedLocal(ImageProcessor mask, int localMeanMaskSize
* not contain BG map.
*/
protected ProbabilityMaps solver(Seeds seeds, RealMatrix[] gradients) {
ImageStack debugPm = null;
RealMatrix diffIfg = null; // normalised squared differences to mean seed intensities for FG
// store number of iterations performed for each object. These numbers are used for stopping
// iterations earlier for background object
Expand Down Expand Up @@ -1077,18 +1081,21 @@ protected ProbabilityMaps solver(Seeds seeds, RealMatrix[] gradients) {
// specified relError and after weighting it dominates leaving only original object seed as
// segmented object. Here we stop segmenting background after certain number of iterations but
// not relErr. This is how we have it solved in MAtlab
if (userBckPoints.contains(seedsPointsFg.get(cell)) && iterations.size() > 0) {
if (true && (userBckPoints.contains(seedsPointsFg.get(cell)) && iterations.size() > 0)) {
// just use average of iters for BCK
iter = iterations.stream().mapToInt(Integer::intValue).max().getAsInt() / iterations.size();
// FIXME This can be disabled, then BCK will need more iterations but sometimes results are
// better
// potential pitfall is if user mark BG far from cell, then small number of iters is not
// enough to flood whole background (but it will work because during comparison bck is on 0
// and FG segmentation rather does not leave object
iter /= (currentSweep + 1);
} else { // object - use specified number of iters
iter = params.iter / (currentSweep + 1);
}
// main loop here we simulate diffusion process in time
outerloop: for (i = 0; i < iter; i++) {
if (i % 50 == 0) {
if (i % relErrStep == 0) {
LOGGER.info("Iter: " + i);
} else {
LOGGER.trace("Iter: " + i);
Expand Down Expand Up @@ -1157,6 +1164,19 @@ protected ProbabilityMaps solver(Seeds seeds, RealMatrix[] gradients) {
break outerloop;
}
}
// store probabilities over iterations
if (QuimP.SUPER_DEBUG) {
debugPm =
(debugPm == null) ? new ImageStack(fg.getColumnDimension(), fg.getRowDimension())
: debugPm;
if (i > 1000) {
if (i % 50 == 0) {
debugPm.addSlice(QuimPArrayUtils.realMatrix2ImageProcessor(fg));
}
} else {
debugPm.addSlice(QuimPArrayUtils.realMatrix2ImageProcessor(fg));
}
}
// remember FG map for this iteration to use it to compute relative error in next iteration
QuimPArrayUtils.copy2darray(fg2d, tmpFglast2d);
} // iter
Expand All @@ -1168,6 +1188,16 @@ protected ProbabilityMaps solver(Seeds seeds, RealMatrix[] gradients) {
} else {
ret.put(SeedTypes.FOREGROUNDS, fg);
}
// save stack of probability maps (over iterations) for each processed object separately
if (QuimP.SUPER_DEBUG) {
if (debugPm != null) {
ImagePlus debugIm = new ImagePlus("debug", debugPm);
String tmp = System.getProperty("java.io.tmpdir");
Path p = Paths.get(tmp, "Rw_ProbMap-cell_" + cell);
IJ.saveAsTiff(debugIm, p.toString());
debugPm = null; // next object
}
}
} // cell
return ret;
}
Expand Down
Expand Up @@ -27,8 +27,8 @@ public static void main(String[] args) throws InterruptedException {
// "open(\"src/test/Resources-static/ticket209gh/fluoreszenz-test.tif\")");
// IJ.runMacro("open(\"src/test/Resources-static/ticket209gh/segmented_color.tif\")");

IJ.openImage("src/test/Resources-static/PropagateSeeds/stack.tif").show();
IJ.openImage("src/test/Resources-static/PropagateSeeds/stack-mask.tif").show();
// IJ.openImage("src/test/Resources-static/PropagateSeeds/stack.tif").show();
// IJ.openImage("src/test/Resources-static/PropagateSeeds/stack-mask.tif").show();

// IJ.openImage("src/test/Resources-static/Stack_cut.tif").show();

Expand All @@ -40,10 +40,66 @@ public static void main(String[] args) throws InterruptedException {
// + "selectedFilteringMethod:NONE,hatFilter:false,alev:0.9,num:1,window:15,"
// + "selectedFilteringPostMethod:MEDIAN,showSeeds:false,showPreview:false,"
// + "paramFile:(null)}");
obj.run("");

// obj.run("");

// ************** Run **************
// example1();
example2();
}

/**
* Super debug.
*
* <p>Save selected number of fg and bg prob maps to stack for each iteration.
*/
public static void example2() {
System.setProperty("quimpconfig.superDebug", "true");
new ImageJ();
RandomWalkSegmentationPlugin_ obj = new RandomWalkSegmentationPlugin_();
//!> example 1
IJ.openImage(
"/home/baniuk/Desktop/Tests/284/Stack_1frame.tif")
.show();
IJ.openImage("/home/baniuk/Desktop/Tests/284/SEED_Stack_1frame.tif")
.show();
obj.run("opts={"
+ "algOptions:{"
+ "alpha:400.0,"
+ "beta:50.0,"
+ "gamma:[100.0,0.0],"
+ "iter:10000," // !
+ "dt:0.1,"
+ "relim:[0.00200,0.02],"
+ "useLocalMean:false,"
+ "localMeanMaskSize:23,"
+ "maskLimit:false"
+ "},"
+ "originalImageName:(Stack_1frame.tif),"
+ "selectedSeedSource:RGBImage,"
+ "seedImageName:(SEED_Stack_1frame.tif),"
+ "qconfFile:(null),"
+ "selectedShrinkMethod:NONE,"
+ "shrinkPower:17.0,"
+ "expandPower:15.0,"
+ "scaleSigma:0.3,"
+ "scaleMagn:4.0,"
+ "scaleEqNormalsDist:12.0,"
+ "scaleCurvDistDist:12.0,"
+ "estimateBackground:false,"
+ "selectedFilteringMethod:NONE,"
+ "hatFilter:false,alev:0.9,num:1,window:15,"
+ "selectedFilteringPostMethod:NONE,"
+ "showSeeds:false,"
+ "showPreview:false,"
+ "showProbMaps:false,"
+ "paramFile:(null)}");
IJ.runMacro("selectWindow(\"Segmented_Stack_1frame.tif\");");
IJ.runMacro("run(\"Outline\");");
IJ.runMacro("run(\"Merge Channels...\", \""
+ "c1=Segmented_Stack_1frame.tif "
+ "c4=Stack_1frame.tif create keep\");");
//!<
}

/**
Expand Down

0 comments on commit c53dca4

Please sign in to comment.