Commit 7a4abe72 authored by Andrey Filippov's avatar Andrey Filippov

Modified grid contrast calculation

parent 288a87a6
......@@ -317,8 +317,11 @@ public static MatchSimulatedPattern.DistortionParameters DISTORTION =new MatchSi
0.6, //2.0, //0.5, //0.0, // correlationLowPassSigma, - fraction of the frequency range
0.4, // correlationRingWidth- ring (around r=0.5 dist to opposite corr) width , center circle r=0.5*PATTERN_DETECT.corrRingWidth
8.0, // 3.0, // correlationMaxOffset, // maximal distance between predicted and actual pattern node
1.0, // 2.0, // increase back to .5? was needed with fisheye. 5.0, // double correlationMinContrast, // minimal contrast for the pattern to pass
1.5, // 2.5, // correlationMinInitialContrast, // minimal contrast for the pattern of the center (initial point)
3.0, // 2.0, // increase back to .5? was needed with fisheye. 5.0, // double correlationMinContrast, // minimal contrast for the pattern to pass
3.5, // 2.5, // correlationMinInitialContrast, // minimal contrast for the pattern of the center (initial point)
1.0, //this.correlationMinAbsoluteContrast, // minimal contrast for the pattern to pass, does not compensate for low ligt
// TODO: adjust to a reasonable number
1.0, //this.correlationMinAbsoluteInitialContrast, // minimal contrast for the pattern of the center (initial point)
0.8, // scaleFirstPassContrast, // Decrease contrast of cells that are too close to the border to be processed in rifinement pass
0.1, // contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
......@@ -16168,14 +16171,16 @@ private double [][] jacobianByJacobian(double [][] jacobian, boolean [] mask) {
imp.getTitle());
// model_corr=fht_instance.correlate(pixels[4],sim_pix[4],0); // destroys operands
WVgreens=matrix2x2_mul(patternMap[nTileY][nTileX],invConvMatrix);
contrast= matchSimulatedPattern.correlationContrast (model_corr, // square pixel array
contrast= matchSimulatedPattern.correlationContrast (
model_corr, // square pixel array
pixels[4],
WVgreens, // wave vectors (same units as the pixels array)
// patternDetectParameters.corrRingWidth, // ring (around r=0.5 dist to opposite corr) width
0.1, // contrastSelectSigma
0.5, // contrastAverageSigma
0.0, // x0, // center coordinates
0.0, //y0,
title); // title base for optional plots names
title)[0]; // title base for optional plots names
// System.out.println("Pattern correlation contrast= "+IJ.d2s(contrast,3)+ ", threshold is "+PATTERN_DETECT.minCorrContrast);
if (!(contrast >= patternDetectParameters.minCorrContrast)) patternMap[nTileY][nTileX]=null; // still getting NaN sometimes
}
......@@ -19219,15 +19224,18 @@ use the result to create a rejectiobn mask - if the energy was high, (multiplica
gd.addNumericField("Correlation low-pass sigma (fraction of sqrt(2)*Nyquist, lower - more filtering, 0 -none):",distortionParameters.correlationLowPassSigma, 3);
gd.addNumericField("Correlation maximal offset from predicted:",distortionParameters.correlationMaxOffset, 3);
gd.addNumericField("Detection ring width (fraction):", distortionParameters.correlationRingWidth, 3);
gd.addNumericField("Correlation minimal contrast:", distortionParameters.correlationMinContrast, 3);
gd.addNumericField("Correlation minimal contrast for initial search:", distortionParameters.correlationMinInitialContrast, 3);
gd.addNumericField("Correlation minimal contrast (normalized)", distortionParameters.correlationMinContrast, 3);
gd.addNumericField("Correlation minimal contrast for initial search (normalized)", distortionParameters.correlationMinInitialContrast, 3);
gd.addNumericField("Correlation minimal contrast (absolute)", distortionParameters.correlationMinContrast, 3);
gd.addNumericField("Correlation minimal contrast for initial search (absolute)", distortionParameters.correlationMinInitialContrast, 3);
gd.addNumericField("Decrease contrast of cells that are too close to the border to be processed in rifinement pass", distortionParameters.scaleFirstPassContrast, 3);
gd.addNumericField("Gaussian sigma to select correlation centers (fraction of UV period), 0.1", distortionParameters.contrastSelectSigma, 3);
gd.addNumericField("Gaussian sigma to average correlation variations (as contrast reference), 0.5", distortionParameters.contrastAverageSigma, 3);
gd.addNumericField("Minimal initial pattern cluster size (0 - disable retries)", distortionParameters.minimalPatternCluster, 0);
gd.addNumericField("Scale minimal contrast if the initial cluster is nonzero but smaller", distortionParameters.scaleMinimalInitialContrast, 3);
gd.addNumericField("Minimal initial pattern cluster size (0 - disable retries)", distortionParameters.correlationMinAbsoluteContrast, 0);
gd.addNumericField("Scale minimal contrast if the initial cluster is nonzero but smaller", distortionParameters.correlationMinAbsoluteInitialContrast, 3);
gd.addNumericField("Overlap of FFT areas when searching for pattern", distortionParameters.searchOverlap, 3);
gd.addNumericField("Pattern subdivision:", distortionParameters.patternSubdiv, 0);
......@@ -19301,6 +19309,9 @@ use the result to create a rejectiobn mask - if the energy was high, (multiplica
distortionParameters.correlationMinContrast= gd.getNextNumber();
distortionParameters.correlationMinInitialContrast= gd.getNextNumber();
distortionParameters.correlationMinAbsoluteContrast= gd.getNextNumber();
distortionParameters.correlationMinAbsoluteInitialContrast= gd.getNextNumber();
distortionParameters.scaleFirstPassContrast= gd.getNextNumber();
distortionParameters.contrastSelectSigma= gd.getNextNumber();
distortionParameters.contrastAverageSigma= gd.getNextNumber();
......
......@@ -961,7 +961,9 @@ public class MatchSimulatedPattern {
return result;
}
/* ======================================================================== */
public double correlationContrast ( double [] pixels, // square pixel array
public double [] correlationContrast (
double [] pixels, // square pixel array
double [] widowedGreens, // array to normailze correlation result
double [][] wVectors, // wave vectors (same units as the pixels array)
// double ringWidth, // ring (around r=0.5 dist to opposite corr) width
double contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
......@@ -970,9 +972,21 @@ public class MatchSimulatedPattern {
double x0, // center coordinates
double y0,
String title){
// for now - just comparison, later - switch to
return correlationContrast (
pixels, // square pixel array
wVectors, // wave vectors (same units as the pixels array)
contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
x0, // center coordinates
y0,
title, // title base for optional plots names
this.debugLevel);
/*
return correlationContrast (
pixels, // square pixel array
widowedGreens,
wVectors, // wave vectors (same units as the pixels array)
// ringWidth, // ring (around r=0.5 dist to opposite corr) width
contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
contrastAverageSigma, // Gaussian sigma to average correlation variations (as contrast reference) 0.5
......@@ -980,6 +994,7 @@ public class MatchSimulatedPattern {
y0,
title, // title base for optional plots names
this.debugLevel);
*/
}
public double correlationContrastOld ( double [] pixels, // square pixel array
double [][] wVectors, // wave vectors (same units as the pixels array)
......@@ -1067,8 +1082,82 @@ public class MatchSimulatedPattern {
return contrast;
}
public double correlationContrast (
public double [] correlationContrast (
double [] pixels, // square pixel array
// double [] widowedGreens, // array to normailze correlation result
double [][] wVectors, // wave vectors (same units as the pixels array)
double sigma,
double x0, // center coordinates
double y0,
String title, // title base for optional plots names
int debugLevel){
double [] badContrasts={-1.0,-1.0};
double sigma32=9*sigma*sigma;
double k=-0.5/(sigma*sigma);
double [][] sampleCentersXY={{0.0,0.0},{0.25,0.25},{0.25,-0.20},{-0.25,0.25},{-0.25,-0.25}};
int [] sampleTypes = {0,1,1,1,1};
int size=(int) Math.sqrt(pixels.length);
double [] xy= new double [2];
double [] uv;
double r2;
int i,j;
double [] dbgMask= new double[size*size];
for (int n=0;n<dbgMask.length;n++) dbgMask[n]=0.0;
double [] s={0.0,0.0};
double [] w={0.0,0.0};
for (i=0;i<size;i++) {
xy[1]=i-size/2-y0;
for (j=0;j<size;j++) {
int index=i*size+j;
xy[0]=j-size/2-x0;
uv=matrix2x2_mul(wVectors,xy);
for (int np=0;np<sampleCentersXY.length;np++){
double dx=uv[0]-sampleCentersXY[np][0];
double dy=uv[1]-sampleCentersXY[np][1];
r2=dx*dx+dy*dy;
if (r2<sigma32){
double m=Math.exp(k*r2);
dbgMask[index]+=m;
w[sampleTypes[np]]+=m;
double d=m*pixels[index];
if (sampleTypes[np]>0)d*=pixels[index]; // squared
s[sampleTypes[np]]+=d;
}
}
}
}
if ((w[0]==0.0) || (w[1]==0.0)) {
if (debugLevel>1) System.out.println("Not enough data for correlation contrast: center - w[0]="+w[0]+" opposite - w[1]="+w[1]);
return badContrasts;
}
double aCenter= s[0]/w[0];
double aQuiet=Math.sqrt(s[1]/w[1]);
double rContrast=aCenter/aQuiet;
double aContrast=aCenter/size/size;
double [] contrasts={rContrast,aContrast};
if (debugLevel>2){
System.out.println("correlationContrast() rContrast="+rContrast+" aContrast="+ aContrast+" aCenter="+aCenter+" aQuiet="+aQuiet+" w[0]="+w[0]+" w[1]="+w[1]+" s[0]="+s[0]+" s[1]="+s[1]);
}
if (debugLevel>2) {
System.out.println("Correlation contrast is: relative="+rContrast+" absolute="+aContrast);
double [][] dbgPixels={pixels,dbgMask};
String [] titles={"all","mask"};
(new showDoubleFloatArrays()).showArrays(
dbgPixels,
size,
size,
true,
title+"_MASK",
titles);
}
return contrasts;
}
public double correlationContrastOld2 (
double [] pixels, // square pixel array
double [] widowedGreens, // array to normailze correlation result
double [][] wVectors, // wave vectors (same units as the pixels array)
double sigma,
double sigmaNorm, // to measure variations for normalization of the contrast
......@@ -1093,6 +1182,7 @@ public class MatchSimulatedPattern {
double [] uv;
double r2;
int i,j;
/* opposite sign correlation points in uv are at uv=(0,-0.5),(0,0.5), (-0.5,0) and (0.5,0), with radius of (1/2)
selecting center circle and a ring from 0.25 to 0.75 of the distance to opposite sign correlations */
double [] dbgMask= new double[size*size];
......@@ -1100,6 +1190,8 @@ public class MatchSimulatedPattern {
double [] s={0.0,0.0};
double [] w={0.0,0.0};
double S0=0.0,S1=0.0,S2=0.0;
double SG1=0.0,SG2=0.0;
// Find measured pixels variations in the window
for (i=0;i<size;i++) {
xy[1]=i-size/2-y0;
for (j=0;j<size;j++) {
......@@ -1123,6 +1215,8 @@ public class MatchSimulatedPattern {
S0+=m;
S1+=m*pixels[index];
S2+=m*pixels[index]*pixels[index];
SG1=m*widowedGreens[index];
SG2=m*widowedGreens[index]*widowedGreens[index];
}
}
}
......@@ -1131,9 +1225,13 @@ public class MatchSimulatedPattern {
return -1.0;
}
double ref=Math.sqrt(S2*S0-S1*S1)/S0;
double refG=Math.sqrt(SG2*S0-SG1*SG1)/S0;
double contrast=((s[0]/w[0]) -(s[1]/w[1]))/ref;
double contrastG=((s[0]/w[0]) -(s[1]/w[1]))/refG; ///size;
if (debugLevel>2){
System.out.println("correlationContrast() contrast="+contrast+" w[0]="+w[0]+" w[1]="+w[1]+" s[0]="+s[0]+" s[1]="+s[1]+" S0="+S0+" S1="+S1+" S2="+S2+" ref="+ref);
System.out.println("correlationContrast() corr_diff="+(((s[0]/w[0]) -(s[1]/w[1])))+" contrast="+contrast+" w[0]="+w[0]+" w[1]="+w[1]+" s[0]="+s[0]+" s[1]="+s[1]);
System.out.println("correlationContrast() S0="+S0+" S1="+S1+" S2="+S2+" ref="+ref);
System.out.println("correlationContrast() contrastG="+contrastG+" S0="+S0+" SG1="+SG1+" SG2="+SG2+" refG="+refG);
}
// if (contrast>3.0){
// System.out.println("correlationContrast() contrast="+contrast+" w[0]="+w[0]+" w[1]="+w[1]+" s[0]="+s[0]+" s[1]="+s[1]+" S0="+S0+" S1="+S1+" S2="+S2+" ref="+ref);
......@@ -1156,6 +1254,7 @@ public class MatchSimulatedPattern {
}
/* ======================================================================== */
public double[] correlateWithModel (double [] imagePixels, // measured pixel array
double [] modelPixels, // simulated (model) pixel array)
......@@ -3047,7 +3146,7 @@ public class MatchSimulatedPattern {
DistortionParameters thisDistortionParameters=distortionParameters.clone();
thisDistortionParameters.correlationMaxOffset=0; // no verification of the offset here
thisDistortionParameters.correlationMinContrast= distortionParameters.correlationMinInitialContrast; // different contrast minimum here
thisDistortionParameters.correlationMinAbsoluteContrast= distortionParameters.correlationMinAbsoluteInitialContrast; // different contrast minimum here
int was_debug_level=debugLevel;
int [] iUV= new int [2];
final boolean updating=(PATTERN_GRID!=null);
......@@ -3235,8 +3334,12 @@ public class MatchSimulatedPattern {
if (!updating){
double [][] node=gn.getNode();
double [] centerXY=node[0];
if (debugLevel>1) {
// if (debugLevel>1) {
if (global_debug_level>1) {
System.out.println("*** distortions: Center x="+IJ.d2s(centerXY[0],3)+" y="+ IJ.d2s(centerXY[1],3));
System.out.println("*** distortions: setting debugX="+IJ.d2s(centerXY[0],3)+" debugY="+ IJ.d2s(centerXY[1],3));
patternDetectParameters.debugX=centerXY[0]; //
patternDetectParameters.debugY=centerXY[1]; //patternDetectParameters.debugRadius);
}
debugLevel=debug_level;
// Reset pattern grid
......@@ -3249,6 +3352,8 @@ public class MatchSimulatedPattern {
node[2]);
waveFrontList.clear();
putInWaveList(waveFrontList, centerUV, 0);
}
// Each layer processing may be multi-threaded, they join before going to the next layer
......@@ -3309,7 +3414,7 @@ public class MatchSimulatedPattern {
if ((iUV[0]<0) || (iUV[1]<0) ||
(iUV[0]>=distortionParameters.gridSize) || (iUV[1]>=distortionParameters.gridSize)) continue; // don't fit into UV grid
if (!isCellNew(PATTERN_GRID,iUV)) continue; // already processed
if (!isCellNew(PATTERN_GRID,iUV)) continue; // already processed (or deleted!)
// add uv and dir to the list
// public boolean [] focusMask=null; // array matching image pixels, used with focusing (false outside sample areas)
// New: if it is updating the grid and focusMask is defined - do not go more than 1 step away from the needed image area
......@@ -3344,11 +3449,14 @@ public class MatchSimulatedPattern {
int [] iUVRef=new int[2];
iUVRef[0]=iUVdir[0]+directionsUV[iUVdir[2]][0];
iUVRef[1]=iUVdir[1]+directionsUV[iUVdir[2]][1];
// refCell - is where it came from, but if the initials are disabled, it is null
double [][] refCell=PATTERN_GRID[iUVRef[1]][iUVRef[0]]; // should never be null as it is an old one
if (refCell==null){
System.out.println("**** refCell==null - what does it mean?****");
System.out.println("**** refCell==null - what does it mean?**** u="+iUVRef[0]+" v="+iUVRef[1]);
continue;
} else if ((refCell[0]!=null) && (refCell[0].length>3)){
System.out.println("**** refCell was deleted ****");
}
//found reference cell, calculate x/y, make sure it is inside the selection w/o borders
double [][] wv=new double [2][];
......@@ -3568,19 +3676,22 @@ public class MatchSimulatedPattern {
}
}
if (debugLevel>1) System.out.println("***** Starting cleanup, wave length="+waveFrontList.size());
if (global_debug_level>1) System.out.println("***** Starting cleanup, wave length="+waveFrontList.size());
}
// end of layer
if (initialWave!=null){ // just after the first layer (usually one cell) - delete it and add next time - otherwise first one needs large correction
if (debugLevel>0) System.out.println("Removing "+initialWave.size()+" initial wave cells");
if (global_debug_level>0)
System.out.println("Removing "+initialWave.size()+" initial wave cells");
while (initialWave.size()>0){
uvdir= getWaveList (initialWave,0);
clearPatternGridCell(PATTERN_GRID, uvdir);
// clearPatternGridCell(PATTERN_GRID, uvdir);
if (global_debug_level>0)
System.out.println("Removing x="+uvdir[0]+" y="+uvdir[1]+" dir="+uvdir[2]);
markDeletedPatternGridCell(PATTERN_GRID, uvdir);
initialWave.remove(0);
}
initialWave=null;
}
}//while (waveFrontList.size()>0)
debugLevel=was_debug_level;
/*
......@@ -3645,9 +3756,13 @@ public class MatchSimulatedPattern {
}
// more tests here (moved from the caller) that result is good
double averageGridPeriod=Double.NaN;
if (this.PATTERN_GRID!=null) averageGridPeriod=averageGridPeriod(this.PATTERN_GRID);
double [] gridPeriods={Double.NaN,Double.NaN};
if (this.PATTERN_GRID!=null) {
averageGridPeriod=averageGridPeriod(this.PATTERN_GRID);
gridPeriods=averageGridPeriods(this.PATTERN_GRID); // {min,max}
}
if (debugLevel>0){
System.out.println("Pattern period="+averageGridPeriod+
System.out.println("Pattern period="+averageGridPeriod+" {"+gridPeriods[0]+","+gridPeriods[1]+"}"+
" limits are set to :"+patternDetectParameters.minGridPeriod+","+patternDetectParameters.maxGridPeriod);
}
if (!Double.isNaN(averageGridPeriod)) {
......@@ -4582,8 +4697,10 @@ public class MatchSimulatedPattern {
iUV1[0]=iUV[0]+dirs[dir][0];
iUV1[1]=iUV[1]+dirs[dir][1];
if (isCellDefined(patternGrid,iUV1)){
dx=patternGrid[iUV1[1]][iUV1[0]][0][0]-patternGrid[iUV1[1]][iUV[0]][0][0];
dy=patternGrid[iUV1[1]][iUV1[0]][0][1]-patternGrid[iUV1[1]][iUV[0]][0][1];
// dx=patternGrid[iUV1[1]][iUV1[0]][0][0]-patternGrid[iUV1[1]][iUV[0]][0][0]; // old bug, skewed period!
// dy=patternGrid[iUV1[1]][iUV1[0]][0][1]-patternGrid[iUV1[1]][iUV[0]][0][1]; // old bug, skewed period!
dx=patternGrid[iUV1[1]][iUV1[0]][0][0]-patternGrid[iUV[1]][iUV[0]][0][0];
dy=patternGrid[iUV1[1]][iUV1[0]][0][1]-patternGrid[iUV[1]][iUV[0]][0][1];
sum+=dx*dx+dy*dy;
n++;
}
......@@ -4592,6 +4709,39 @@ public class MatchSimulatedPattern {
if (n>0) sum/=n;
return Math.sqrt (sum);
}
/* ======================================================================== */
public double [] averageGridPeriods( // min,max for u,v
double [][][][] patternGrid){
double [] result={Double.NaN,Double.NaN};
// int n=0;
double [] sum={0.0,0.0};
int [] numSamples={0,0};
int [] iUV=new int[2];
int [] iUV1=new int[2];
int [][]dirs={{0,1},{1,0}};
double dx,dy;
for (iUV[1]=0;iUV[1]<patternGrid.length-1;iUV[1]++) for (iUV[0]=0;iUV[0]<patternGrid[0].length-1;iUV[0]++) if (isCellDefined(patternGrid,iUV)){
for (int dir=0;dir<dirs.length;dir++){
iUV1[0]=iUV[0]+dirs[dir][0];
iUV1[1]=iUV[1]+dirs[dir][1];
if (isCellDefined(patternGrid,iUV1)){
dx=patternGrid[iUV1[1]][iUV1[0]][0][0]-patternGrid[iUV[1]][iUV[0]][0][0];
dy=patternGrid[iUV1[1]][iUV1[0]][0][1]-patternGrid[iUV[1]][iUV[0]][0][1];
sum[dir]+=dx*dx+dy*dy;
numSamples[dir]++;
}
}
}
for (int dir=0;dir<dirs.length;dir++){
if (numSamples[dir]>0) result[dir]=Math.sqrt (sum[dir]/numSamples[dir]);
}
if (result[0]>result[1]){
double tmp=result[0];
result[0]=result[1];
result[1]=tmp;
}
return result;
}
/* ======================================================================== */
public double [] calcFlatFieldForGrid(
......@@ -5265,10 +5415,26 @@ public class MatchSimulatedPattern {
int [] uv){
grid[uv[1]][uv[0]]=null;
}
private void markDeletedPatternGridCell(
double [][][][] grid,
int [] uv){
if ((grid[uv[1]][uv[0]]!=null) && (grid[uv[1]][uv[0]][0]!=null)) {
double [] newXYC=new double[4];
for (int i=0;i<newXYC.length;i++){
if (i<grid[uv[1]][uv[0]][0].length)
newXYC[i]=grid[uv[1]][uv[0]][0][i];
else
newXYC[i]=Double.NaN;
}
}
grid[uv[1]][uv[0]]=null;
}
private boolean isCellNew( //modified, for invalid uv will return "not new"
double [][][][] grid,
int [] uv){
return (uv[1]>=0) && (uv[0]>=0) && (uv[1]<grid.length) && (uv[0]<grid[uv[1]].length) && (grid[uv[1]][uv[0]]==null);
// return (uv[1]>=0) && (uv[0]>=0) && (uv[1]<grid.length) && (uv[0]<grid[uv[1]].length) && (grid[uv[1]][uv[0]]==null);
// 4-th element is added to mark that the cell is dleted, but keep coordinates
return (uv[1]>=0) && (uv[0]>=0) && (uv[1]<grid.length) && (uv[0]<grid[uv[1]].length) && ((grid[uv[1]][uv[0]]==null) || (grid[uv[1]][uv[0]].length>3));
}
private boolean isCellValid(
double [][][][] grid,
......@@ -7700,6 +7866,7 @@ y=xy0[1] + dU*deltaUV[0]*(xy1[1]-xy0[1])+dV*deltaUV[1]*(xy2[1]-xy0[1])
System.out.println("==========Showing simGreensCentered"+ixc+":"+iyc);
SDFA_INSTANCE.showArrays(simGreensCentered.clone(), "simGreensCentered"+ixc+":"+iyc);
SDFA_INSTANCE.showArrays(greens.clone(), "greensWidowed"+ixc+":"+iyc);
// System.out.println("debug_level="+debug_level+" *** Remove next line ***");
// sim_pix[14]=null; // make it crash here
}
......@@ -7740,25 +7907,39 @@ y=xy0[1] + dU*deltaUV[0]*(xy1[1]-xy0[1])+dV*deltaUV[1]*(xy2[1]-xy0[1])
}
// Verify contrast (if specified) - only for the center sample (numNeib==0)
if (numNeib==0) {
contrast= correlationContrast(modelCorr,
double [] contrasts= correlationContrast(
modelCorr,
greens,
WVgreens, // wave vectors (same units as the pixels array)
// distortionParameters.correlationRingWidth, // ring (around r=0.5 dist to opposite corr) width
distortionParameters.contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
distortionParameters.contrastAverageSigma, // Gaussian sigma to average correlation variations (as contrast reference) 0.5
distortionParameters.contrastAverageSigma,
//TODO: verify that displacement is correct here (sign, direction)
centerXY[0], // x0, // center coordinates
centerXY[1], //y0,
"test-contrast"); // title base for optional plots names
contrast=contrasts[0];
result[2]=contrast;
if ((distortionParameters.correlationMinContrast>0) && (contrast<distortionParameters.correlationMinContrast)) {
if (debug_level>1) System.out.println("Contrast too low - "+contrast+"<"+distortionParameters.correlationMinContrast);
if (debug_level>1) System.out.println("Contrast "+IJ.d2s(contrast,3)+" ("+distortionParameters.correlationMinContrast+")"+
if ((distortionParameters.correlationMinContrast>0) && (contrasts[0]<distortionParameters.correlationMinContrast)) {
if (debug_level>1) System.out.println("Contrast too low - "+contrasts[0]+"<"+distortionParameters.correlationMinContrast);
if (debug_level>1) System.out.println("Contrast "+IJ.d2s(contrasts[0],3)+" ("+distortionParameters.correlationMinContrast+")"+
" is too low ("+IJ.d2s(beforeXY[0],3)+"/"+IJ.d2s(beforeXY[1],3)+")->"+
IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
return null;
}
if ((distortionParameters.correlationMinAbsoluteContrast>0) && (contrasts[1]<distortionParameters.correlationMinAbsoluteContrast)) {
if (debug_level>1) System.out.println("Absolute contrast too low - "+contrasts[1]+"<"+distortionParameters.correlationMinAbsoluteContrast);
if (debug_level>1) System.out.println("Absolute contrast "+IJ.d2s(contrasts[1],3)+" ("+distortionParameters.correlationMinAbsoluteContrast+")"+
" is too low ("+IJ.d2s(beforeXY[0],3)+"/"+IJ.d2s(beforeXY[1],3)+")->"+
IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
return null;
}
if (debug_level>2) System.out.println("Contarst="+contrast+" (legacy)");
}
if (debug_level>2) System.out.println("correctedPatternCrossLocation: Center x="+IJ.d2s(centerXY[0],3)+" y="+ IJ.d2s(centerXY[1],3));
......@@ -7820,7 +8001,7 @@ y=xy0[1] + dU*deltaUV[0]*(xy1[1]-xy0[1])+dV*deltaUV[1]*(xy2[1]-xy0[1])
System.out.println("correctedPatternCrossLocationAverage4(), beforeXY[0]="+beforeXY[0]+", beforeXY[1]="+beforeXY[1]);
debug_level+=3;
}
// System.out.println("correctedPatternCrossLocationAverage4(): beforeXY[0]="+beforeXY[0]+". beforeXY[1]="+beforeXY[1]);
// System.out.println("correctedPatternCrossLocationAverage4(): beforeXY[0]="+beforeXY[0]+". beforeXY[1]="+beforeXY[1]);
// Just for testing
beforeXY[0]+=distortionParameters.correlationDx; // offset, X (in pixels)
beforeXY[1]+=distortionParameters.correlationDy; // offset y (in pixels)
......@@ -7850,7 +8031,7 @@ y=xy0[1] + dU*deltaUV[0]*(xy1[1]-xy0[1])+dV*deltaUV[1]*(xy2[1]-xy0[1])
double [] dUV;
double[][] sim_pix;
double [] simGreensCentered;
// double [] modelCorr;
// double [] modelCorr;
double [] centerXY;
double contrast;
......@@ -7899,7 +8080,7 @@ y=xy0[1] + dU*deltaUV[0]*(xy1[1]-xy0[1])+dV*deltaUV[1]*(xy2[1]-xy0[1])
thisWindow=window4;
}
}
/*
/*
if ((min_span<uv_threshold) && (window2!=null)) { // trying to increase only twice
thisCorrelationSize*=2;
min_span*=2;
......@@ -7910,7 +8091,7 @@ y=xy0[1] + dU*deltaUV[0]*(xy1[1]-xy0[1])+dV*deltaUV[1]*(xy2[1]-xy0[1])
thisWindow=window4;
}
}
*/
*/
setCorrelationSizesUsed(thisCorrelationSize);
if ((debug_level>0)&&(thisCorrelationSize>distortionParameters.correlationSize)) System.out.println("**** u/v span too small, increasing FFT size to "+thisCorrelationSize);
Rectangle centerCross=correlationSelection(
......@@ -7927,7 +8108,7 @@ y=xy0[1] + dU*deltaUV[0]*(xy1[1]-xy0[1])+dV*deltaUV[1]*(xy2[1]-xy0[1])
if (debug_level>2) System.out.println("ixc="+ixc+" iyc="+iyc);
double [] greens=normalizeAndWindow (input_bayer[4], thisWindow);
if (debug_level>2) SDFA_INSTANCE.showArrays(greens, "greensWindowed");
// average is not zero - probably
// average is not zero - probably
if (debug_level>2) {
System.out.println(" wv0x="+IJ.d2s(wv0x,5)+" wv0y="+IJ.d2s(wv0y,5));
......@@ -8047,7 +8228,7 @@ y=xy0[1] + dU*deltaUV[0]*(xy1[1]-xy0[1])+dV*deltaUV[1]*(xy2[1]-xy0[1])
return null;
}
// debug_level=3;
// debug_level=3;
if (numNeib>1){
centerXY[0]*=0.5;
......@@ -8055,18 +8236,16 @@ y=xy0[1] + dU*deltaUV[0]*(xy1[1]-xy0[1])+dV*deltaUV[1]*(xy2[1]-xy0[1])
for (int i=0;i<2;i++) for (int j=0;j<2;j++) WVgreens[i][j]*=0.5;
}
contrast= correlationContrast(modelCorr,
/*
contrast= correlationContrast1(
modelCorr,
WVgreens, // wave vectors (same units as the pixels array)
// distortionParameters.correlationRingWidth, // ring (around r=0.5 dist to opposite corr) width
distortionParameters.contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
distortionParameters.contrastAverageSigma, // Gaussian sigma to average correlation variations (as contrast reference) 0.5
//TODO: verify that displacement is correct here (sign, direction)
centerXY[0], // x0, // center coordinates
centerXY[1], //y0,
"test-contrast", // title base for optional plots names
debug_level);
result[2]=contrast;
if ((distortionParameters.correlationMinContrast>0) && (contrast<distortionParameters.correlationMinContrast)) {
......@@ -8076,15 +8255,44 @@ y=xy0[1] + dU*deltaUV[0]*(xy1[1]-xy0[1])+dV*deltaUV[1]*(xy2[1]-xy0[1])
IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
return null;
}
if (debug_level>1) System.out.println(">>>Contarst="+contrast+" ("+IJ.d2s(beforeXY[0],3)+":"+IJ.d2s(beforeXY[1],3)+")->"+IJ.d2s(result[0],3)+":"+IJ.d2s(result[1],3));
*/
double [] contrasts= correlationContrast(
modelCorr,
greens,
WVgreens, // wave vectors (same units as the pixels array)
distortionParameters.contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
distortionParameters.contrastAverageSigma,
centerXY[0], // x0, // center coordinates
centerXY[1], //y0,
"test-contrast"); // title base for optional plots names
contrast=contrasts[0];
result[2]=contrast;
if ((distortionParameters.correlationMinContrast>0) && (contrasts[0]<distortionParameters.correlationMinContrast)) {
if (debug_level>1) System.out.println("Contrast too low - "+contrasts[0]+"<"+distortionParameters.correlationMinContrast);
if (debug_level>1) System.out.println("Contrast "+IJ.d2s(contrasts[0],3)+" ("+distortionParameters.correlationMinContrast+")"+
" is too low ("+IJ.d2s(beforeXY[0],3)+"/"+IJ.d2s(beforeXY[1],3)+")->"+
IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
return null;
}
if ((distortionParameters.correlationMinAbsoluteContrast>0) && (contrasts[1]<distortionParameters.correlationMinAbsoluteContrast)) {
if (debug_level>1) System.out.println("Absolute contrast too low - "+contrasts[1]+"<"+distortionParameters.correlationMinAbsoluteContrast);
if (debug_level>1) System.out.println("Absolute contrast "+IJ.d2s(contrasts[1],3)+" ("+distortionParameters.correlationMinAbsoluteContrast+")"+
" is too low ("+IJ.d2s(beforeXY[0],3)+"/"+IJ.d2s(beforeXY[1],3)+")->"+
IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
return null;
}
if (debug_level>1) System.out.println(">>>Contrast="+contrasts[0]+"/"+contrasts[1]+" ("+IJ.d2s(beforeXY[0],3)+":"+IJ.d2s(beforeXY[1],3)+")->"+IJ.d2s(result[0],3)+":"+IJ.d2s(result[1],3));
result[0]=ixc-(-centerXY[0]-centerXY[1])+diffBeforeXY[0];
result[1]=iyc-( centerXY[0]-centerXY[1])+diffBeforeXY[1];
if (debug_level>2) System.out.println(">---correctedPatternCrossLocation: before x="+IJ.d2s(beforeXY[0],3)+" y="+IJ.d2s(beforeXY[1],3));
if (debug_level>2) System.out.println(">+++correctedPatternCrossLocation: after x="+IJ.d2s(result[0],3)+" y="+IJ.d2s(result[1],3));
// if (debug_level>0) System.out.println(">---correctedPatternCrossLocation: before x="+IJ.d2s(beforeXY[0],3)+" y="+IJ.d2s(beforeXY[1],3));
// if (debug_level>0) System.out.println(">+++correctedPatternCrossLocation: after x="+IJ.d2s(result[0],3)+" y="+IJ.d2s(result[1],3));
// if (debug_level>0) System.out.println(">---correctedPatternCrossLocation: before x="+IJ.d2s(beforeXY[0],3)+" y="+IJ.d2s(beforeXY[1],3));
// if (debug_level>0) System.out.println(">+++correctedPatternCrossLocation: after x="+IJ.d2s(result[0],3)+" y="+IJ.d2s(result[1],3));
return result;
......@@ -9491,7 +9699,10 @@ error=Sum(W(x,y)*(F^2 + 2*F*(A*x^2+B*y^2+C*x*y+D*x+E*y-Z(x,y)) +(...) )
public double correlationMaxOffset; // maximal distance between predicted and actual pattern node
public double correlationMinContrast; // minimal contrast for the pattern to pass
public double correlationMinInitialContrast; // minimal contrast for the pattern of the center (initial point)
public double scaleFirstPassContrast; // Decrease contrast of cells that are too close to the border to be processed in rifinement pass
public double correlationMinAbsoluteContrast; // minimal contrast for the pattern to pass, does not compensate for low ligt
public double correlationMinAbsoluteInitialContrast; // minimal contrast for the pattern of the center (initial point)
public double scaleFirstPassContrast; // Decrease contrast of cells that are too close to the border to be processed in refinement pass
public double contrastSelectSigma; // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
public double contrastAverageSigma; // Gaussian sigma to average correlation variations (as contrast reference) 0.5
......@@ -9557,6 +9768,9 @@ error=Sum(W(x,y)*(F^2 + 2*F*(A*x^2+B*y^2+C*x*y+D*x+E*y-Z(x,y)) +(...) )
double correlationMaxOffset, // maximal distance between predicted and actual pattern node
double correlationMinContrast, // minimal contrast for the pattern to pass
double correlationMinInitialContrast, // minimal contrast for the pattern of the center (initial point)
double correlationMinAbsoluteContrast, // minimal contrast for the pattern to pass, does not compensate for low ligt
double correlationMinAbsoluteInitialContrast, // minimal contrast for the pattern of the center (initial point)
double scaleFirstPassContrast, // Decrease contrast of cells that are too close to the border to be processed in rifinement pass
double contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
double contrastAverageSigma, // Gaussian sigma to average correlation variations (as contrast reference) 0.5
......@@ -9616,6 +9830,8 @@ error=Sum(W(x,y)*(F^2 + 2*F*(A*x^2+B*y^2+C*x*y+D*x+E*y-Z(x,y)) +(...) )
this.correlationMaxOffset=correlationMaxOffset;
this.correlationMinContrast=correlationMinContrast;
this.correlationMinInitialContrast=correlationMinInitialContrast;
this.correlationMinAbsoluteContrast=correlationMinAbsoluteContrast; // minimal contrast for the pattern to pass, does not compensate for low ligt
this.correlationMinAbsoluteInitialContrast=correlationMinAbsoluteInitialContrast; // minimal contrast for the pattern of the center (initial point)
this.scaleFirstPassContrast=scaleFirstPassContrast; // Decrease contrast of cells that are too close to the border to be processed in rifinement pass
this.contrastSelectSigma=contrastSelectSigma; // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
this.contrastAverageSigma=contrastAverageSigma; // Gaussian sigma to average correlation variations (as contrast reference) 0.5
......@@ -9677,6 +9893,8 @@ error=Sum(W(x,y)*(F^2 + 2*F*(A*x^2+B*y^2+C*x*y+D*x+E*y-Z(x,y)) +(...) )
this.correlationMaxOffset, // maximal distance between predicted and actual pattern node
this.correlationMinContrast, // minimal contrast for the pattern to pass
this.correlationMinInitialContrast,
this.correlationMinAbsoluteContrast, // minimal contrast for the pattern to pass, does not compensate for low ligt
this.correlationMinAbsoluteInitialContrast, // minimal contrast for the pattern of the center (initial point)
this.scaleFirstPassContrast, // Decrease contrast of cells that are too close to the border to be processed in rifinement pass
this.contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
this.contrastAverageSigma, // Gaussian sigma to average correlation variations (as contrast reference) 0.5
......@@ -9738,6 +9956,8 @@ error=Sum(W(x,y)*(F^2 + 2*F*(A*x^2+B*y^2+C*x*y+D*x+E*y-Z(x,y)) +(...) )
properties.setProperty(prefix+"correlationMaxOffset",this.correlationMaxOffset+"");
properties.setProperty(prefix+"correlationMinContrast",this.correlationMinContrast+"");
properties.setProperty(prefix+"correlationMinInitialContrast",this.correlationMinInitialContrast+"");
properties.setProperty(prefix+"correlationMinAbsoluteContrast",this.correlationMinAbsoluteContrast+"");
properties.setProperty(prefix+"correlationMinAbsoluteInitialContrast",this.correlationMinAbsoluteInitialContrast+"");
properties.setProperty(prefix+"scaleFirstPassContrast",this.scaleFirstPassContrast+"");
properties.setProperty(prefix+"contrastSelectSigma",this.contrastSelectSigma+"");
properties.setProperty(prefix+"contrastAverageSigma",this.contrastAverageSigma+"");
......@@ -9814,6 +10034,11 @@ error=Sum(W(x,y)*(F^2 + 2*F*(A*x^2+B*y^2+C*x*y+D*x+E*y-Z(x,y)) +(...) )
if (properties.getProperty(prefix+"correlationMinInitialContrast")!=null)
this.correlationMinInitialContrast=Double.parseDouble(properties.getProperty(prefix+"correlationMinInitialContrast"));
if (properties.getProperty(prefix+"correlationMinAbsoluteContrast")!=null)
this.correlationMinAbsoluteContrast=Double.parseDouble(properties.getProperty(prefix+"correlationMinAbsoluteContrast"));
if (properties.getProperty(prefix+"correlationMinAbsoluteInitialContrast")!=null)
this.correlationMinAbsoluteInitialContrast=Double.parseDouble(properties.getProperty(prefix+"correlationMinAbsoluteInitialContrast"));
if (properties.getProperty(prefix+"scaleFirstPassContrast")!=null)
this.scaleFirstPassContrast=Double.parseDouble(properties.getProperty(prefix+"scaleFirstPassContrast"));
if (properties.getProperty(prefix+"contrastSelectSigma")!=null)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment