MatchSimulatedPattern.java 534 KB
Newer Older
Andrey Filippov's avatar
Andrey Filippov committed
1
package com.elphel.imagej.calibration;
Andrey Filippov's avatar
Andrey Filippov committed
2 3 4 5 6
/**
 **
 ** MatchSimulatedPattern.java - Determine simulation pattern parameters to match
 ** the acquired image
 **
7
 ** Copyright (C) 2010-2014 Elphel, Inc.
Andrey Filippov's avatar
Andrey Filippov committed
8 9
 **
 ** -----------------------------------------------------------------------------**
10
 **
Andrey Filippov's avatar
Andrey Filippov committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 **  MatchSimulatedPattern.java is free software: you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License as published by
 **  the Free Software Foundation, either version 3 of the License, or
 **  (at your option) any later version.
 **
 **  This program is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License
 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ** -----------------------------------------------------------------------------**
 **
 */

import java.awt.Rectangle;
import java.util.ArrayList;
29
import java.util.Arrays;
30
import java.util.Enumeration;
Andrey Filippov's avatar
Andrey Filippov committed
31 32
import java.util.List;
import java.util.Properties;
33 34
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
Andrey Filippov's avatar
Andrey Filippov committed
35 36 37 38 39
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import javax.swing.SwingUtilities;

Andrey Filippov's avatar
Andrey Filippov committed
40 41
import com.elphel.imagej.common.DoubleFHT;
import com.elphel.imagej.common.DoubleGaussianBlur;
42
import com.elphel.imagej.common.PolynomialApproximation;
43
import com.elphel.imagej.common.ShowDoubleFloatArrays;
Andrey Filippov's avatar
Andrey Filippov committed
44
import com.elphel.imagej.jp4.JP46_Reader_camera;
45
import com.elphel.imagej.lwir.LwirReaderParameters;
Andrey Filippov's avatar
Andrey Filippov committed
46

Andrey Filippov's avatar
Andrey Filippov committed
47 48 49 50 51 52 53 54 55 56 57 58
import Jama.LUDecomposition;
import Jama.Matrix;  // Download here: http://math.nist.gov/javanumerics/jama/
import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.gui.Roi;
import ij.process.FHT; // get rid, change to double
import ij.process.FloatProcessor;
import ij.process.ImageProcessor;


public class MatchSimulatedPattern {
59
	private ShowDoubleFloatArrays SDFA_INSTANCE= new ShowDoubleFloatArrays(); // just for debugging?
Andrey Filippov's avatar
Andrey Filippov committed
60 61 62
	public int debugLevel=2;
	public int FFT_SIZE=256;
	public double [][][][] PATTERN_GRID=null; // global to be used with threads? TODO: Same as DIST_ARRAY - merge?
63
	public int [] minUV={0,0};
64 65 66 67 68 69
	public int [][] reMap=null;               // maps grid coordinates from laser pointers to PATTERN_GRID u,v (2x3 - rotation + translation) - SEEMS NOT USED!
	public int [] UVShiftRot={0,0,0}; // {shift U, shift V, rot (1 of 8 - 4 non-mirrored, 4 - mirrored}
	public int [][][] targetUV=null; // maps PATTERN_GRID cells to the target (absolute) UV
	//    public double [][][] pixelsUV=null; // made of PATTERN_GRID, but does not have any wave vectors. Calculated during laser calibration
	public double [][][] pXYUV=null; // made of PATTERN_GRID, but does not have any wave vectors. Calculated during laser calibration
	public double [][][] gridContrastBrightness=null; //{grid contrast, grid intensity red, grid intensity green, grid intensity blue}[v][u]
Andrey Filippov's avatar
Andrey Filippov committed
70 71 72 73 74 75 76 77 78
	public Rectangle DIST_SELECTION=null;
	public int [] UV_INDEX=null; // array containing index of the pattern UV (scanline order, U first), or -1 for the areas with no pattern
	public int  UV_INDEX_WIDTH=0; // width of UV_INDEX (== full image, not selection, width)
	public int [] debugUV={-1,-1}; // debug the same cell on the second pass
	public int passNumber=0;
	public boolean [] correlationSizesUsed=null;
	public double [] gridFFCorr=null; // array matching greens with the flat field correction for the grid (zero outside of detected grid?)
	public double [] flatFieldForGrid=null; // array matching image pixels, divide the input pixels by these values (if not null)
	public boolean [] focusMask=null; // array matching image pixels, used with focusing (false outside sample areas)
79
	final private static int [][][] rotations={
80 81 82 83 84 85 86 87 88 89
			{{ 1, 0},{ 0, 1}}, // not mirrored
			{{ 0, 1},{-1, 0}},
			{{-1, 0},{ 0,-1}},
			{{ 0,-1},{ 1, 0}},

			{{ 1, 0},{ 0,-1}}, // mirrored
			{{ 0, 1},{ 1, 0}},
			{{-1, 0},{ 0, 1}},
			{{ 0,-1},{-1, 0}}};
	// shifts when rotating around unknown center (make it white)
90
	final private static int [][] dfltShifts={
91 92 93 94 95 96 97 98
			{0,0},
			{0,1},
			{0,0},
			{1,0},
			{0,1},
			{0,0},
			{1,0},
			{0,0}};
99
	final private static int [][] combinedRotations={
100 101 102 103 104 105 106 107 108
			{0,	1,	2,	3,	4,	5,	6,	7},
			{1,	2,	3,	0,	7,	4,	5,	6},
			{2,	3,	0,	1,	6,	7,	4,	5},
			{3,	0,	1,	2,	5,	6,	7,	4},
			{4,	5,	6,	7,	0,	1,	2,	3},
			{5,	6,	7,	4,	3,	0,	1,	2},
			{6,	7,	4,	5,	2,	3,	0,	1},
			{7,	4,	5,	6,	1,	2,	3,	0}};

109 110 111 112 113 114
	public MatchSimulatedPattern (){ }
	public MatchSimulatedPattern (int fft_size){
		this.FFT_SIZE=fft_size;
	}
	// not real clone, just for threads - if there will be FFT - keep individual
	@Override
115
	public MatchSimulatedPattern clone(){ // used in createPSFMap when creating threads
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		MatchSimulatedPattern msp=new MatchSimulatedPattern (this.FFT_SIZE);
		// cloning should be thread safe, when using DoubleFHT - use individual instances
		msp.debugLevel=this.debugLevel;
		msp.PATTERN_GRID=this.PATTERN_GRID; // global to be used with threads? TODO: Same as DIST_ARRAY - merge?
		//    	msp.DIST_ARRAY=this.DIST_ARRAY;
		msp.DIST_SELECTION=this.DIST_SELECTION;
		msp.UV_INDEX=this.UV_INDEX; // array containing index of the pattern UV (scanline order, U first), or -1 for the areas with no pattern
		msp.UV_INDEX_WIDTH=this.UV_INDEX_WIDTH;
		msp.reMap=this.reMap;
		msp.UVShiftRot=this.UVShiftRot.clone();
		//    public int [] UVShiftRot={0,0,0}; // {shift U, shift V, rot (1 of 8 - 4 non-mirrored, 4 - mirrored}

		msp.targetUV=this.targetUV; //
		msp.pXYUV=this.pXYUV;
		msp.flatFieldForGrid=this.flatFieldForGrid;
		msp.focusMask=this.focusMask;
		return msp;
	}
134

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	public MatchSimulatedPattern cloneDeep(
			boolean clonePATTERN_GRID,
			boolean cloneTargetUV,
			boolean clonePixelsUV,
			boolean cloneFlatFieldForGrid,
			boolean cloneFocusMask
			){ // used in createPSFMap when creating threads
		MatchSimulatedPattern msp=new MatchSimulatedPattern (this.FFT_SIZE);
		// cloning should be thread safe, when using DoubleFHT - use individual instances
		msp.debugLevel=this.debugLevel;
		//    	msp.PATTERN_GRID=this.PATTERN_GRID; // global to be used with threads? TODO: Same as DIST_ARRAY - merge?
		if ( clonePATTERN_GRID && (msp.PATTERN_GRID!=null)){
			msp.PATTERN_GRID=new double [this.PATTERN_GRID.length][this.PATTERN_GRID[0].length][][];
			for (int i=0;i<this.PATTERN_GRID.length;i++) for (int j=0;j<this.PATTERN_GRID[i].length;j++) {
				if (this.PATTERN_GRID[i][j]!=null){
					msp.PATTERN_GRID[i][j]=new double [this.PATTERN_GRID[i][j].length][];
					for (int k=0;k<this.PATTERN_GRID[i][j].length;k++){
						if (this.PATTERN_GRID[i][j][k]!=null) msp.PATTERN_GRID[i][j][k]=this.PATTERN_GRID[i][j][k].clone();
						else msp.PATTERN_GRID[i][j][k]=null;
					}
				} else msp.PATTERN_GRID[i][j]=null;
			}
		} else msp.PATTERN_GRID=this.PATTERN_GRID;
		//    	msp.DIST_ARRAY=this.DIST_ARRAY;
		if (this.DIST_SELECTION!=null) msp.DIST_SELECTION=new Rectangle(this.DIST_SELECTION);
		else msp.DIST_SELECTION=null;

		if (this.UV_INDEX!=null) msp.UV_INDEX=this.UV_INDEX.clone(); // array containing index of the pattern UV (scanline order, U first), or -1 for the areas with no pattern
		else msp.UV_INDEX=null;

		msp.UV_INDEX_WIDTH=this.UV_INDEX_WIDTH;

		if (this.reMap!=null) { // probably not used
			msp.reMap=new int [2][];
			msp.reMap[0]=this.reMap[0].clone();
			msp.reMap[1]=this.reMap[1].clone();
		} else msp.reMap=null;

		msp.UVShiftRot=this.UVShiftRot.clone();
		//     public int [] UVShiftRot={0,0,0}; // {shift U, shift V, rot (1 of 8 - 4 non-mirrored, 4 - mirrored}
		//    	msp.targetUV=this.targetUV;
		if (cloneTargetUV && (this.targetUV != null)) {
			msp.targetUV=new int [this.targetUV.length][this.targetUV[0].length][];
			for (int i=0;i<this.targetUV.length;i++) for (int j=0;j<this.targetUV[i].length;j++){
				if (this.targetUV[i][j]!=null) msp.targetUV[i][j]=this.targetUV[i][j].clone();
				else msp.targetUV[i][j]=null;
			}
		} else msp.targetUV=this.targetUV;
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
		//    	msp.pixelsUV=this.pixelsUV;

		if (clonePixelsUV && (this.pXYUV != null)) {
			msp.pXYUV=new double [this.pXYUV.length][this.pXYUV[0].length][];
			for (int i=0;i<this.pXYUV.length;i++) for (int j=0;j<this.pXYUV[i].length;j++){
				if (this.pXYUV[i][j]!=null) msp.pXYUV[i][j]=this.pXYUV[i][j].clone();
				else msp.pXYUV[i][j]=null;
			}
		} else msp.targetUV=this.targetUV;

		//    	msp.flatFieldForGrid=this.flatFieldForGrid;
		if (cloneFlatFieldForGrid && (this.flatFieldForGrid!=null)) msp.flatFieldForGrid=this.flatFieldForGrid.clone();
		else msp.flatFieldForGrid=this.flatFieldForGrid;

		//		msp.focusMask=this.focusMask;
		if (cloneFocusMask && (this.focusMask!=null)) msp.focusMask=this.focusMask.clone();
		else msp.focusMask=this.focusMask;
		return msp;
	}

	public int [] getUVShiftRot(boolean shift){
		if (!shift) return this.UVShiftRot;
		int [][] reReMap=getRemapMatrix(this.UVShiftRot);
		int [] UVShiftRotCorr=this.UVShiftRot.clone();
		UVShiftRotCorr[0]-=reReMap[0][0]*this.minUV[0]+reReMap[0][1]*this.minUV[1];
		UVShiftRotCorr[1]-=reReMap[1][0]*this.minUV[0]+reReMap[1][1]*this.minUV[1];
		System.out.println("getUVShiftRot(true): minUV[0]="+minUV[0]+" minUV[1]="+minUV[1]);
		return UVShiftRotCorr;
	}
213

214
	public static int [][] getRemapMatrix(
215 216 217 218 219 220 221
			int [] UVShiftRot){
		// Moved shift calculation to calibrateGrid(), here just a regular R,T 2x3 matix
		int [][] reReMap={
				{rotations[UVShiftRot[2]][0][0],rotations[UVShiftRot[2]][0][1], UVShiftRot[0]},
				{rotations[UVShiftRot[2]][1][0],rotations[UVShiftRot[2]][1][1], UVShiftRot[1]}};
		return reReMap;
	}
222

223
	public static int [] combineUVShiftRot(
224 225 226 227 228 229 230 231 232 233
			int [] UVShiftRotA,
			int [] UVShiftRotB){
		int [][] reReMapA=getRemapMatrix(UVShiftRotA);
		int [][] reReMapB=getRemapMatrix(UVShiftRotB);
		int [] UVShiftRot=new int [3];
		UVShiftRot[0] = reReMapB[0][0]*reReMapA[0][2]+ reReMapB[0][1]*reReMapA[1][2] + reReMapB[0][2];
		UVShiftRot[1] = reReMapB[1][0]*reReMapA[0][2]+ reReMapB[1][1]*reReMapA[1][2] + reReMapB[1][2];
		UVShiftRot[2] = combinedRotations[UVShiftRotA[2]][UVShiftRotB[2]];
		return UVShiftRot;
	}
Andrey Filippov's avatar
Andrey Filippov committed
234

235 236 237 238 239 240 241 242 243 244 245 246 247
	public double focusQualityR2(
			ImagePlus imp,
			int fftSize,
			int spotSize, //5
			double x0,
			double y0,
			int debugLevel
			){
		int ix0=(int) Math.round(x0);
		int iy0=(int) Math.round(y0);
		Rectangle selection=new Rectangle(ix0-fftSize,iy0-fftSize,fftSize*2,fftSize*2);
		double [][] pixels=splitBayer(imp, selection,true);
		//    	SDFA_INSTANCE.showArrays(pixels, fftSize, fftSize, true,"bayer");
Andrey Filippov's avatar
Andrey Filippov committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
		DoubleFHT fht_instance =new DoubleFHT(); // provide DoubleFHT instance to save on initializations (or null)
		double [] hamming1d=fht_instance.getHamming1d(fftSize);
		for (int c=0;c<pixels.length;c++) if (pixels[c]!=null){
			double sum=0.0;
			for (int i=0;i<pixels[c].length;i++) sum+=pixels[c][i];
			double average=sum/pixels[c].length;
			int index=0;
			for (int y=0;y<fftSize;y++)for (int x=0;x<fftSize;x++){
				pixels[c][index]=(pixels[c][index]-average)*hamming1d[y]*hamming1d[x];
				index++;
			}
		}
		// slightly blur the image to be sure there are no aliases in the low frequencies
		double preBlurSigma=1.5;
		DoubleGaussianBlur gb=new DoubleGaussianBlur();
		for (int c=0;c<pixels.length;c++) if (pixels[c]!=null){
			gb.blurDouble(
					pixels[c],
					fftSize,
					fftSize,
					preBlurSigma,
					preBlurSigma,
					0.01);

272

Andrey Filippov's avatar
Andrey Filippov committed
273
		}
274
		//    	SDFA_INSTANCE.showArrays(pixels, fftSize, fftSize, true,"bayer-winowed");
Andrey Filippov's avatar
Andrey Filippov committed
275 276 277 278 279 280
		for (int c=0;c<pixels.length;c++) if (pixels[c]!=null){
			fht_instance.swapQuadrants(pixels[c]);
			fht_instance.transform(pixels[c]);
			pixels[c]=fht_instance.calculateAmplitude(pixels[c]);
		}
		if (debugLevel>1) SDFA_INSTANCE.showArrays(pixels, fftSize, fftSize, true,"amplitudes");
281 282 283 284 285 286 287 288 289 290 291 292 293
		int [][][] spectrumMaximums=new int [pixels.length][][];
		double [] spectralContrast=new double [pixels.length];
		int hsize=fftSize/2;
		int radius=spotSize/2; // 2
		int [][]dirs={
				{-1, 0},
				{-1, 1},
				{ 0, 1},
				{ 1, 1},
				{ 1, 0},
				{ 1,-1},
				{ 0,-1},
				{-1,-1}};
Andrey Filippov's avatar
Andrey Filippov committed
294 295
		int lowLim= (fftSize*3)/8;
		int highLim=(fftSize*5)/8;
296 297 298
		for (int c=0;c<pixels.length;c++) if (pixels[c]!=null){
			spectrumMaximums[c]=new int [2][2];
			double max=0.0;
Andrey Filippov's avatar
Andrey Filippov committed
299 300 301 302 303 304 305 306
			for (int y=lowLim;y<=fftSize/2;y++)for (int x=lowLim;x<highLim;x++){
				if ((y>=(hsize-radius)) && (x>=(hsize-radius)) && (x<=(hsize+radius))) continue; // do not count zero freq
				if (max<pixels[c][y*fftSize+x]) {
					max=pixels[c][y*fftSize+x];
					spectrumMaximums[c][0][0]=x;
					spectrumMaximums[c][0][1]=y;
				}
			}
307
			max=0.0;
Andrey Filippov's avatar
Andrey Filippov committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
			for (int y=lowLim;y<=fftSize/2;y++)for (int x=lowLim;x<highLim;x++){
				if ((y>=(hsize-radius)) && (x>=(hsize-radius)) && (x<=(hsize+radius))) {
					if ((debugLevel>1) && (c==0))System.out.println("x]="+x+" y="+y+" - too close to 0,0");
					continue; // do not count zero freq
				}
				if ((y>=(spectrumMaximums[c][0][1]-radius)) && (y<=(spectrumMaximums[c][0][1]+radius)) &&
						(x>=(spectrumMaximums[c][0][0]-radius)) && (x<=(spectrumMaximums[c][0][0]+radius))){
					if ((debugLevel>1) && (c==0))System.out.println("x="+x+" y="+y+" - too close to first max "+spectrumMaximums[c][0][0]+":"+spectrumMaximums[c][0][1]);
					continue; // do not count zero freq
				}
				if ((y>=((fftSize-spectrumMaximums[c][0][1])-radius)) && (y<=((fftSize-spectrumMaximums[c][0][1])+radius)) &&
						(x>=((fftSize-spectrumMaximums[c][0][0])-radius)) && (x<=((fftSize-spectrumMaximums[c][0][0])+radius))){
					if ((debugLevel>1) && (c==0))System.out.println("x="+x+" y="+y+" - too close to alias "+(fftSize-spectrumMaximums[c][0][0])+":"+(fftSize-spectrumMaximums[c][0][1]));
					continue; // do not count zero first maximum
				}
				if (max<pixels[c][y*fftSize+x]) {
					max=pixels[c][y*fftSize+x];
					spectrumMaximums[c][1][0]=x;
					spectrumMaximums[c][1][1]=y;
					if ((debugLevel>1) && (c==0))System.out.println("New max at x="+x+" y="+y+": "+max);
				}
			}
			if (debugLevel>1) System.out.println("spectrumMaximums["+c+"]="+(spectrumMaximums[c][0][0]-hsize)+":"+(spectrumMaximums[c][0][1]-hsize)+", "+
331 332
					""+(spectrumMaximums[c][1][0]-hsize)+":"+(spectrumMaximums[c][1][1]-hsize));
			//			double s1=0,s2=0;
Andrey Filippov's avatar
Andrey Filippov committed
333 334 335
			int [][] xy3={
					{3*spectrumMaximums[c][0][0]-fftSize,3*spectrumMaximums[c][0][1]-fftSize},
					{3*spectrumMaximums[c][1][0]-fftSize,3*spectrumMaximums[c][1][1]-fftSize}};
336
			//			if (debugLevel>3){
337
			if ((c==0) && (debugLevel>1)){
338 339
				System.out.println(" xy3[0][0]="+xy3[0][0]+" xy3[0][1]="+xy3[0][1]);
				System.out.println(" xy3[1][0]="+xy3[1][0]+" xy3[1][1]="+xy3[1][1]);
Andrey Filippov's avatar
Andrey Filippov committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
			}
			// make 3-rd  harmonic frequency adjustment
			// May be improved by quadratic maximums
			for (int n=0;n<2;n++) for (int i=0;i<2;i++){
				max=pixels[c][xy3[i][1]*fftSize+xy3[i][0]];
				int d=-1;
				for (int dir=0;dir<dirs.length;dir++) {
					double v=pixels[c][(xy3[i][1]+dirs[dir][1])*fftSize+(xy3[i][0]+dirs[dir][0])];
					if (max<v){
						max=v;
						d=dir;
					}
				}
				if (d>=0) {
					xy3[i][0]+=dirs[d][0];
					xy3[i][1]+=dirs[d][1];
				}
			}
358
			//			if (debugLevel>2) {
359
			if ((c==0) && (debugLevel>1)) {
Andrey Filippov's avatar
Andrey Filippov committed
360 361 362 363 364 365
				System.out.println("*xy3[0][0]="+xy3[0][0]+" xy3[0][1]="+xy3[0][1]);
				System.out.println("*xy3[1][0]="+xy3[1][0]+" xy3[1][1]="+xy3[1][1]);
			}
			double [][] xy={
					{(xy3[0][0]-hsize)/3.0,(xy3[0][1]-hsize)/3.0},
					{(xy3[1][0]-hsize)/3.0,(xy3[1][1]-hsize)/3.0}};
366
			if ((c==0) && (debugLevel>1)) {
Andrey Filippov's avatar
Andrey Filippov committed
367 368 369
				System.out.println(" xy[0][0]="+IJ.d2s(xy[0][0],1)+" xy3[0][1]="+IJ.d2s(xy[0][1],1));
				System.out.println(" xy[1][0]="+IJ.d2s(xy[1][0],1)+" xy3[1][1]="+IJ.d2s(xy[1][1],1));
			}
370
			// once more - correct 5-th:
Andrey Filippov's avatar
Andrey Filippov committed
371 372 373
			int [][] xy5={
					{hsize+ (int) Math.round(5*xy[0][0]),hsize+ (int) Math.round(5*xy[0][1])},
					{hsize+ (int) Math.round(5*xy[1][0]),hsize+ (int) Math.round(5*xy[1][1])}};
374
			// just once
Andrey Filippov's avatar
Andrey Filippov committed
375

376
			if ((c==0) && (debugLevel>1)) {
Andrey Filippov's avatar
Andrey Filippov committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
				System.out.println(" xy5[0][0]="+xy5[0][0]+" xy5[0][1]="+xy5[0][1]);
				System.out.println(" xy5[1][0]="+xy5[1][0]+" xy5[1][1]="+xy5[1][1]);
			}
			for (int i=0;i<2;i++){
				max=pixels[c][xy5[i][1]*fftSize+xy5[i][0]];
				int d=-1;
				for (int dir=0;dir<dirs.length;dir++) {
					double v=pixels[c][(xy5[i][1]+dirs[dir][1])*fftSize+(xy5[i][0]+dirs[dir][0])];
					if (max<v){
						max=v;
						d=dir;
					}
				}
				if (d>=0) {
					xy5[i][0]+=dirs[d][0];
					xy5[i][1]+=dirs[d][1];
				}
			}
395
			if ((c==0) && (debugLevel>1)) {
Andrey Filippov's avatar
Andrey Filippov committed
396 397 398 399
				System.out.println("*xy5[0][0]="+xy5[0][0]+" xy5[0][1]="+xy5[0][1]);
				System.out.println("*xy5[1][0]="+xy5[1][0]+" xy5[1][1]="+xy5[1][1]);
			}
			for (int i=0;i<2;i++) for (int j=0;j<2;j++) xy[i][j]=(xy5[i][j]-hsize)/5.0;
400
			if ((c==0) && (debugLevel>1)) {
Andrey Filippov's avatar
Andrey Filippov committed
401 402 403
				System.out.println(" xy[0][0]="+IJ.d2s(xy[0][0],1)+" xy3[0][1]="+IJ.d2s(xy[0][1],1));
				System.out.println(" xy[1][0]="+IJ.d2s(xy[1][0],1)+" xy3[1][1]="+IJ.d2s(xy[1][1],1));
			}
404 405


406
			// now generate mask and then blur it
Andrey Filippov's avatar
Andrey Filippov committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
			int maxMode=7; // 1, 3-rd and 5-th harmonics, should be odd (now 7-th included)
			double [][] dirNeg={{-0.5,-0.5},{0.5,-0.5},{-0.5,0.5},{0.5,0.5}}; // direction to negative mask
			double [] mask = new double [fftSize*fftSize];
			for (int i=0;i<mask.length;i++) mask[i]=0.0;
			for (int i=-maxMode;i<=maxMode;i+=2) for (int j=-maxMode;j<=maxMode;j+=2){
				double xpc=-0.5*(xy[0][0]*(i+j)+xy[1][0]*(i-j));
				double ypc=-0.5*(xy[0][1]*(i+j)+xy[1][1]*(i-j));
				double xp=xpc+hsize;
				double yp=ypc+hsize;
				double w=xpc*xpc+ypc*ypc; // increase weight of high frequencies
				int ixp=(int) Math.round(xp);
				int iyp=(int) Math.round(yp);
				if ((ixp<0) ||(ixp>=fftSize) || (iyp<0) ||(iyp>=fftSize)) continue;
				mask[iyp*fftSize+ixp]+=w;
				if ((c==0) && (debugLevel>1)) 	System.out.println(" xp="+IJ.d2s(xp,1)+" xm="+IJ.d2s(yp,1));
				for (int d=0;d<dirNeg.length;d++){
					double xm=xp+dirNeg[d][0]*xy[0][0]+dirNeg[d][1]*xy[1][0];
					double ym=yp+dirNeg[d][0]*xy[0][1]+dirNeg[d][1]*xy[1][1];
					int ixm=(int) Math.round(xm);
					int iym=(int) Math.round(ym);
					ixm=(ixm+fftSize)%fftSize;
					iym=(iym+fftSize)%fftSize;
					mask[iym*fftSize+ixm]-=w/dirNeg.length;
				}
			}
			double sigmaScale=0.2;
			double averageLength=Math.sqrt((xy[0][0]*xy[0][0] + xy[0][1]*xy[0][1] +xy[1][0]*xy[1][0] +xy[1][1]*xy[1][1])/2);
			if ((c==0) && (debugLevel>1)) SDFA_INSTANCE.showArrays(mask, fftSize, fftSize, "mask_color");
435 436 437 438 439 440 441 442
			gb.blurDouble(
					mask,
					fftSize,
					fftSize,
					sigmaScale*averageLength,
					sigmaScale*averageLength,
					0.01);
			if ((c==0) && (debugLevel>1)) SDFA_INSTANCE.showArrays(mask, fftSize, fftSize, "mask_color_blured"+IJ.d2s(sigmaScale*averageLength,3));
Andrey Filippov's avatar
Andrey Filippov committed
443 444 445 446 447 448 449 450 451 452
			double SFR2=0.0,SFP=0.0;
			for (int i=0;i<mask.length;i++){
				int x=(i % fftSize)-hsize;
				int y=(i / fftSize)-hsize;
				SFR2+=pixels[c][i]*mask[i]*(x*x+y*y);
				if (mask[i]>0) SFP+=pixels[c][i]*mask[i]; // sum only positive masks? Or abs value? Does not really matter, it is just a scale
			}
			spectralContrast[c]=Math.sqrt(SFR2/SFP)/averageLength;
			if ((c==0) && (debugLevel>1)) System.out.println("SFR2="+SFR2+" SFP="+SFP+" averageLength="+averageLength);
			if ( (debugLevel>1)) System.out.println("spectrumContrast["+c+"]="+spectralContrast[c]);
453

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
		} else{
			spectrumMaximums[c]=null;
			spectralContrast[c]=Double.NaN;
		}
		// 	    return 0.25*(spectralContrast[0]+spectralContrast[1]+spectralContrast[2]+spectralContrast[3]);
		return 0.5*(spectralContrast[0]+spectralContrast[3]); // green only
	}
	public double focusQuality(
			ImagePlus imp,
			int fftSize,
			int spotSize, //5
			double x0,
			double y0,
			int debugLevel
			){
		int ix0=(int) Math.round(x0);
		int iy0=(int) Math.round(y0);
		Rectangle selection=new Rectangle(ix0-fftSize,iy0-fftSize,fftSize*2,fftSize*2);
		double [][] pixels=splitBayer(imp, selection,true);
		//    	SDFA_INSTANCE.showArrays(pixels, fftSize, fftSize, true,"bayer");
Andrey Filippov's avatar
Andrey Filippov committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
		DoubleFHT fht_instance =new DoubleFHT(); // provide DoubleFHT instance to save on initializations (or null)
		double [] hamming1d=fht_instance.getHamming1d(fftSize);
		for (int c=0;c<pixels.length;c++) if (pixels[c]!=null){
			double sum=0.0;
			for (int i=0;i<pixels[c].length;i++) sum+=pixels[c][i];
			double average=sum/pixels[c].length;
			int index=0;
			for (int y=0;y<fftSize;y++)for (int x=0;x<fftSize;x++){
				pixels[c][index]=(pixels[c][index]-average)*hamming1d[y]*hamming1d[x];
				index++;
			}
		}
		// slightly blur the image to be sure there are no aliases in the low frequencies
		double preBlurSigma=1.5;
		DoubleGaussianBlur gb=new DoubleGaussianBlur();
		for (int c=0;c<pixels.length;c++) if (pixels[c]!=null){
			gb.blurDouble(
					pixels[c],
					fftSize,
					fftSize,
					preBlurSigma,
					preBlurSigma,
					0.01);

498

Andrey Filippov's avatar
Andrey Filippov committed
499
		}
500
		//    	SDFA_INSTANCE.showArrays(pixels, fftSize, fftSize, true,"bayer-winowed");
Andrey Filippov's avatar
Andrey Filippov committed
501 502 503 504 505 506
		for (int c=0;c<pixels.length;c++) if (pixels[c]!=null){
			fht_instance.swapQuadrants(pixels[c]);
			fht_instance.transform(pixels[c]);
			pixels[c]=fht_instance.calculateAmplitude(pixels[c]);
		}
		if (debugLevel>1) SDFA_INSTANCE.showArrays(pixels, fftSize, fftSize, true,"amplitudes");
507 508 509 510 511 512 513 514 515 516 517 518 519
		int [][][] spectrumMaximums=new int [pixels.length][][];
		double [] spectralContrast=new double [pixels.length];
		int hsize=fftSize/2;
		int radius=spotSize/2; // 2
		int [][]dirs={
				{-1, 0},
				{-1, 1},
				{ 0, 1},
				{ 1, 1},
				{ 1, 0},
				{ 1,-1},
				{ 0,-1},
				{-1,-1}};
Andrey Filippov's avatar
Andrey Filippov committed
520 521
		int lowLim= (fftSize*3)/8;
		int highLim=(fftSize*5)/8;
522 523 524
		for (int c=0;c<pixels.length;c++) if (pixels[c]!=null){
			spectrumMaximums[c]=new int [2][2];
			double max=0.0;
Andrey Filippov's avatar
Andrey Filippov committed
525 526 527 528 529 530 531 532
			for (int y=lowLim;y<=fftSize/2;y++)for (int x=lowLim;x<highLim;x++){
				if ((y>=(hsize-radius)) && (x>=(hsize-radius)) && (x<=(hsize+radius))) continue; // do not count zero freq
				if (max<pixels[c][y*fftSize+x]) {
					max=pixels[c][y*fftSize+x];
					spectrumMaximums[c][0][0]=x;
					spectrumMaximums[c][0][1]=y;
				}
			}
533
			max=0.0;
Andrey Filippov's avatar
Andrey Filippov committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
			for (int y=lowLim;y<=fftSize/2;y++)for (int x=lowLim;x<highLim;x++){
				if ((y>=(hsize-radius)) && (x>=(hsize-radius)) && (x<=(hsize+radius))) {
					if ((debugLevel>1) && (c==0))System.out.println("x]="+x+" y="+y+" - too close to 0,0");
					continue; // do not count zero freq
				}
				if ((y>=(spectrumMaximums[c][0][1]-radius)) && (y<=(spectrumMaximums[c][0][1]+radius)) &&
						(x>=(spectrumMaximums[c][0][0]-radius)) && (x<=(spectrumMaximums[c][0][0]+radius))){
					if ((debugLevel>1) && (c==0))System.out.println("x="+x+" y="+y+" - too close to first max "+spectrumMaximums[c][0][0]+":"+spectrumMaximums[c][0][1]);
					continue; // do not count zero freq
				}
				if ((y>=((fftSize-spectrumMaximums[c][0][1])-radius)) && (y<=((fftSize-spectrumMaximums[c][0][1])+radius)) &&
						(x>=((fftSize-spectrumMaximums[c][0][0])-radius)) && (x<=((fftSize-spectrumMaximums[c][0][0])+radius))){
					if ((debugLevel>1) && (c==0))System.out.println("x="+x+" y="+y+" - too close to alias "+(fftSize-spectrumMaximums[c][0][0])+":"+(fftSize-spectrumMaximums[c][0][1]));
					continue; // do not count zero first maximum
				}
				if (max<pixels[c][y*fftSize+x]) {
					max=pixels[c][y*fftSize+x];
					spectrumMaximums[c][1][0]=x;
					spectrumMaximums[c][1][1]=y;
					if ((debugLevel>1) && (c==0))System.out.println("New max at x="+x+" y="+y+": "+max);
				}
			}
			if (debugLevel>1) System.out.println("spectrumMaximums["+c+"]="+(spectrumMaximums[c][0][0]-hsize)+":"+(spectrumMaximums[c][0][1]-hsize)+", "+
557 558
					""+(spectrumMaximums[c][1][0]-hsize)+":"+(spectrumMaximums[c][1][1]-hsize));
			//			double s1=0,s2=0;
Andrey Filippov's avatar
Andrey Filippov committed
559 560 561
			int [][] xy3={
					{3*spectrumMaximums[c][0][0]-fftSize,3*spectrumMaximums[c][0][1]-fftSize},
					{3*spectrumMaximums[c][1][0]-fftSize,3*spectrumMaximums[c][1][1]-fftSize}};
562
			//			if (debugLevel>3){
563
			if ((c==0) && (debugLevel>1)){
564 565
				System.out.println(" xy3[0][0]="+xy3[0][0]+" xy3[0][1]="+xy3[0][1]);
				System.out.println(" xy3[1][0]="+xy3[1][0]+" xy3[1][1]="+xy3[1][1]);
Andrey Filippov's avatar
Andrey Filippov committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
			}
			// make 3-rd  harmonic frequency adjustment
			// May be improved by quadratic maximums
			for (int n=0;n<2;n++) for (int i=0;i<2;i++){
				max=pixels[c][xy3[i][1]*fftSize+xy3[i][0]];
				int d=-1;
				for (int dir=0;dir<dirs.length;dir++) {
					double v=pixels[c][(xy3[i][1]+dirs[dir][1])*fftSize+(xy3[i][0]+dirs[dir][0])];
					if (max<v){
						max=v;
						d=dir;
					}
				}
				if (d>=0) {
					xy3[i][0]+=dirs[d][0];
					xy3[i][1]+=dirs[d][1];
				}
			}
584
			//			if (debugLevel>2) {
585
			if ((c==0) && (debugLevel>1)) {
Andrey Filippov's avatar
Andrey Filippov committed
586 587 588 589 590 591
				System.out.println("*xy3[0][0]="+xy3[0][0]+" xy3[0][1]="+xy3[0][1]);
				System.out.println("*xy3[1][0]="+xy3[1][0]+" xy3[1][1]="+xy3[1][1]);
			}
			double [][] xy={
					{(xy3[0][0]-hsize)/3.0,(xy3[0][1]-hsize)/3.0},
					{(xy3[1][0]-hsize)/3.0,(xy3[1][1]-hsize)/3.0}};
592
			if ((c==0) && (debugLevel>1)) {
Andrey Filippov's avatar
Andrey Filippov committed
593 594 595
				System.out.println(" xy[0][0]="+IJ.d2s(xy[0][0],1)+" xy3[0][1]="+IJ.d2s(xy[0][1],1));
				System.out.println(" xy[1][0]="+IJ.d2s(xy[1][0],1)+" xy3[1][1]="+IJ.d2s(xy[1][1],1));
			}
596
			// once more - correct 5-th:
Andrey Filippov's avatar
Andrey Filippov committed
597 598 599
			int [][] xy5={
					{hsize+ (int) Math.round(5*xy[0][0]),hsize+ (int) Math.round(5*xy[0][1])},
					{hsize+ (int) Math.round(5*xy[1][0]),hsize+ (int) Math.round(5*xy[1][1])}};
600
			// just once
Andrey Filippov's avatar
Andrey Filippov committed
601

602
			if ((c==0) && (debugLevel>1)) {
Andrey Filippov's avatar
Andrey Filippov committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
				System.out.println(" xy5[0][0]="+xy5[0][0]+" xy5[0][1]="+xy5[0][1]);
				System.out.println(" xy5[1][0]="+xy5[1][0]+" xy5[1][1]="+xy5[1][1]);
			}
			for (int i=0;i<2;i++){
				max=pixels[c][xy5[i][1]*fftSize+xy5[i][0]];
				int d=-1;
				for (int dir=0;dir<dirs.length;dir++) {
					double v=pixels[c][(xy5[i][1]+dirs[dir][1])*fftSize+(xy5[i][0]+dirs[dir][0])];
					if (max<v){
						max=v;
						d=dir;
					}
				}
				if (d>=0) {
					xy5[i][0]+=dirs[d][0];
					xy5[i][1]+=dirs[d][1];
				}
			}
621
			if ((c==0) && (debugLevel>1)) {
Andrey Filippov's avatar
Andrey Filippov committed
622 623 624 625
				System.out.println("*xy5[0][0]="+xy5[0][0]+" xy5[0][1]="+xy5[0][1]);
				System.out.println("*xy5[1][0]="+xy5[1][0]+" xy5[1][1]="+xy5[1][1]);
			}
			for (int i=0;i<2;i++) for (int j=0;j<2;j++) xy[i][j]=(xy5[i][j]-hsize)/5.0;
626
			if ((c==0) && (debugLevel>1)) {
Andrey Filippov's avatar
Andrey Filippov committed
627 628 629
				System.out.println(" xy[0][0]="+IJ.d2s(xy[0][0],1)+" xy3[0][1]="+IJ.d2s(xy[0][1],1));
				System.out.println(" xy[1][0]="+IJ.d2s(xy[1][0],1)+" xy3[1][1]="+IJ.d2s(xy[1][1],1));
			}
630 631


632
			// now generate mask and then blur it
Andrey Filippov's avatar
Andrey Filippov committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
			int maxMode=7; // 1, 3-rd and 5-th harmonics, should be odd (now 7-th included)
			double [][] dirNeg={{-0.5,-0.5},{0.5,-0.5},{-0.5,0.5},{0.5,0.5}}; // direction to negative mask
			double [] mask = new double [fftSize*fftSize];
			for (int i=0;i<mask.length;i++) mask[i]=0.0;
			for (int i=-maxMode;i<=maxMode;i+=2) for (int j=-maxMode;j<=maxMode;j+=2){
				double xpc=-0.5*(xy[0][0]*(i+j)+xy[1][0]*(i-j));
				double ypc=-0.5*(xy[0][1]*(i+j)+xy[1][1]*(i-j));
				double xp=xpc+hsize;
				double yp=ypc+hsize;
				double w=xpc*xpc+ypc*ypc; // increase weight of high frequencies
				w*=w; // even sharper
				int ixp=(int) Math.round(xp);
				int iyp=(int) Math.round(yp);
				if ((ixp<0) ||(ixp>=fftSize) || (iyp<0) ||(iyp>=fftSize)) continue;
				mask[iyp*fftSize+ixp]+=w;
				if ((c==0) && (debugLevel>1)) 	System.out.println(" xp="+IJ.d2s(xp,1)+" xm="+IJ.d2s(yp,1));
				for (int d=0;d<dirNeg.length;d++){
					double xm=xp+dirNeg[d][0]*xy[0][0]+dirNeg[d][1]*xy[1][0];
					double ym=yp+dirNeg[d][0]*xy[0][1]+dirNeg[d][1]*xy[1][1];
					int ixm=(int) Math.round(xm);
					int iym=(int) Math.round(ym);
					ixm=(ixm+fftSize)%fftSize;
					iym=(iym+fftSize)%fftSize;
					mask[iym*fftSize+ixm]-=w/dirNeg.length;
				}
			}
			double sigmaScale=0.2;
			double averageLength=Math.sqrt((xy[0][0]*xy[0][0] + xy[0][1]*xy[0][1] +xy[1][0]*xy[1][0] +xy[1][1]*xy[1][1])/2);
			if ((c==0) && (debugLevel>1)) SDFA_INSTANCE.showArrays(mask, fftSize, fftSize, "mask_color");
662 663 664 665 666 667 668 669 670 671 672 673 674
			gb.blurDouble(
					mask,
					fftSize,
					fftSize,
					sigmaScale*averageLength,
					sigmaScale*averageLength,
					0.01);
			if ((c==0) && (debugLevel>1)){
				SDFA_INSTANCE.showArrays(mask, fftSize, fftSize, "mask_color_blured"+IJ.d2s(sigmaScale*averageLength,3));
				double [] ppixels=new double [fftSize*fftSize];
				for (int i=0;i<ppixels.length;i++) ppixels[i]=pixels[c][i]*mask[i];
				SDFA_INSTANCE.showArrays(ppixels, fftSize, fftSize, "masked-amplitude"+IJ.d2s(sigmaScale*averageLength,3));
			}
Andrey Filippov's avatar
Andrey Filippov committed
675 676
			double SFM=0.0,SF=0.0,SM=0;
			for (int i=0;i<mask.length;i++){
677 678
				//				int x=(i % fftSize)-hsize;
				//				int y=(i / fftSize)-hsize;
Andrey Filippov's avatar
Andrey Filippov committed
679 680 681
				SFM+=pixels[c][i]*mask[i];
				SF+=pixels[c][i];
				if (mask[i]>0) SM+=mask[i]; // sum only positive masks? Or abs value? Does not really matter, it is just a scale
682

Andrey Filippov's avatar
Andrey Filippov committed
683 684 685 686 687
			}
			int S0=fftSize*fftSize;
			spectralContrast[c]=S0*SFM/SF/SM;
			if ((c==0) && (debugLevel>1)) System.out.println("SFM="+(SFM/S0)+" SF="+(SF/S0)+" SM="+(SM/S0)+" averageLength="+averageLength);
			if ( (debugLevel>1)) System.out.println("spectrumContrast["+c+"]="+spectralContrast[c]);
688

689 690 691 692 693 694 695
		} else{
			spectrumMaximums[c]=null;
			spectralContrast[c]=Double.NaN;
		}
		// 	    return 0.25*(spectralContrast[0]+spectralContrast[1]+spectralContrast[2]+spectralContrast[3]);
		return 0.5*(spectralContrast[0]+spectralContrast[3]); // green only
	}
Andrey Filippov's avatar
Andrey Filippov committed
696

697 698
	public void resetCorrelationSizesUsed(){
		this.correlationSizesUsed=null;
699

700 701 702 703 704 705 706
	}
	public void setCorrelationSizesUsed(int size){
		int maxSize=20;
		if (this.correlationSizesUsed==null) {
			this.correlationSizesUsed=new boolean [maxSize];
			for (int i=0;i<maxSize;i++) this.correlationSizesUsed[i]=false;
		}
Andrey Filippov's avatar
Andrey Filippov committed
707 708 709
		int ln2;
		for (ln2=0;size>(1<<ln2); ln2++);
		if (size!=(1<<ln2)){
710 711 712
			String msg="Not a power of 2 :"+ size;
			IJ.showMessage("Error",msg);
			throw new IllegalArgumentException (msg);
Andrey Filippov's avatar
Andrey Filippov committed
713 714
		}
		if (ln2>=maxSize) {
715 716 717
			String msg="Too large array length, increase maxSize (it is now "+maxSize+", wanted "+ln2+")";
			IJ.showMessage("Error",msg);
			throw new IllegalArgumentException (msg);
Andrey Filippov's avatar
Andrey Filippov committed
718 719
		}
		this.correlationSizesUsed[ln2]=true;
720

721 722 723 724
	}
	public boolean [] getCorrelationSizesUsed(){
		return this.correlationSizesUsed;
	}
Andrey Filippov's avatar
Andrey Filippov committed
725

Andrey Filippov's avatar
Andrey Filippov committed
726
	/* returns array of 3 arrays: first two are 3-element wave vectors (x,y,phase), last - 3-rd order correction coefficients */
727
	public double[][] findPatternDistorted(
728
			double [][]             bayer_mono_pixels, // pixel array to process (no windowing!), two greens will be used
Andrey Filippov's avatar
Andrey Filippov committed
729
			PatternDetectParameters patternDetectParameters,
730 731 732 733
			double                  min_half_period,
			double                  max_half_period,
			boolean                 greens,  // this is a pattern for combined greens (diagonal), adjust results accordingly
			String                  title){ // title prefix to use for debug  images
734
		if (bayer_mono_pixels==null) return null;
735
		if (bayer_mono_pixels.length < 1) return null;
736
		if (bayer_mono_pixels[0]==null) return null;
737 738
		if (greens) {
			if (bayer_mono_pixels.length<4) return null;
739
			if (bayer_mono_pixels[3]==null) return null;
740 741 742 743
		} else {
			if (bayer_mono_pixels.length > 1) return null;
		}
		boolean is_mono = bayer_mono_pixels.length == 1;
744 745 746
		int tile_size2=bayer_mono_pixels[0].length;
		int tile_size=(int) Math.sqrt(tile_size2);
		int hsize=tile_size/2;
Andrey Filippov's avatar
Andrey Filippov committed
747 748 749 750 751 752 753 754 755
		int hsize2=hsize*hsize;
		double [] quarterHamming=initWindowFunction(hsize, patternDetectParameters.gaussWidth);
		double [] patternCorr=new double[6]; // second order non-linear pattern correction (perspective+distortion)
		double [] green0= new double[hsize2];
		double [] green3= new double[hsize2];

		double [][]   quarter_pixels =new double[9][];
		double [][][] quarter_patterns =new double[9][][];
		int [] quarterIndex={0, // top left
756 757 758 759
				tile_size /2, // top right
				tile_size * tile_size/2, // bottom left
				(tile_size + 1) * tile_size/2, // bottom right
				(tile_size + 1) * tile_size/4, // center
760
				tile_size/4, // top
761 762 763
				tile_size * tile_size/4,// left
				(tile_size + 2) * tile_size/4,   // right
				(2 * tile_size + 1) * tile_size/4};   // bottom
Andrey Filippov's avatar
Andrey Filippov committed
764 765 766

		int i,j,iq;
		int index,qindex;
767
		if (this.debugLevel>2) SDFA_INSTANCE.showArrays(bayer_mono_pixels, tile_size, tile_size, title+"-bayer");
Andrey Filippov's avatar
Andrey Filippov committed
768 769 770
		for (iq=0; iq<9;iq++) {
			index=quarterIndex[iq];
			qindex=0;
771 772 773 774 775 776 777 778 779
			if (is_mono) {
				quarter_pixels[iq] = new double [hsize * hsize];
				for (i=0;i<hsize;i++) {
					for (j=0;j<hsize;j++) { //quarter_pixels[iq][qindex++]=input_pixels[index++];
						quarter_pixels[iq][qindex++] = bayer_mono_pixels[0][index++];
					}
					index+=hsize; // jump to the next line
				}
			} else {
780 781 782 783 784
				for (i=0;i<hsize;i++) {
					for (j=0;j<hsize;j++) { //quarter_pixels[iq][qindex++]=input_pixels[index++];
						green0[qindex]=  bayer_mono_pixels[0][index];
						green3[qindex++]=bayer_mono_pixels[3][index++];
					}
785 786 787 788 789
					//				quarter_pixels[iq]=combineDiagonalGreens (
					//						green0,
					//						green3,
					//						hsize,
					//						hsize);
790 791
					index+=hsize; // jump to the next line
				}
792 793 794 795 796 797 798 799 800
				// Moved outside of the i-loop, that was wrong!
				quarter_pixels[iq]=combineDiagonalGreens (
						green0,
						green3,
						hsize,
						hsize);

			}

Andrey Filippov's avatar
Andrey Filippov committed
801 802
			quarter_pixels[iq]= normalizeAndWindow (quarter_pixels[iq], quarterHamming);
			if (this.debugLevel>2) SDFA_INSTANCE.showArrays(quarter_pixels[iq],hsize, hsize, title+"-new"+iq);
803
			//			findPattern - see MSP 3290:
804 805 806
			quarter_patterns[iq] = findPattern(
					null, // 			DoubleFHT doubleFHT,
					quarter_pixels[iq],
Andrey Filippov's avatar
Andrey Filippov committed
807
					hsize,
808 809 810
					patternDetectParameters,
					min_half_period,
					max_half_period,
Andrey Filippov's avatar
Andrey Filippov committed
811 812 813 814
					greens,
					title+"Q_"+iq);
			if (quarter_patterns[iq]==null) return null;
		}
815 816


Andrey Filippov's avatar
Andrey Filippov committed
817 818 819 820 821 822 823 824 825 826 827
		if (this.debugLevel>2) {
			for (iq=0; iq<9;iq++) {
				System.out.println("Quarter="+iq+
						" W0x="+     IJ.d2s(quarter_patterns[iq][0][0],4)+
						" W0y="+     IJ.d2s(quarter_patterns[iq][0][1],4)+
						" W0_phase="+IJ.d2s(quarter_patterns[iq][0][2],2)+
						" W1x="+     IJ.d2s(quarter_patterns[iq][1][0],4)+
						" W1y="+     IJ.d2s(quarter_patterns[iq][1][1],4)+
						" W1_phase="+IJ.d2s(quarter_patterns[iq][1][2],2));
			}
		}
Andrey Filippov's avatar
Andrey Filippov committed
828
		/* Filter pattern coefficients to make sure they all match between quadrants (match to the center one)*/
Andrey Filippov's avatar
Andrey Filippov committed
829 830 831 832 833 834
		boolean patternsMatchedInitially=matchPatterns(quarter_patterns,quarter_patterns[4]);    // use center pattern
		if (this.debugLevel>2) {
			System.out.println(patternsMatchedInitially?"All quadrant wave vectors matched initially, no correction needed":"Some quadrant wave vectors were adjusted to match");
		}

		patternCorr=calcPatternNonLinear(quarter_patterns); // divide results by ,(FFT_SIZE/2)^2 - only first 5 patterns are used
Andrey Filippov's avatar
Andrey Filippov committed
835
		if (this.debugLevel>2) { /* increase LEVEL later */
Andrey Filippov's avatar
Andrey Filippov committed
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
			System.out.println("Pre- (1000x)   "+
					" Ax="+     IJ.d2s(1000*patternCorr[0]/(FFT_SIZE/2),5)+
					" Bx="+     IJ.d2s(1000*patternCorr[1]/(FFT_SIZE/2),5)+
					" Cx="+     IJ.d2s(1000*patternCorr[2]/(FFT_SIZE/2),5)+
					" Ay="+     IJ.d2s(1000*patternCorr[3]/(FFT_SIZE/2),5)+
					" By="+     IJ.d2s(1000*patternCorr[4]/(FFT_SIZE/2),5)+
					" Cy="+     IJ.d2s(1000*patternCorr[5]/(FFT_SIZE/2),5)+
					" Dx="+     IJ.d2s(1000*patternCorr[6],5)+
					" Ex="+     IJ.d2s(1000*patternCorr[7],5)+
					" Dy="+     IJ.d2s(1000*patternCorr[8],5)+
					" Ey="+     IJ.d2s(1000*patternCorr[9],5));
		}
		patternCorr=refinePatternNonLinear(quarter_patterns, // [tl,tr,bl,br, center][wv0, wv1][x,y,phase]
				patternCorr, //[ax,bx,cx,ay,by,cy]
				hsize ); // distance to quadrats center in sensor pixels ==FFT_SIZE/2



		//    for (i=0;i<patternCorr.length;i++)patternCorr[i]/= hsize;
Andrey Filippov's avatar
Andrey Filippov committed
855
		for (i=0;i<6;i++)patternCorr[i]/= hsize; /* Not linear Dx,Ex, Dy,Ey! */
Andrey Filippov's avatar
Andrey Filippov committed
856

Andrey Filippov's avatar
Andrey Filippov committed
857
		if (this.debugLevel>2) { /* increase LEVEL later */
Andrey Filippov's avatar
Andrey Filippov committed
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
			System.out.println("Corr (1000x)   "+
					" Ax="+     IJ.d2s(1000*patternCorr[0],5)+
					" Bx="+     IJ.d2s(1000*patternCorr[1],5)+
					" Cx="+     IJ.d2s(1000*patternCorr[2],5)+
					" Ay="+     IJ.d2s(1000*patternCorr[3],5)+
					" By="+     IJ.d2s(1000*patternCorr[4],5)+
					" Cy="+     IJ.d2s(1000*patternCorr[5],5)+
					" Dx="+     IJ.d2s(1000*patternCorr[6],5)+
					" Ex="+     IJ.d2s(1000*patternCorr[7],5)+
					" Dy="+     IJ.d2s(1000*patternCorr[8],5)+
					" Ey="+     IJ.d2s(1000*patternCorr[9],5));
		}

		double [][]result=new double [3][];
		result[0]=quarter_patterns[4][0].clone();
		result[1]=quarter_patterns[4][1].clone();
		result[2]=patternCorr.clone();
		return result;
	}
877
	/* ======================================================================== */
878 879
	public  double [] correlationContrast (
			double [] pixels,       // square pixel array
880
			double [] widowedGreens, // array to normalize correlation result
Andrey Filippov's avatar
Andrey Filippov committed
881
			double [][] wVectors,   // wave vectors (same units as the pixels array)
882 883
			double  contrastSelectSigmaCenter, // Gaussian sigma to select correlation centers (in PIXELS), 2.0
			double  contrastSelectSigmaOther,  // Gaussian sigma to select correlation off-centers centers (fraction of UV period), 0.1
884

Andrey Filippov's avatar
Andrey Filippov committed
885 886
			double x0,              // center coordinates
			double y0,
887
			String title){
888
		// for now - just comparison, later - switch to
889 890 891
		return correlationContrast (
				pixels,       // square pixel array
				wVectors,   // wave vectors (same units as the pixels array)
892 893
				contrastSelectSigmaCenter, // Gaussian sigma to select correlation centers (in PIXELS), 2.0
				contrastSelectSigmaOther,  // Gaussian sigma to select correlation off-centers centers (fraction of UV period), 0.1
894 895 896 897 898
				x0,              // center coordinates
				y0,
				title, // title base for optional plots names
				this.debugLevel);
	}
Andrey Filippov's avatar
Andrey Filippov committed
899
	public  double correlationContrastOld ( double [] pixels,       // square pixel array
900 901 902 903 904 905
			double [][] wVectors,   // wave vectors (same units as the pixels array)
			double ringWidth,       // ring (around r=0.5 dist to opposite corr) width
			double x0,              // center coordinates
			double y0,
			String title, // title base for optional plots names
			int debugLevel){
Andrey Filippov's avatar
Andrey Filippov committed
906 907
		int size=(int) Math.sqrt(pixels.length);
		double [] xy= new double [2];
908
		double [] uv;
Andrey Filippov's avatar
Andrey Filippov committed
909 910
		double r2,d;
		int i,j;
911
		/* 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)
Andrey Filippov's avatar
Andrey Filippov committed
912 913 914 915 916
    selecting center circle and a ring from 0.25 to 0.75 of the distance to opposite sign correlations */

		double r2WingsOuter= 0.0625*(1.0+ringWidth)*(1.0+ringWidth);
		double r2WingsInner= 0.0625*(1.0-ringWidth)*(1.0-ringWidth);
		double r2Center=0.0625*(ringWidth)*(ringWidth);
917
		if (debugLevel>2) System.out.println("rWingsOuter="+Math.sqrt(r2WingsOuter)+" rWingsInner="+Math.sqrt(r2WingsInner)+" rCenter="+Math.sqrt(r2Center));
Andrey Filippov's avatar
Andrey Filippov committed
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941

		double valCenter=0.0;
		double valWings=0.0;
		double numCenter=0.0;
		double numWings=0.0;
		for (i=0;i<size;i++) {
			xy[1]=i-size/2-y0;
			for (j=0;j<size;j++) {
				xy[0]=j-size/2-x0;
				uv=matrix2x2_mul(wVectors,xy);
				r2=uv[0]*uv[0]+uv[1]*uv[1];
				if (r2<=r2WingsOuter) {
					d=pixels[i*size+j];
					if (r2<=r2Center){
						valCenter+=d*d;
						numCenter+=1.0;
					} else if (r2>r2WingsInner){
						valWings+=d*d;
						numWings+=1.0;
					}
				}
			}
		}
		if ((numWings==0.0) || (numCenter==0.0)) {
942
			if (debugLevel>1) System.out.println("Not enough data for correlation contrast: numCenter="+numCenter+" numWings="+numWings+
Andrey Filippov's avatar
Andrey Filippov committed
943 944 945 946
					" valCenter="+IJ.d2s(valCenter,2)+" valWings="+IJ.d2s(valWings,2));
			return -1.0;
		}
		double contrast=Math.sqrt((valCenter/numCenter)/(valWings/numWings));
947
		if (debugLevel>2) {
Andrey Filippov's avatar
Andrey Filippov committed
948
			System.out.println("Correlation contrast is "+contrast);
949 950 951
			double [] maskedPixels=new double[size*size];
			double [] u_value=new double[size*size];
			double [] v_value=new double[size*size];
Andrey Filippov's avatar
Andrey Filippov committed
952 953 954 955 956 957 958 959
			int index;
			for (i=0;i<size;i++) {
				xy[1]=i-size/2-y0;
				for (j=0;j<size;j++) {
					xy[0]=j-size/2-x0;
					uv=matrix2x2_mul(wVectors,xy);
					r2=uv[0]*uv[0]+uv[1]*uv[1];
					index=i*size+j;
960 961
					u_value[index]=uv[0];
					v_value[index]=uv[1];
Andrey Filippov's avatar
Andrey Filippov committed
962 963 964 965
					/*          r=Math.sqrt(r2);
          r-=Math.floor(r);
          floatPixels[index]=(float) r;*/
					if (((r2<=r2WingsOuter) && (r2>r2WingsInner)) || (r2<=r2Center)){
966
						maskedPixels[index]= pixels[index];
Andrey Filippov's avatar
Andrey Filippov committed
967
					} else {
968
						maskedPixels[index]=0.0;
Andrey Filippov's avatar
Andrey Filippov committed
969 970 971
					}
				}
			}
972 973
			double [][] dbgPixels={pixels,maskedPixels,u_value,v_value};
			String [] titles={"all","masked","u","v"};
974
			(new ShowDoubleFloatArrays()).showArrays(
975 976 977 978 979 980
					dbgPixels,
					size,
					size,
					true,
					title+"_CORR_MASK",
					titles);
Andrey Filippov's avatar
Andrey Filippov committed
981 982 983
		}
		return contrast;
	}
Andrey Filippov's avatar
Andrey Filippov committed
984

985
	public double [] correlationContrast (
986
			double [] pixels,       // square pixel array
987
			double [][] wVectors,   // wave vectors (same units as the pixels array)
988 989
			double  sigma_center, // GGaussian sigma to select correlation centers (in PIXELS), 1.5
			double  sigma_other,  // Gaussian sigma to select correlation off-centers centers (fraction of UV period), 0.1
990 991 992 993
			double x0,              // center coordinates
			double y0,
			String title, // title base for optional plots names
			int debugLevel){
994 995 996 997 998
		double avg_rad = 2.0/Math.sqrt(wVectors[0][0]*wVectors[0][0]+wVectors[0][1]*wVectors[0][1]+wVectors[1][0]*wVectors[1][0]+wVectors[1][1]*wVectors[1][1]);
		double max_center_sigma = 0.05 * avg_rad; // approximate - make configurable or just relative rather than absolute?
		if (sigma_center > max_center_sigma) {
			sigma_center = max_center_sigma;
		}
999
		double [] badContrasts={-1.0,-1.0};
1000 1001 1002 1003 1004
		double sigma32_center=9*sigma_center*sigma_center; // in pixels
		double k_center=-0.5/(sigma_center*sigma_center);  // in pixels
		double sigma32_other=9*sigma_other*sigma_other;    // in periods
		double k_other=-0.5/(sigma_other*sigma_other);     // in periods
		double [][] sampleCentersXY={{0.0,0.0},{0.25,0.25},{0.25,-0.25},{-0.25,0.25},{-0.25,-0.25}};
1005
		// System.out.println("avg_rad="+avg_rad);
1006
		int [] sampleTypes = {0,1,1,1,1};
1007 1008
		int size=(int) Math.sqrt(pixels.length);
		double [] xy= new double [2];
1009
		double [] uv;
1010 1011
		double r2;
		int i,j;
1012
		// TODO: limit sigma_center to fit between others;
1013 1014 1015 1016
		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};
1017
		//		double [] dbg_weights = new double [size*size];
1018 1019 1020 1021 1022 1023 1024
		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++){
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
					if (sampleTypes[np] == 0) { // center spot, size in pixels
						r2 = xy[0]*xy[0] + xy[1]*xy[1];
						if (r2 < sigma32_center){
							double m=Math.exp(k_center*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;
						}
					} else {  // between correlation spots, size relative to the  periods
						double dx=uv[0]-sampleCentersXY[np][0];
						double dy=uv[1]-sampleCentersXY[np][1];
						r2=dx*dx+dy*dy;
						if (r2 < sigma32_other){
							double m=Math.exp(k_other*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;
						}

1048 1049 1050 1051 1052 1053 1054 1055
					}
				}
			}
		}
		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;
		}
1056
		double [][] dbg_corr_mask = {pixels, dbgMask};
1057 1058 1059 1060
		double aCenter= s[0]/w[0];
		double aQuiet=Math.sqrt(s[1]/w[1]);
		double rContrast=aCenter/aQuiet;
		double aContrast=aCenter/size/size;
1061

1062 1063 1064 1065 1066 1067 1068 1069
		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"};
1070
			(new ShowDoubleFloatArrays()).showArrays(
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
					dbgPixels,
					size,
					size,
					true,
					title+"_MASK",
					titles);
		}
		return contrasts;
	}
	public  double correlationContrastOld2 (
			double [] pixels,       // square pixel array
			double [] widowedGreens, // array to normailze correlation result
Andrey Filippov's avatar
Andrey Filippov committed
1083
			double [][] wVectors,   // wave vectors (same units as the pixels array)
1084 1085
			double sigma,
			double sigmaNorm,       // to measure variations for normalization of the contrast
Andrey Filippov's avatar
Andrey Filippov committed
1086 1087 1088 1089
			double x0,              // center coordinates
			double y0,
			String title, // title base for optional plots names
			int debugLevel){
1090 1091 1092
		// TODO: make configurable parameters
		//		double sigma=0.1;
		//		double sigmaNorm=0.5; // to measure variations for normalization of the contrast
1093

Andrey Filippov's avatar
Andrey Filippov committed
1094 1095
		double sigma32=9*sigma*sigma;
		double k=-0.5/(sigma*sigma);
1096

1097 1098
		double sigmaNorm32=9*sigmaNorm*sigmaNorm;
		double kNorm=-0.5/(sigmaNorm*sigmaNorm);
1099

Andrey Filippov's avatar
Andrey Filippov committed
1100
		double [][] sampleCentersXY={{0.0,0.0},{0.0,0.5},{0.5,0.0},{0.0,-0.5},{-0.5,0.0}};
1101
		int [] sampleTypes = {0,1,1,1,1};
Andrey Filippov's avatar
Andrey Filippov committed
1102 1103
		int size=(int) Math.sqrt(pixels.length);
		double [] xy= new double [2];
1104
		double [] uv;
Andrey Filippov's avatar
Andrey Filippov committed
1105 1106
		double r2;
		int i,j;
1107

1108
		/* 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)
Andrey Filippov's avatar
Andrey Filippov committed
1109 1110 1111 1112 1113
    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];
		for (int n=0;n<dbgMask.length;n++) dbgMask[n]=0.0;
		double [] s={0.0,0.0};
		double [] w={0.0,0.0};
1114
		double S0=0.0,S1=0.0,S2=0.0;
1115 1116
		double SG1=0.0,SG2=0.0;
		// Find measured pixels variations in the window
Andrey Filippov's avatar
Andrey Filippov committed
1117 1118 1119
		for (i=0;i<size;i++) {
			xy[1]=i-size/2-y0;
			for (j=0;j<size;j++) {
1120
				int index=i*size+j;
Andrey Filippov's avatar
Andrey Filippov committed
1121 1122 1123 1124 1125 1126 1127 1128
				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);
1129
						dbgMask[index]+=m;
Andrey Filippov's avatar
Andrey Filippov committed
1130
						w[sampleTypes[np]]+=m;
1131
						s[sampleTypes[np]]+=m*pixels[index];
Andrey Filippov's avatar
Andrey Filippov committed
1132 1133
					}
				}
1134 1135 1136 1137 1138 1139
				r2=uv[0]*uv[0]+uv[1]*uv[1];
				if (r2<sigmaNorm32){
					double m=Math.exp(kNorm*r2);
					S0+=m;
					S1+=m*pixels[index];
					S2+=m*pixels[index]*pixels[index];
1140 1141
					SG1=m*widowedGreens[index];
					SG2=m*widowedGreens[index]*widowedGreens[index];
1142
				}
Andrey Filippov's avatar
Andrey Filippov committed
1143 1144 1145 1146 1147 1148
			}
		}
		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 -1.0;
		}
1149
		double ref=Math.sqrt(S2*S0-S1*S1)/S0;
1150
		double refG=Math.sqrt(SG2*S0-SG1*SG1)/S0;
1151
		double contrast=((s[0]/w[0]) -(s[1]/w[1]))/ref;
1152
		double contrastG=((s[0]/w[0]) -(s[1]/w[1]))/refG; ///size;
1153
		if (debugLevel>2){
1154 1155 1156
			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);
1157
		}
1158 1159 1160 1161 1162
		//		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);
		//		}
		//		double contrast=Math.sqrt((s[0]/w[0]) /(s[1]/w[1]));
		//		double contrast=((s[0]/w[0]) -(s[1]/w[1]))/(size*size);
Andrey Filippov's avatar
Andrey Filippov committed
1163 1164 1165 1166
		if (debugLevel>2) {
			System.out.println("Correlation contrast is "+contrast);
			double [][] dbgPixels={pixels,dbgMask};
			String [] titles={"all","mask"};
1167
			(new ShowDoubleFloatArrays()).showArrays(
Andrey Filippov's avatar
Andrey Filippov committed
1168 1169 1170 1171 1172 1173 1174 1175 1176
					dbgPixels,
					size,
					size,
					true,
					title+"_CORR_MASK",
					titles);
		}
		return contrast;
	}
1177 1178 1179



1180
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
	public  double[] correlateWithModel (double [] imagePixels,  // measured pixel array
			double [] modelPixels,  // simulated (model) pixel array)
			double sigma,   // Sigma for high pass filtering TODO: implement!
			String title) { // title base for optional plots names

		if (imagePixels.length!=modelPixels.length) {
			IJ.showMessage("Error","Arrays have different sizes - imagePixels.length="+imagePixels.length+", modelPixels.length="+ modelPixels.length);
			return null;
		}
		int size = (int) Math.sqrt(imagePixels.length);
		ImageProcessor ip,ip_model;
		FHT fht,fht_model;
		double[][][] fft_complex,fft_model;
		int i,j;
		double a;

		float [] floatImagePixels=new float[size*size];
1198
		/* convert to float for image processor; */
Andrey Filippov's avatar
Andrey Filippov committed
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
		for (i=0;i<(size*size); i++) floatImagePixels[i]=(float) imagePixels[i];
		ip = new FloatProcessor(size,size);
		ip.setPixels(floatImagePixels);
		fht =  new FHT(ip);
		// Swapping quadrants, so the center will be 0,0
		fht.swapQuadrants();
		// get to frequency domain
		fht.transform();
		floatImagePixels=(float []) fht.getPixels();

		if((this.debugLevel>5) && (title!="")) {
			ImageProcessor ip_fht = new FloatProcessor(size,size);
			ip_fht.setPixels(floatImagePixels);
			ip_fht.resetMinAndMax();
			ImagePlus imp_fht= new ImagePlus(title+"_FHT_image", ip_fht);
			imp_fht.show();
		}
		// Convert from FHT to complex FFT
		fft_complex= FHT2FFTHalf (fht,size);
		float [] floatModelPixels=new float[size*size];
		// convert to float for image processor;
		for (i=0;i<(size*size); i++) floatModelPixels[i]=(float) modelPixels[i];
		ip_model = new FloatProcessor(size,size);
		ip_model.setPixels(floatModelPixels);
		fht_model =  new FHT(ip_model);
		// Swapping quadrants, so the center will be 0,0
		fht_model.swapQuadrants();
		// get to frequency domain
		fht_model.transform();
		floatModelPixels=(float []) fht_model.getPixels();
		if ((this.debugLevel>5) && (title!="")) {
			ImageProcessor ip_fht_model = new FloatProcessor(size,size);
			ip_fht_model.setPixels(floatModelPixels);
			ip_fht_model.resetMinAndMax();
			ImagePlus imp_fht_model= new ImagePlus(title+"_FHT_model", ip_fht_model);
			imp_fht_model.show();
		}
		// Convert from FHT to complex FFT
		fft_model= FHT2FFTHalf (fht_model,size);

		// multiply fft_complex by fft_nominator
		for (i=0;i<fft_complex.length; i++) for (j=0;j<fft_complex[0].length;j++) {
			a=                    fft_complex[i][j][0]*fft_model[i][j][0]+fft_complex[i][j][1]*fft_model[i][j][1]; // already changed Im() sign
			fft_complex[i][j][1]=-fft_complex[i][j][0]*fft_model[i][j][1]+fft_complex[i][j][1]*fft_model[i][j][0]; // already changed Im() sign
			fft_complex[i][j][0]=a;
1244
		}
1245
		/* Add sigma high=pass filtering here */
Andrey Filippov's avatar
Andrey Filippov committed
1246
		// Convert fft array back to fht array and
1247
		// set fht_target pixels with new values
Andrey Filippov's avatar
Andrey Filippov committed
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
		fht.setPixels (floatFFTHalf2FHT (fft_complex,size));
		/// optionally show the result
		if ((this.debugLevel>5) && (title!="")) {
			ImageProcessor ip_fht2 = new FloatProcessor(size,size);
			ip_fht2.setPixels(floatFFTHalf2FHT (fft_complex,size));
			ip_fht2.resetMinAndMax();
			ImagePlus imp_fht2= new ImagePlus(title+"-corr-sigma"+sigma, ip_fht2);
			imp_fht2.show();
		}

		/// transform to space

		fht.inverseTransform();
		fht.swapQuadrants();
		fht.resetMinAndMax();

		//   ImagePlus imp= new ImagePlus(title, ip_fht);
		if ((this.debugLevel>2) && (title!="")) {
			ImagePlus imp_corr= new ImagePlus(title+"_Correlated_filt-"+sigma, fht);
			imp_corr.show();
		}
		//   return direct_target;
		floatImagePixels =(float[])fht.getPixels();
		double [] pixels=new double[floatImagePixels.length];
		for (i=0;i<floatImagePixels.length;i++) pixels[i]=floatImagePixels[i];
		return pixels;
	}
1275 1276


Andrey Filippov's avatar
Andrey Filippov committed
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
	/**
	Refining non-linear mesh matching by comparing phases in the centers of 4 quadrants and the very center.
	Can only compensate to a fraction of mesh period (TBD - total range, probably +/-half period),
	so non-linear coefficients should be already known to that precision
	9 measurements are used here - top-left, top-right,bottom-left, bottom-right, center, top, left, right,bottom

	 */
	private	double [] refinePatternNonLinear(double [][][] qp, // [tl,tr,bl,br, center][wv0, wv1][x,y,phase]
			double [] nonlin, //[ax,bx,cx,ay,by,cy]
			int size ) { // distance to quadrants center in sensor pixels ==FFT_SIZE/2

		int iq,i,j;
		double [][] xy=new double [qp.length][2];
		double [] uv= new double [2];
		double [] duv=new double [2];

		//  double [][][] wl=new double [5][2][2]; // Wave length vectors - same direction as wavevectors, length=distance between wavefronts
		double [][][] wp=new double [9][2][3]; // pattern vectors (with phase)
		double x1,y1;
		double xq=0;
		double yq=0;
		//probably only wl[4] is needed
		for (iq=0;iq<9;iq++) for (i=0;i<2;i++) for (j=0;j<2;j++) wp[iq]= waveVectorsToPatternVectors(qp[iq][0], qp[iq][1]);

Andrey Filippov's avatar
Andrey Filippov committed
1301
		if (this.debugLevel>2) { /* increase LEVEL later */
Andrey Filippov's avatar
Andrey Filippov committed
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
			for (iq=0;iq<5;iq++){
				System.out.println(" wp["+iq+"][0][0]="+     IJ.d2s(wp[iq][0][0],4)+
						" wp["+iq+"][0][1]="+     IJ.d2s(wp[iq][0][1],4)+
						" wp["+iq+"][0][2]="+     IJ.d2s(wp[iq][0][2],4)+"("+ IJ.d2s(wp[iq][0][2]/2/Math.PI,4)+")"+
						" wp["+iq+"][1][0]="+     IJ.d2s(wp[iq][1][0],4)+
						" wp["+iq+"][1][1]="+     IJ.d2s(wp[iq][1][1],4)+
						" wp["+iq+"][1][2]="+     IJ.d2s(wp[iq][1][2],4)+"("+ IJ.d2s(wp[iq][1][2]/2/Math.PI,4)+")");
			}
		}
		for (iq=0;iq<9;iq++) {
			if (iq==4) continue; // nothing to calculate in the center
			switch (iq){
			case 0: xq=-1.0;yq=-1.0; break;
			case 1: xq= 1.0;yq=-1.0; break;
			case 2: xq=-1.0;yq= 1.0; break;
			case 3: xq= 1.0;yq= 1.0; break;
			case 4: xq= 0.0;yq= 0.0; break;
			case 5: xq= 0.0;yq=-1.0; break;
			case 6: xq=-1.0;yq= 0.0; break;
			case 7: xq= 1.0;yq= 0.0; break;
			case 8: xq= 0.0;yq= 1.0; break;
			}

			x1=size*(xq + nonlin[0]*xq*xq+ nonlin[1]*yq*yq+ 2* nonlin[2]*xq*yq +nonlin[6]*xq+ nonlin[7]*yq); // in pixels
			y1=size*(yq + nonlin[3]*xq*xq+ nonlin[4]*yq*yq+ 2* nonlin[5]*xq*yq +nonlin[8]*xq+ nonlin[9]*yq); // in pixels

Andrey Filippov's avatar
Andrey Filippov committed
1328
			/* convert x1,y1 into wp vector coordiantes */
Andrey Filippov's avatar
Andrey Filippov committed
1329 1330
			uv[0]=(wp[4][1][1]*x1-wp[4][1][0]*y1)/(wp[4][0][0]*wp[4][1][1]-wp[4][0][1]*wp[4][1][0]); // wl in center vectors, not local !
			uv[1]=(wp[4][0][0]*y1-wp[4][0][1]*x1)/(wp[4][0][0]*wp[4][1][1]-wp[4][0][1]*wp[4][1][0]);
Andrey Filippov's avatar
Andrey Filippov committed
1331
			/* Actually phases seem to be the same? */
Andrey Filippov's avatar
Andrey Filippov committed
1332 1333 1334
			duv[0]=uv[0]-Math.round(uv[0])- (wp[iq][0][2]-wp[4][0][2])/(Math.PI*2);
			duv[1]=uv[1]-Math.round(uv[1])- (wp[iq][1][2]-wp[4][1][2])/(Math.PI*2);

Andrey Filippov's avatar
Andrey Filippov committed
1335
			if (this.debugLevel>2) { /* increase LEVEL later */
Andrey Filippov's avatar
Andrey Filippov committed
1336 1337 1338 1339 1340 1341 1342 1343
				System.out.println("iq=  "+ iq+
						" x1="+     IJ.d2s(x1,4)+
						" y1="+     IJ.d2s(y1,4)+
						" uv[0]"+   IJ.d2s(uv[0],4)+
						" uv[1]"+   IJ.d2s(uv[1],4)+
						" duv[0]"+   IJ.d2s(duv[0],4)+
						" duv[1]"+   IJ.d2s(duv[1],4));
			}
Andrey Filippov's avatar
Andrey Filippov committed
1344
			/* re-normalize phases to be +/- 0.5 (+/-PI) range */
Andrey Filippov's avatar
Andrey Filippov committed
1345 1346
			duv[0]=duv[0]-Math.round(duv[0]);
			duv[1]=duv[1]-Math.round(duv[1]);
Andrey Filippov's avatar
Andrey Filippov committed
1347
			if (this.debugLevel>2) { /* increase LEVEL later */
Andrey Filippov's avatar
Andrey Filippov committed
1348
				System.out.println("iq=  "+ iq+" ------- "+
Andrey Filippov's avatar
Andrey Filippov committed
1349
						" duv[0]"+   IJ.d2s(duv[0],4)+	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
1350 1351 1352

						" duv[1]"+   IJ.d2s(duv[1],4));
			}
Andrey Filippov's avatar
Andrey Filippov committed
1353
			/* Fix half period vertical/half period horizontal shift - is that needed?*/
Andrey Filippov's avatar
Andrey Filippov committed
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
			if (Math.abs(duv[0])>0.25) {
				duv[0]+=0.5;
				duv[1]+=0.5;
				duv[0]=duv[0]-Math.round(duv[0]);
				duv[1]=duv[1]-Math.round(duv[1]);
				if (this.debugLevel>2) {
					System.out.println("Correct phase shift >0.25 in quadrant "+ iq+ ", now"+
							" duv[0]"+   IJ.d2s(duv[0],4)+
							" duv[1]"+   IJ.d2s(duv[1],4));
				}
			}

Andrey Filippov's avatar
Andrey Filippov committed
1366
			/* Verify here that phase adjustment is within range, fail otherwise */
Andrey Filippov's avatar
Andrey Filippov committed
1367 1368 1369 1370 1371 1372 1373
			if ((Math.abs(duv[0])>0.5) || (Math.abs(duv[1])>0.5)) {
				if (this.debugLevel>0) {
					System.out.println("Error: in quadrant "+ iq+" - attempted to adjust phase too much (>+/- pi/2), keeping initial parematers");
				}
				return nonlin;
			}

Andrey Filippov's avatar
Andrey Filippov committed
1374
			/* convert duv to x,y */
Andrey Filippov's avatar
Andrey Filippov committed
1375 1376 1377 1378
			xy[iq][0]=x1-wp[4][0][0]*duv[0]-wp[4][1][0]*duv[1];
			xy[iq][1]=y1-wp[4][0][1]*duv[0]-wp[4][1][1]*duv[1];


Andrey Filippov's avatar
Andrey Filippov committed
1379
			if (this.debugLevel>2) { /* increase LEVEL later */
Andrey Filippov's avatar
Andrey Filippov committed
1380 1381 1382
				System.out.println(" xy["+iq+"][0]="+   IJ.d2s(xy[iq][0],4)+
						" xy["+iq+"][1]="+   IJ.d2s(xy[iq][1],4));
			}
Andrey Filippov's avatar
Andrey Filippov committed
1383
			/* convert xy to non-linear differences, remove pixels dimensions - quadrats +/- 1.0 */
Andrey Filippov's avatar
Andrey Filippov committed
1384 1385 1386
			xy[iq][0]=(xy[iq][0]-size*xq)/size;
			xy[iq][1]=(xy[iq][1]-size*yq)/size;

Andrey Filippov's avatar
Andrey Filippov committed
1387
			if (this.debugLevel>2) { /* increase LEVEL later */
Andrey Filippov's avatar
Andrey Filippov committed
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
				System.out.println("Quadrant "+iq+": Original non-linear difference was"+
						" x="+   IJ.d2s(nonlin[0]*xq*xq+ nonlin[1]*yq*yq+ nonlin[2]*xq*yq,4)+
						" y="+   IJ.d2s(nonlin[3]*xq*xq+ nonlin[4]*yq*yq+ nonlin[5]*xq*yq,4));
				System.out.println("Quadrant "+iq+": Refined  non-linear difference is"+
						" x="+   IJ.d2s(xy[iq][0],4)+
						" y="+   IJ.d2s(xy[iq][1],4));
			}

		}

Andrey Filippov's avatar
Andrey Filippov committed
1398
		/* Do the refinement itself - recalculate coefficients minimizing errors in 5 points */
Andrey Filippov's avatar
Andrey Filippov committed
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
		/**

	    x1=size*(xq + nonlin[0]*xq*xq+ nonlin[1]*yq*yq+ 2* nonlin[2]*xq*yq +nonlin[6]*xq+ nonlin[7]*yq); // in pixels
	    y1=size*(yq + nonlin[3]*xq*xq+ nonlin[4]*yq*yq+ 2* nonlin[5]*xq*yq +nonlin[8]*xq+ nonlin[9]*yq); // in pixels
	  dx=AX*x^2 +BX*y^2 +2*CX*x*y + DX*x +EX *y
	  dy=AY*x^2 +BY*y^2 +2*CY*x*y + DY*x +EY *y

	  dx0= AX +BX +2*CX -DX -EX
	  dy0= AY +BY +2*CY -DY -EY

	  dx1= AX +BX -2*CX +DX -EX
	  dy1= AY +BY -2*CY +DY -EY

	  dx2= AX +BX -2*CX -DX +EX
	  dy2= AY +BY -2*CY -DY +EY

	  dx3= AX +BX +2*CX +DX +EX
	  dy3= AY +BY +2*CY +DY +EY

	  dx5=    +BX           -EX
	  dy5=    +BY           -EY

	  dx6= AX           -DX
	  dy6= AY           -DY

	  dx7= AX           +DX
	  dy7= AY           +DY

	  dx8=    +BX           +EX
	  dy8=    +BY           +EY

	//---------
	-(1)   dx0= AX +BX +2*CX -DX -EX
	-(2)   dx1= AX +BX -2*CX +DX -EX
	+(3)   dx2= AX +BX -2*CX -DX +EX
	+(4)   dx3= AX +BX +2*CX +DX +EX
	-(5)   dx5=    +BX           -EX
	(6)   dx6= AX           -DX
	(7)   dx7= AX           +DX
	+(8)   dx8=    +BX           +EX
	-(1)+(2)-(3)+(4)-(6)+(7) DX=()-dx0+dx1-dx2+dx3-dx6+dx7)/6
	-(1)-(2)+(3)+(4)-(5)+(8) EX=()-dx0-dx1+dx2+dx3-dx5+dx8)/6
	-(1)-(2)+(3)+(4)-(5)+(8) EY=()-dy0-dy1+dy2+dy3-dy5+dy8)/6
	-(1)+(2)-(3)+(4)-(6)+(7) DY=()-dy0+dy1-dy2+dy3-dy6+dy7)/6

	  AX+BX+2*CX = p1x= (dx0+dx3)/2
	  AY+BY+2*CY = p1y= (dy0+dy3)/2
	  AX+BX-2*CX = p2x= (dx1+dx2)/2
	  AY+BY-2*CY = p2y= (dy1+dy2)/2
	     BX      = p3x= (dx5+dx8)/2
	     BY      = p3y= (dy5+dy8)/2
	  AX         = p4x= (dx6+dx7)/2
	  AY         = p4y= (dy6+dy7)/2
	//minimizing sum of squares of errors:
	  CX= (p1x-p2x)/4
	  CY= (p1y-p2y)/4
	  AX= (3*p4x-2*p3x+p1x+p2x)/5
	  AY= (3*p4y-2*p3y+p1y+p2y)/5
	  BX= (3*p3x-2*p4x+p1x+p2x)/5
	  BY= (3*p3y-2*p4y+p1y+p2y)/5
		 */
		double p1x =(xy[0][0]+xy[3][0])/2;
		double p1y =(xy[0][1]+xy[3][1])/2;

		double p2x =(xy[1][0]+xy[2][0])/2;
		double p2y =(xy[1][1]+xy[2][1])/2;

		double p3x =(xy[5][0]+xy[8][0])/2;
		double p3y =(xy[5][1]+xy[8][1])/2;

		double p4x =(xy[6][0]+xy[7][0])/2;
		double p4y =(xy[6][1]+xy[7][1])/2;

		double [] rslt=new double[10];
		rslt[2]=(p1x-p2x)/4; //CX
		rslt[5]=(p1y-p2y)/4; //CY
		rslt[0]= (3*p4x-2*p3x+p1x+p2x)/5 ; //AX
		rslt[3]= (3*p4y-2*p3y+p1y+p2y)/5; // AY
		rslt[1]= (3*p3x-2*p4x+p1x+p2x)/5; // BX
		rslt[4]= (3*p3y-2*p4y+p1y+p2y)/5; // BY
		/*
	-(1)+(2)-(3)+(4)-(6)+(7) DX=(-dx0+dx1-dx2+dx3-dx6+dx7)/6
	-(1)-(2)+(3)+(4)-(5)+(8) EX=(-dx0-dx1+dx2+dx3-dx5+dx8)/6
	-(1)-(2)+(3)+(4)-(5)+(8) EY=(-dy0-dy1+dy2+dy3-dy5+dy8)/6
	-(1)+(2)-(3)+(4)-(6)+(7) DY=(-dy0+dy1-dy2+dy3-dy6+dy7)/6
		 */
		rslt[6]=(-xy[0][0]+xy[1][0]-xy[2][0]+xy[3][0]-xy[6][0]+xy[7][0])/6; // DX=(-dx0+dx1-dx2+dx3-dx6+dx7)/6
		rslt[7]=(-xy[0][0]-xy[1][0]+xy[2][0]+xy[3][0]-xy[5][0]+xy[8][0])/6; // EX=(-dx0-dx1+dx2+dx3-dx5+dx8)/6
		rslt[8]=(-xy[0][1]+xy[1][1]-xy[2][1]+xy[3][1]-xy[6][1]+xy[7][1])/6; // EY=(-dy0-dy1+dy2+dy3-dy5+dy8)/6
		rslt[9]=(-xy[0][1]-xy[1][1]+xy[2][1]+xy[3][1]-xy[5][1]+xy[8][1])/6; // DY=(-dy0+dy1-dy2+dy3-dy6+dy7)/6

		return rslt; // temporary
	}
Andrey Filippov's avatar
Andrey Filippov committed
1492
	/* ======================================================================== */
1493 1494
	public double[][] findPatternUsedNow(
			double [] input_pixels, // pixel array to process
Andrey Filippov's avatar
Andrey Filippov committed
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
			int size, // FFT size
			PatternDetectParameters patternDetectParameters,
			boolean greens, // this is a pattern for combined greens (diagonal), adjust results accordingly
			String title) { // title prefix to use for debug  images
		double [] pixels=input_pixels.clone();
		double [][]result=new double [2][3];
		//System.out.println("pixels.length="+pixels.length); //4096

		ImageProcessor ip, ip1;
		FHT fht, fht1;
		double[][][] fft_complex,fft_corr;
		double[][] fft_gamma;
		int i,j;
		double DCLevel=0.0;
		double a;
		float []floatPixels=new float[pixels.length];
		for (i=0;i<pixels.length; i++)  DCLevel+=pixels[i];
		DCLevel/=(size*size);
		for (i=0;i<pixels.length; i++)  pixels[i]-=DCLevel;
		// convert to float for image processor;
		for (i=0;i<pixels.length; i++) floatPixels[i]=(float) pixels[i];
		ip = new FloatProcessor(size,size);
		ip.setPixels(floatPixels);
		if (this.debugLevel>8) {
			ip.resetMinAndMax();
			ImagePlus imp_direct=  new ImagePlus(title+"_Direct_"+patternDetectParameters.corrGamma, ip);
			imp_direct.show();
		}
		fht =  new FHT(ip);
		// Swapping quadrants, so the center will be 0,0
		fht.swapQuadrants();
		// get to frequency domain
		fht.transform();
		if (this.debugLevel>5) {
			floatPixels=(float []) fht.getPixels();
			ImageProcessor ip_fht = new FloatProcessor(size,size);
			ip_fht.setPixels(floatPixels);
			ip_fht.resetMinAndMax();
			ImagePlus imp_fht= new ImagePlus(title+"_FHT", ip_fht);
			imp_fht.show();
		}

		// Convert from FHT to complex FFT
		fft_complex= FHT2FFTHalf (fht,size);
		// will need fft_complex  again later for later phase pattern measurements, calculate fft_gamma for correlation (pattern 2 frequencies measurement)
		fft_gamma=new double [size][size];
		floatPixels=new float[pixels.length];
		DCLevel=0.0;
		for (i=0;i<fft_complex.length; i++) for (j=0;j<fft_complex[0].length;j++) {
			fft_gamma[i][j]=Math.pow(fft_complex[i][j][0]*fft_complex[i][j][0]+fft_complex[i][j][1]*fft_complex[i][j][1],patternDetectParameters.corrGamma);
			DCLevel+=fft_gamma[i][j];
			floatPixels[i*size+j]=(float) fft_gamma[i][j];
		}
		DCLevel/=(fft_complex.length*fft_complex[0].length);
		for (i=0;i<fft_complex.length; i++) for (j=0;j<fft_complex[0].length;j++) {
			floatPixels[i*size+j]-=DCLevel;
			if ((i>0)&& (i<(size/2))){
				floatPixels[(size-i)*size+((size-j)%size)]=floatPixels[i*size+j];
			}
		}

Andrey Filippov's avatar
Andrey Filippov committed
1556
		/* TODO:  maybe it is better to find the pattern frequencies just here, without converting back.
Andrey Filippov's avatar
Andrey Filippov committed
1557
    After rejecting low frequencies, there seem to be just 2 nice maximums - easy to extract*/
1558
		// now perform direct FFT of gamma(power spectrum)
Andrey Filippov's avatar
Andrey Filippov committed
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
		ip1 = new FloatProcessor(size,size);
		ip1.setPixels(floatPixels);
		if (this.debugLevel>7) {
			ip1.resetMinAndMax();
			ImagePlus imp1=  new ImagePlus(title+"_gamma(ps)_"+patternDetectParameters.corrGamma, ip1);
			imp1.show();
		}
		fht1 =  new FHT(ip1);
		// Swapping quadrants, so the center will be 0,0
		fht1.swapQuadrants();
		fht1.transform();
		fft_corr= FHT2FFTHalf (fht1,size);
		double[] highPassFilter=new double[fft_complex[0].length];
		double expK=(patternDetectParameters.corrSigma>0)?(1.0/(2*patternDetectParameters.corrSigma*patternDetectParameters.corrSigma)):0.0;
		for (j=0;j<=fft_complex[0].length/2;j++) {
			highPassFilter[j]=(expK>0.0)?(1.0-Math.exp(-(expK*j*j))):1.0;
			if (j>0) highPassFilter[highPassFilter.length-j]=highPassFilter[j];
		}

		for (i=0;i<fft_complex.length; i++) for (j=0;j<fft_complex[0].length;j++) {
			fft_corr[i][j][0]=highPassFilter[i]*highPassFilter[j]*(fft_corr[i][j][0]*fft_corr[i][j][0]+fft_corr[i][j][1]*fft_corr[i][j][1]);
			fft_corr[i][j][1]=0.0;
		}

		// Convert fft array back to fht array and
1584
		// set fht_target pixels with new values
Andrey Filippov's avatar
Andrey Filippov committed
1585
		fht1.setPixels (floatFFTHalf2FHT (fft_corr,size));   /* FIXME: - done, there is no difference as Im()==0 */
Andrey Filippov's avatar
Andrey Filippov committed
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
		/// optionally show the result
		if (this.debugLevel>7) {
			ImageProcessor ip_fht2 = new FloatProcessor(size,size);
			ip_fht2.setPixels(floatFFTHalf2FHT (fft_corr,size));
			ip_fht2.resetMinAndMax();
			ImagePlus imp_fht2= new ImagePlus(title+"_fht_corr_"+patternDetectParameters.corrGamma, ip_fht2);
			imp_fht2.show();
		}

		/// transform to space
		fht1.inverseTransform();
		floatPixels=(float []) fht1.getPixels();
		a=1/floatPixels[0];
		for (i=0; i<floatPixels.length; i++){
			floatPixels[i]*=a;
		}
		fht1.setPixels(floatPixels);

		//System.out.println("2:y="+y+" x="+x+" base_b="+base_b+" base="+base);

		fht1.swapQuadrants();

		if (this.debugLevel>2) {
			fht1.resetMinAndMax();
			ImagePlus imp_corr= new ImagePlus(title+"_corr_"+patternDetectParameters.corrGamma, fht1);
			imp_corr.show();
		}
		//   return direct_target;
		floatPixels =(float[])fht1.getPixels();
		for (i=0;i<floatPixels.length;i++) pixels[i]=floatPixels[i];


		int [][] max2OnSpectrum=  findFirst2MaxOnSpectrum (fft_complex, // complex, top half, starting from 0,0
				1,   // skip +- from (0,0) and previous max - add parameter to dialog?
				0.5); // 0.5 - 30deg. orthogonality of 2 vectors - 1.0 - perpendicular, 0.0 - parallel - add parameter to dialog?
		/**TODO:  get out on failure */
		if (max2OnSpectrum==null) {
			if (this.debugLevel>2){
				System.out.println("findPattern() 1: Failed to find a pattern");
				if (this.debugLevel>2){
					SDFA_INSTANCE.showArrays(input_pixels, "failed-findPattern-1-");
				}
			}
			return null;
		}
Andrey Filippov's avatar
Andrey Filippov committed
1631
		/* Trying to filter out unreasonable maximums (if there is no pattern at all) */
Andrey Filippov's avatar
Andrey Filippov committed
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
		double maxFrequency=0.25*fft_complex.length;
		if ((Math.abs(max2OnSpectrum[0][0])>maxFrequency) ||
				(Math.abs(max2OnSpectrum[0][1])>maxFrequency) ||
				(Math.abs(max2OnSpectrum[1][0])>maxFrequency) ||
				(Math.abs(max2OnSpectrum[1][1])>maxFrequency)) {
			if (this.debugLevel>2) {
				System.out.println("Failed to detect pattern, as frequecy is above limit="+IJ.d2s(maxFrequency,2));
				System.out.println("Maximum 1 on spectrum:  x="+IJ.d2s(max2OnSpectrum[0][0],4)+" y="+IJ.d2s(max2OnSpectrum[0][1],4));
				System.out.println("Maximum 2 on spectrum:  x="+IJ.d2s(max2OnSpectrum[1][0],4)+" y="+IJ.d2s(max2OnSpectrum[1][1],4));
			}
			return null;
		}
		if (this.debugLevel>6) {
			System.out.println("Maximum 1 on spectrum:  x="+IJ.d2s(max2OnSpectrum[0][0],4)+" y="+IJ.d2s(max2OnSpectrum[0][1],4));
			System.out.println("Maximum 2 on spectrum:  x="+IJ.d2s(max2OnSpectrum[1][0],4)+" y="+IJ.d2s(max2OnSpectrum[1][1],4));
		}

		int [][] startPoints={{max2OnSpectrum[0][0]+max2OnSpectrum[1][0], max2OnSpectrum[0][1]+max2OnSpectrum[1][1]},
				{max2OnSpectrum[0][0]-max2OnSpectrum[1][0], max2OnSpectrum[0][1]-max2OnSpectrum[1][1]}};
Andrey Filippov's avatar
Andrey Filippov committed
1651
		if (startPoints[1][1] <0) { /* startPoints[1][1] > 0 anyway */
Andrey Filippov's avatar
Andrey Filippov committed
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
			startPoints[1][0]= -startPoints[1][0];
			startPoints[1][1]= -startPoints[1][1];
		}
		if (this.debugLevel>2) {
			System.out.println("Predicted correlation maximum 1 from spectrum:  x="+IJ.d2s(startPoints[0][0],4)+" y="+IJ.d2s(startPoints[0][1],4));
			System.out.println("Predicted correlation maximum 2 from spectrum:  x="+IJ.d2s(startPoints[1][0],4)+" y="+IJ.d2s(startPoints[1][1],4));
		}

		double[][] max2=  findFirst2MaxOnCorrelation(
				pixels,
				startPoints,
				patternDetectParameters
1664
				);
Andrey Filippov's avatar
Andrey Filippov committed
1665 1666 1667 1668 1669 1670 1671 1672

		/**TODO:  get out on failure */
		if (max2==null) {
			if (this.debugLevel>2){
				System.out.println("findPattern() 2: Failed to find a pattern");
				if (this.debugLevel>2){
					SDFA_INSTANCE.showArrays(input_pixels, "failed-findPattern-2-");
				}
1673

Andrey Filippov's avatar
Andrey Filippov committed
1674 1675 1676
			}
			return null;
		}
Andrey Filippov's avatar
Andrey Filippov committed
1677
		/* these are combined greens, convert vectors to original pixel space) */
1678
		if (greens) {
Andrey Filippov's avatar
Andrey Filippov committed
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
			double [][] rotMatrix= {{1.0,-1.0},{1.0,1.0}};
			double [][] max2orig= matrix2x2_mul(max2,rotMatrix);
			for (i=0;i<2;i++) for (j=0;j<2;j++) result[i][j]=max2orig[i][j]; // result is [2][3], max2orig is [2][2]
			if (this.debugLevel>2) {
				System.out.println("Corrected to original pixels[0]  x="+IJ.d2s(result[0][0],4)+" y="+IJ.d2s(result[0][1],4));
				System.out.println("Corrected to original pixels[1]  x="+IJ.d2s(result[1][0],4)+" y="+IJ.d2s(result[1][1],4));
			}
		} else {
			for (i=0;i<2;i++) for (j=0;j<2;j++) result[i][j]=max2[i][j]; // result is [2][3], max2 is [2][2]
		}
Andrey Filippov's avatar
Andrey Filippov committed
1689
		/* Calculate locations of the maximums on FFT (corresponding to the diagonals of the checkerboard pattern) */
Andrey Filippov's avatar
Andrey Filippov committed
1690 1691
		double [][] maxOnFFT = {{2*size*(max2[0][0]-max2[1][0]),2*size*(max2[0][1]-max2[1][1])},
				{2*size*(max2[0][0]+max2[1][0]),2*size*(max2[0][1]+max2[1][1])}};
Andrey Filippov's avatar
Andrey Filippov committed
1692
		/*  We have only one half of the FFT data  so rotate 180-degrees around the center if the point is in the bottom half*/
Andrey Filippov's avatar
Andrey Filippov committed
1693 1694 1695
		double [] maxPhases=new double[2];
		boolean [] invertPhaseSign={false,false};
		int maxIndex; // iterate through the two maximums on FFT for phase measurement
1696
		int [][]      interpolateXY= new int [2][2];
Andrey Filippov's avatar
Andrey Filippov committed
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
		double [][]   interpolateKxy=new double [2][2];
		double [][][] interpolatePhases=new double [2][2][2];
		double [][][] interpolateAmplitudes=new double [2][2][2];// Maybe use it? if the amplitudes are very different?
		int ix,iy;
		boolean phaseCorr; // phase shift before averaging, to prevent rollover
		for (maxIndex=0;maxIndex<2;maxIndex++) {
			if (maxOnFFT[maxIndex][1]<0) {
				invertPhaseSign[maxIndex]=true;
				maxOnFFT[maxIndex][0]=-maxOnFFT[maxIndex][0];
				maxOnFFT[maxIndex][1]=-maxOnFFT[maxIndex][1];
			}
			interpolateXY [maxIndex][1] = (int) maxOnFFT[maxIndex][1];
			interpolateKxy[maxIndex][1] = maxOnFFT[maxIndex][1] - interpolateXY [maxIndex][1];
			if (maxOnFFT[maxIndex][0]<0) {
				interpolateXY  [maxIndex][0] = (int) (size+ maxOnFFT[maxIndex][0]);
				interpolateKxy [maxIndex][0] = (size+ maxOnFFT[maxIndex][0]) - interpolateXY[maxIndex][0];
			} else {
				interpolateXY  [maxIndex][0] = (int) maxOnFFT[maxIndex][0];
				interpolateKxy [maxIndex][0] = maxOnFFT[maxIndex][0] - interpolateXY[maxIndex][0];
			}
			for (j=0;j<2;j++) {
				ix=(interpolateXY[maxIndex][0]+j) % size;
				for (i=0;i<2;i++) {
					iy=interpolateXY[maxIndex][1]+i;
					interpolateAmplitudes[maxIndex][i][j]= Math.sqrt(fft_complex[iy][ix][0]*fft_complex[iy][ix][0]+fft_complex[iy][ix][1]*fft_complex[iy][ix][1]);
					interpolatePhases[maxIndex][i][j]= Math.atan2((invertPhaseSign[maxIndex]?-1.0:1.0)*fft_complex[iy][ix][1], fft_complex[iy][ix][0]);
					if (this.debugLevel>5) {
						System.out.println("maxIndex="+maxIndex+" ix="+ix+" iy="+iy+" phase="+IJ.d2s(interpolatePhases[maxIndex][i][j],4)+" amplitude="+interpolateAmplitudes[maxIndex][i][j]);
					}
				}
			}
			phaseCorr=false;
			if ((interpolatePhases[maxIndex][0][0]> Math.PI/2) ||
					(interpolatePhases[maxIndex][0][1]> Math.PI/2) ||
					(interpolatePhases[maxIndex][1][0]> Math.PI/2) ||
					(interpolatePhases[maxIndex][1][1]> Math.PI/2) ||
					(interpolatePhases[maxIndex][0][0]<-Math.PI/2) ||
					(interpolatePhases[maxIndex][0][1]<-Math.PI/2) ||
					(interpolatePhases[maxIndex][1][0]<-Math.PI/2) ||
					(interpolatePhases[maxIndex][1][1]<-Math.PI/2)) {
				phaseCorr=true;
				interpolatePhases[maxIndex][0][0]+= (interpolatePhases[maxIndex][0][0]<0)?Math.PI:-Math.PI;
				interpolatePhases[maxIndex][0][1]+= (interpolatePhases[maxIndex][0][1]<0)?Math.PI:-Math.PI;
				interpolatePhases[maxIndex][1][0]+= (interpolatePhases[maxIndex][1][0]<0)?Math.PI:-Math.PI;
				interpolatePhases[maxIndex][1][1]+= (interpolatePhases[maxIndex][1][1]<0)?Math.PI:-Math.PI;
				if (this.debugLevel>5) {
					System.out.println("Shifting phases by PI/2 before averaging (to avoid rollover)");
				}
			}

			maxPhases[maxIndex]=       interpolateKxy[maxIndex][1] *(interpolateKxy[maxIndex][0] * interpolatePhases[maxIndex][1][1] + (1.0-interpolateKxy[maxIndex][0])* interpolatePhases[maxIndex][1][0])+
1748
					(1.0 - interpolateKxy[maxIndex][1])*(interpolateKxy[maxIndex][0] * interpolatePhases[maxIndex][0][1] + (1.0-interpolateKxy[maxIndex][0])* interpolatePhases[maxIndex][0][0]);
Andrey Filippov's avatar
Andrey Filippov committed
1749 1750 1751 1752 1753 1754 1755 1756
			if (phaseCorr) maxPhases[maxIndex]+=(maxPhases[maxIndex]<0)?Math.PI:-Math.PI;
			if (this.debugLevel>5) {
				System.out.println("kx="+IJ.d2s(interpolateKxy [maxIndex][0],4)+ " ky="+IJ.d2s(interpolateKxy [maxIndex][1],4));
			}
			if (this.debugLevel>2) {
				System.out.println("maxIndex="+maxIndex+" phase="+IJ.d2s(maxPhases[maxIndex],4));
			}
		}
Andrey Filippov's avatar
Andrey Filippov committed
1757
		double [] checkerPhases= findCheckerPhases(max2, maxPhases); /* may be different for greens==true . No, the same */
Andrey Filippov's avatar
Andrey Filippov committed
1758 1759 1760 1761
		for (i=0;i<2;i++) result[i][2]=checkerPhases[i];
		if (this.debugLevel>2)  System.out.println();
		return result;
	}
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785

	// Above is currently used for years, below is for experiments

	public double[][] findPattern(
			DoubleFHT doubleFHT,
			double [] input_pixels, // pixel array to process
			int size, // FFT size
			PatternDetectParameters patternDetectParameters,
			double    min_half_period,
			double    max_half_period,
			boolean greens, // this is a pattern for combined greens (diagonal), adjust results accordingly
			String title) { // title prefix to use for debug  images

		if (!patternDetectParameters.use_large_cells) {
			return findPatternUsedNow(
					input_pixels, // pixel array to process
					size, // FFT size
					patternDetectParameters,
					greens, // this is a pattern for combined greens (diagonal), adjust results accordingly
					title); // title prefix to use for debug  images
		}
		int debug_threshold = 4;

//		this.debugLevel = 11;
1786 1787 1788
		if (this.debugLevel > 5) {
			System.out.println("findPattern(): this.debugLevel = "+this.debugLevel);
		}
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
		if (doubleFHT == null) doubleFHT = new DoubleFHT(); // pass from the caller to re-use
		double [] cpixels=input_pixels.clone();
		if (min_half_period < 2) {
			min_half_period = 2;
		}
		double [] dfht = new double [input_pixels.length]; // will save FHT from phase auto correlation
		double [][]rslt= new double [2][3];
		cpixels = doubleFHT.phaseCorrelate (cpixels, patternDetectParameters.phaseCoeff, 0, patternDetectParameters.lowpass_sigma, dfht);
		if (this.debugLevel > (debug_threshold+1)) {
			double [][] src_corr = {input_pixels, cpixels};
			String [] titles = {"source","corr"};
			(new ShowDoubleFloatArrays()).showArrays(src_corr,size,size,true,"fft_corr-"+patternDetectParameters.phaseCoeff,titles);
		}

		int [][] half_periods = findFirst2MinOnCorrelation( cpixels, min_half_period, max_half_period);
		if (half_periods != null) { // check vectors are not close to colinear
			double sin =  (half_periods[0][0]*half_periods[1][1]-half_periods[0][1]*half_periods[1][0])/
					Math.sqrt((half_periods[0][0]*half_periods[0][0] + half_periods[0][1]*half_periods[0][1])*
							(half_periods[1][0]*half_periods[1][0] + half_periods[1][1]*half_periods[1][1]));
			if (Math.abs(sin) < patternDetectParameters.min_sin) {
				if (this.debugLevel > debug_threshold) System.out.println("Half-period vectors on correlation are too close to colinear - sin="+sin);
				return null;
			}

		}
		if (this.debugLevel>7) {
			if (half_periods == null) {
				if (this.debugLevel > (debug_threshold+0)) System.out.println("Could not find half-periods on correlation");
				return null;
			} else  {
				if (this.debugLevel > (debug_threshold+1)) System.out.println("first half period (x/y) :"+half_periods[0][0]+"/"+half_periods[0][1]+
1820
						", second half period :"+half_periods[1][0]+"/"+half_periods[1][1]);
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
			}
		}

		double [][] dhp  = getPeriodsFromCorrelation(
				cpixels,
				half_periods, // a pair of half-periods
				patternDetectParameters.min_frac,
				patternDetectParameters.no_crazy) ;
		if (dhp != null) {
			if (this.debugLevel > (debug_threshold+1)) System.out.println(String.format("findPattern(): dhp= [[%.5f,%.5f],[%.5f,%.5f]]",dhp[0][0],dhp[0][1],dhp[1][0],dhp[1][1]));
		} else {
			if (this.debugLevel > (debug_threshold+0)) System.out.println("findPattern():  getPeriodsFromCorrelation() FAILED");
		}

		/* these are combined greens, convert vectors to original pixel space)! */
		if (greens) {
			double [][] rotMatrix= {{1.0,-1.0},{1.0,1.0}};
			double [][] max2orig= matrix2x2_mul(dhp,rotMatrix);
			for (int i=0; i<2; i++) for (int j=0; j<2; j++) rslt[i][j]=max2orig[i][j]; // result is [2][3], max2orig is [2][2]
		} else {
1841 1842 1843 1844 1845
			for (int i=0;i<2;i++) for (int j=0;j<2;j++) rslt[i][j]=2.0 * dhp[i][j]; // result is [2][3], max2 is [2][2]
		}
		if (this.debugLevel > (debug_threshold + 0)) {
			System.out.println("Corrected to original pixels[0]  x="+IJ.d2s(rslt[0][0],4)+" y="+IJ.d2s(rslt[0][1],4));
			System.out.println("Corrected to original pixels[1]  x="+IJ.d2s(rslt[1][0],4)+" y="+IJ.d2s(rslt[1][1],4));
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
		}



		// Calculate complex FFT - needed to determine pattern phases (autocorrelation does not provide this
		// dfht array has FHT(input_pixels) save from calculation of the phase autocorrelation

		double [][][] fft_cmplx = doubleFHT.FHT2FFTHalf(dfht,size);
		if (this.debugLevel > (debug_threshold+2)) {
			(new ShowDoubleFloatArrays()).showComplex(fft_cmplx,"fft_cmplx");
		}

		/* Calculate locations of the maximums on FFT (corresponding to the diagonals of the checkerboard pattern) */
		double [][] max_on_FFT = {{2*size*(dhp[0][0]-dhp[1][0]),2*size*(dhp[0][1]-dhp[1][1])},
				{2*size*(dhp[0][0]+dhp[1][0]),2*size*(dhp[0][1]+dhp[1][1])}};
		/*  We have only one half of the FFT data  so rotate 180-degrees around the center if the point is in the bottom half*/
		double [] max_phases =    getPatternPhasesFromFFT(fft_cmplx, size, max_on_FFT);
		double [] checker_phases= findCheckerPhases(dhp, max_phases); /* may be different for greens==true . No, the same */
		for (int i=0;i<2;i++) rslt[i][2]=checker_phases[i];
		if (this.debugLevel > (debug_threshold + 0)) System.out.println();
1866
		return rslt;
1867
	}
Andrey Filippov's avatar
Andrey Filippov committed
1868
	/* ======================================================================== */
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
	// calculate pattern 3D phases by interpolating im/re for the found maximums
	private double [] getPatternPhasesFromFFT(
			double [][][] fft_complex,
			int           size,
			double [][]   maxOnFFT) {
		double [] maxPhases=new double[2];
		boolean [] invertPhaseSign={false,false};
		int maxIndex; // iterate through the two maximums on FFT for phase measurement
		int [][]      interpolateXY= new int [2][2];
		double [][]   interpolateKxy=new double [2][2];
		double [][][] interpolatePhases=new double [2][2][2];
		double [][][] interpolateAmplitudes=new double [2][2][2];// Maybe use it? if the amplitudes are very different?
		int ix,iy;
		boolean phaseCorr; // phase shift before averaging, to prevent rollover
		for (maxIndex=0;maxIndex<2;maxIndex++) {
			if (maxOnFFT[maxIndex][1]<0) {
				invertPhaseSign[maxIndex]=true;
				maxOnFFT[maxIndex][0]=-maxOnFFT[maxIndex][0];
				maxOnFFT[maxIndex][1]=-maxOnFFT[maxIndex][1];
			}
			interpolateXY [maxIndex][1] = (int) maxOnFFT[maxIndex][1];
			interpolateKxy[maxIndex][1] = maxOnFFT[maxIndex][1] - interpolateXY [maxIndex][1];
			if (maxOnFFT[maxIndex][0]<0) {
				interpolateXY  [maxIndex][0] = (int) (size+ maxOnFFT[maxIndex][0]);
				interpolateKxy [maxIndex][0] = (size+ maxOnFFT[maxIndex][0]) - interpolateXY[maxIndex][0];
			} else {
				interpolateXY  [maxIndex][0] = (int) maxOnFFT[maxIndex][0];
				interpolateKxy [maxIndex][0] = maxOnFFT[maxIndex][0] - interpolateXY[maxIndex][0];
			}
			for (int j=0;j<2;j++) {
				ix=(interpolateXY[maxIndex][0]+j) % size;
				for (int i=0;i<2;i++) {
					iy=interpolateXY[maxIndex][1]+i; // next: OOB 2147483647
					interpolateAmplitudes[maxIndex][i][j]= Math.sqrt(fft_complex[iy][ix][0]*fft_complex[iy][ix][0]+fft_complex[iy][ix][1]*fft_complex[iy][ix][1]);
					interpolatePhases[maxIndex][i][j]= Math.atan2((invertPhaseSign[maxIndex]?-1.0:1.0)*fft_complex[iy][ix][1], fft_complex[iy][ix][0]);
					if (this.debugLevel>5) {
						System.out.println("maxIndex="+maxIndex+" ix="+ix+" iy="+iy+" phase="+IJ.d2s(interpolatePhases[maxIndex][i][j],4)+" amplitude="+interpolateAmplitudes[maxIndex][i][j]);
					}
				}
			}
			phaseCorr=false;
			if ((interpolatePhases[maxIndex][0][0]> Math.PI/2) ||
					(interpolatePhases[maxIndex][0][1]> Math.PI/2) ||
					(interpolatePhases[maxIndex][1][0]> Math.PI/2) ||
					(interpolatePhases[maxIndex][1][1]> Math.PI/2) ||
					(interpolatePhases[maxIndex][0][0]<-Math.PI/2) ||
					(interpolatePhases[maxIndex][0][1]<-Math.PI/2) ||
					(interpolatePhases[maxIndex][1][0]<-Math.PI/2) ||
					(interpolatePhases[maxIndex][1][1]<-Math.PI/2)) {
				phaseCorr=true;
				interpolatePhases[maxIndex][0][0]+= (interpolatePhases[maxIndex][0][0]<0)?Math.PI:-Math.PI;
				interpolatePhases[maxIndex][0][1]+= (interpolatePhases[maxIndex][0][1]<0)?Math.PI:-Math.PI;
				interpolatePhases[maxIndex][1][0]+= (interpolatePhases[maxIndex][1][0]<0)?Math.PI:-Math.PI;
				interpolatePhases[maxIndex][1][1]+= (interpolatePhases[maxIndex][1][1]<0)?Math.PI:-Math.PI;
				if (this.debugLevel>5) {
					System.out.println("Shifting phases by PI/2 before averaging (to avoid rollover)");
				}
			}

			maxPhases[maxIndex]=       interpolateKxy[maxIndex][1] *(interpolateKxy[maxIndex][0] * interpolatePhases[maxIndex][1][1] + (1.0-interpolateKxy[maxIndex][0])* interpolatePhases[maxIndex][1][0])+
1929
					(1.0 - interpolateKxy[maxIndex][1])*(interpolateKxy[maxIndex][0] * interpolatePhases[maxIndex][0][1] + (1.0-interpolateKxy[maxIndex][0])* interpolatePhases[maxIndex][0][0]);
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
			if (phaseCorr) maxPhases[maxIndex]+=(maxPhases[maxIndex]<0)?Math.PI:-Math.PI;
			if (this.debugLevel>5) {
				System.out.println("kx="+IJ.d2s(interpolateKxy [maxIndex][0],4)+ " ky="+IJ.d2s(interpolateKxy [maxIndex][1],4));
			}
			if (this.debugLevel>2) {
				System.out.println("maxIndex="+maxIndex+" phase="+IJ.d2s(maxPhases[maxIndex],4));
			}
		}
		return maxPhases;
	}

	/* ======================================================================== */
	// zero is in the center
	private int [][] findFirst2MinOnCorrelation(
1944 1945 1946
			double [] pixels,
			double    min_half_period, // from white to black
			double    max_half_period) { // from white to black
1947 1948 1949 1950
		double min_fract_period = 0.3; //second minimum should be not closer to the first than this fraction of the first
		// distance from zero
		int size = (int) Math.sqrt(pixels.length);
		int size2 = size >>1;
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
				if (max_half_period <= 0) max_half_period = size;
				if (min_half_period <  1) min_half_period = 1;
				double mn2 = min_half_period*min_half_period;
				double mx2 = max_half_period*max_half_period;
				int [][] mins={{0,0},{0,0}};
				double mn = 0.0; // minimums are always negative
				// find the first minimum
				for (int row = 0; row <= size2; row++) {
					double dy2 = (row-size2)*(row-size2);
					for (int col = 0; col < size; col++) {
						double d2 = (col-size2)*(col-size2) + dy2;
						if ((d2 >= mn2) && (d2 < mx2) && (pixels[row * size + col] < mn)) {
1963
							mn = pixels[row * size + col];
1964 1965
							mins[0][0] = col-size2;
							mins[0][1] = row-size2; // always <=0
1966 1967 1968
						}
					}
				}
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
				if (mn ==0.0) {
					return null; // could not find a first minimum
				}
				// find the second minimum (not too close to the first
				int x0 = mins[0][0]+size2;
				int y0 = mins[0][1]+size2;
				int x1 = size - x0;
				int y1 = size - y0;
				mn = 0.0;
				double mn2a = Math.max(mn2, min_fract_period*( mins[0][0]* mins[0][0]+mins[0][1]*mins[0][1]));

				for (int row = 0; row <= size2; row++) {
					double dy2 = (row-size2)*(row-size2);
					for (int col = 0; col < size; col++) {
						double d2 = (col-size2)*(col-size2) + dy2;
						if ((d2 >= mn2) && (d2 < mx2) && (pixels[row * size + col] < mn)) {
							// verify it is far enough from the first minimum and it's mirror
							d2 = (row - y0)*(row -y0)+(col - x0)*(col - x0);
							if (d2 >= 2*mn2a) { // diagonal
								// verify it is far enough from the mirrored first
								d2 = (row - y1)*(row -y1)+(col - x1)*(col - x1);
								if (d2 >= 2 * mn2a) { // diagonal
									mn = pixels[row * size + col];
									mins[1][0] = col-size2;
									mins[1][1] = row-size2; // always <=0
								}
							}
						}
					}
				}
				if (mn ==0.0) {
					return null; // could not find a second minimum
				}
				return mins;
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
	}
	private int rad_search(int [][]  half_periods) {
		int ahp;
		int amx;
		int dist = -1;
		for (int n = 0; n < half_periods.length; n++) {
			amx = 0;
			for (int i = 0; i < half_periods[0].length; i++) {
				ahp = half_periods[n][i];
				if (ahp < 0) ahp=-ahp;
				if (ahp > amx) amx = ahp;
			}
			if ((dist <0) || (dist > amx)) dist = amx;
		}
		if (dist < 3) return 1;
		return (dist -1) >> 1;
	}

	private boolean isLocalMinMax(double [] pixels, int size, int indx, boolean need_min) {
		int sizesize = size*size;
2023 2024 2025 2026 2027 2028
		if ((indx < 0) || (indx >= sizesize)){
			if (this.debugLevel > 0) {
				System.out.println("isLocalMinMax(): bad indx = "+indx);
			}
			return false;
		}
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
		for (int dy = -size; dy<=size; dy +=size) {
			for (int dx = -1; dx <=1; dx++) {
				int indx1 = indx + dy +dx;
				if ((indx1 >= 0) && (indx1 < sizesize)){
					if (need_min) {
						if (pixels[indx] > pixels[indx1]) return false;
					} else {
						if (pixels[indx] < pixels[indx1]) return false;
					}
				}
			}
		}
		return true;
	}

	private double [][] getPeriodsFromCorrelation(
			double [] pixels,
			int [][]  half_periods, // a pair of half-periods
			double    min_frac,     // minimal fraction of the center maximum to consider
			boolean   no_crazy ){   // discard min/max with failed interpolation
2049
		// how far to look around expected maximum/minimum
2050 2051 2052 2053 2054
		double [][] rslt = null;
		int dbg_threshold = 3;
		int      search_radius = rad_search (half_periods);
		// below first >= second
		int [][] search_order = {{1,0}, // just half periods, negative
2055 2056 2057 2058
				{1,1}, // diagonals, positive
				{2,0}, // full period, positive
				{2,1}, // 8 (4) directions, negative
				{2,2}}; // positive
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
		int size = (int) Math.sqrt(pixels.length);
		int size2 = size/2;
		int [][] aper = new int [2][2];
		double [][] dhalf_periods = new double [2][2]; // will refine  half_periods;
		for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) aper[i][j] = Math.abs(half_periods[i][j]);
		for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) dhalf_periods[i][j] =  half_periods[i][j];
		double min_avalue = pixels[size2*(size+1)]*min_frac; // minimal absolute value of min/max
		int max_dist = size2 - 2; //arbitrary ??
		int nord =  0; // to preserve after loop
		layers_iteration:
			for (nord = 0; nord < search_order.length; nord++) {
				int [] xy_ind= search_order[nord];
				// 1 check if too far to fit
				if (    ((xy_ind[0] * aper[0][0] + xy_ind[1] * aper[1][0]) > max_dist ) ||
						((xy_ind[0] * aper[0][1] + xy_ind[1] * aper[1][1]) > max_dist ) ||
						((xy_ind[1] * aper[0][0] + xy_ind[0] * aper[1][0]) > max_dist ) ||
						((xy_ind[1] * aper[0][1] + xy_ind[0] * aper[1][1]) > max_dist )
						) {
					break layers_iteration;
				}
				boolean are_mins = ((xy_ind[0] + xy_ind[1]) % 2) != 0;
				int nspots = ((xy_ind[1]==0) || (xy_ind[0]==xy_ind[1])) ? 2 : 4;
				//			double [][] predicted_centers = new double nsamples
				int [][] lin_comb = new int [nspots][2]; // number of half periods (incl. negative) to combine
				if (xy_ind[1]==0) {
					lin_comb[0][0] = xy_ind[0];  // first spot, first vector
					lin_comb[0][1] = 0;          // first spot, second vector
					lin_comb[1][0] = 0;          // second spot, first vector
					lin_comb[1][1] = xy_ind[0];  // second spot, second vector
				} else if (xy_ind[0]==xy_ind[1]){
					lin_comb[0][0] =  xy_ind[0]; // first spot, first vector
					lin_comb[0][1] =  xy_ind[0]; // first spot, second vector
					lin_comb[1][0] =  xy_ind[0]; // second spot, first vector
					lin_comb[1][1] = -xy_ind[0]; // second spot, second vector
				} else {
					lin_comb[0][0] =  xy_ind[0]; // first spot,  first vector
					lin_comb[0][1] =  xy_ind[1]; // first spot,  second vector
					lin_comb[1][0] =  xy_ind[0]; // second spot, first vector
					lin_comb[1][1] = -xy_ind[1]; // second spot, second vector
					lin_comb[2][0] =  xy_ind[1]; // third spot,  first vector
					lin_comb[2][1] =  xy_ind[0]; // third spot,  second vector
					lin_comb[3][0] = -xy_ind[1]; // fourth spot, first vector
					lin_comb[3][1] =  xy_ind[0]; // fourth spot, second vector
				}
				double [][] spot_centers = new double[nspots][2];
				// iterate through all min/max spots in a layer, break outer loop on any failure (and use previous layer data)
				for (int nspot = 0; nspot < nspots; nspot++) {
					double [] xyc = {
							lin_comb[nspot][0]*dhalf_periods[0][0] + lin_comb[nspot][1]*dhalf_periods[1][0],
							lin_comb[nspot][0]*dhalf_periods[0][1] + lin_comb[nspot][1]*dhalf_periods[1][1]};
					int [] ixyc = {(int) Math.round(xyc[0]),(int) Math.round(xyc[1])};
2110 2111 2112
					if ((ixyc[0] < 0) || (ixyc[1] < 0) || (ixyc[0] >= size) || (ixyc[0] >= size)) {
						break layers_iteration;
					}
2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
					int [] ixym = ixyc.clone();
					for (int y = ixyc[1] - search_radius; y <= ixyc[1] + search_radius; y ++ ) if ((y >= -max_dist) && (y < max_dist)) {
						for (int x = ixyc[0] - search_radius; x <= ixyc[0] + search_radius; x ++ ) if ((x >= -max_dist) && (x < max_dist)) {
							double d = pixels[(y+size2)*size+(x+size2)] - pixels[(ixym[1]+size2)*size+(ixym[0]+size2)];
							if (are_mins) d= -d;
							if (d > 0) {
								ixym[0] = x;
								ixym[1] = y;
							}
						}
					}
					// verify it is local max/min
					if (!isLocalMinMax(pixels, size, (ixym[1]+size2)*size+(ixym[0]+size2), are_mins)) {
						if (this.debugLevel > dbg_threshold) {
2127
							System.out.println("getPeriodsFromCorrelation(): is not a local max/min, nord="+nord);
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
						}
						break layers_iteration;
					}
					// verify it is strong enough
					if (Math.abs(pixels[(ixym[1]+size2)*size+(ixym[0]+size2)]) < min_avalue) { // BUG: -8030
						if (this.debugLevel > dbg_threshold) {
							System.out.println("getPeriodsFromCorrelation(): min/max is too weak: "+
									pixels[(ixym[1]+size2)*size+(ixym[0]+size2)]+ " < "+ min_avalue+
									" (layer"+nord+")");
						}
						break layers_iteration;
					}

					//use quadratic interpolation to find min/max
					double [][][] data =new double [9][3][];
					int index=0;
					for (int dy = -1; dy <= 1; dy++) {
						for (int dx = -1; dx <= 1; dx++) {
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
							data[index][0] = new double [2];
							data[index][1]=     new double[1];
							data[index][2]=     new double[1];
							int pix_indx = (ixym[1] + dy + size2) * size + (ixym[0] + dx + size2);
							if ((pix_indx >= 0) && (pix_indx < pixels.length)) {
								data[index][0][0] = dx;
								data[index][0][1] = dy;
								data[index][1][0]=  are_mins ? -pixels[pix_indx] : pixels[pix_indx];
								data[index][2][0]=  1.0;
							} else {
								data[index][2][0]=  0.0;
							}
							index++;
						}
					}
					double [] corrXY=(new PolynomialApproximation()).quadraticMax2d (data);
					if (corrXY == null) {
						if (no_crazy) {
							if (this.debugLevel > dbg_threshold) {
								System.out.println("getPeriodsFromCorrelation(): interpolation failed,"+
										" (layer"+nord+")");
							}
							break layers_iteration;
2169
						}
2170 2171 2172
						corrXY=new double[2];
						corrXY[0] = 0.0;
						corrXY[1] = 0.0;
2173
					}
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
					// verify that interpolation did not go crazy. If it did - reset to center
					if ((corrXY[0] > 1.5) || (corrXY[1] > 1.5) || (corrXY[0] < -1.5) || (corrXY[1] < -1.5)) {
						if (no_crazy) {
							if (this.debugLevel > dbg_threshold) {
								System.out.println("getPeriodsFromCorrelation(): interpolation failed, "+
										"corrXY = ["+corrXY[0]+", "+corrXY[1]+"]  (layer"+nord+")");
							}
							break layers_iteration;
						}
						corrXY[0] = 0.0;
						corrXY[1] = 0.0;
					}

					spot_centers[nspot][0] = ixym[0] + corrXY[0];
					spot_centers[nspot][1] = ixym[1] + corrXY[1];
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
				}
				// refine half-period values
				if (xy_ind[1]==0) {
					dhalf_periods[0][0] = spot_centers[0][0]/xy_ind[0];
					dhalf_periods[0][1] = spot_centers[0][1]/xy_ind[0];
					dhalf_periods[1][0] = spot_centers[1][0]/xy_ind[0];
					dhalf_periods[1][1] = spot_centers[1][1]/xy_ind[0];
				} else if (xy_ind[0]==xy_ind[1]){
					dhalf_periods[0][0] = (spot_centers[0][0] + spot_centers[1][0])/2/xy_ind[0];
					dhalf_periods[0][1] = (spot_centers[0][1] + spot_centers[1][1])/2/xy_ind[0];
					dhalf_periods[1][0] = (spot_centers[0][0] - spot_centers[1][0])/2/xy_ind[0];
					dhalf_periods[1][1] = (spot_centers[0][1] - spot_centers[1][1])/2/xy_ind[0];
				} else {
					dhalf_periods[0][0] = (spot_centers[0][0] + spot_centers[1][0] + spot_centers[2][0] - spot_centers[3][0]) / (xy_ind[0] + xy_ind[1])/2;
					dhalf_periods[0][1] = (spot_centers[0][1] + spot_centers[1][1] + spot_centers[2][1] - spot_centers[3][1]) / (xy_ind[0] + xy_ind[1])/2;
					dhalf_periods[1][0] = (spot_centers[0][0] - spot_centers[1][0] + spot_centers[2][0] + spot_centers[3][0]) / (xy_ind[0] + xy_ind[1])/2;
					dhalf_periods[1][1] = (spot_centers[0][1] - spot_centers[1][1] + spot_centers[2][1] + spot_centers[3][1]) / (xy_ind[0] + xy_ind[1])/2;
				}
			} // for (int nord = 0; nord < search_order.length; nord++) {
2208 2209 2210
		//		double [][] pattern_diagonals = {
		//				{dhalf_periods[0][0]+dhalf_periods[1][0], dhalf_periods[0][1]+dhalf_periods[1][1]},
		//				{dhalf_periods[0][0]-dhalf_periods[1][0], dhalf_periods[0][1]-dhalf_periods[1][1]}};
2211 2212 2213 2214 2215
		double [][] scaled_v = new double [2][2];
		double k_scale = 4.0;
		for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) {
			scaled_v[i][j] = k_scale*dhalf_periods[i][j]; // not clear why 4 and not 2
		}
2216
		//		rslt =  vectToWaveVect(pattern_diagonals);
2217 2218 2219 2220 2221 2222 2223 2224
		// make vectors to have positive y:
		for (int i = 0; i < 2; i++) {
			if (scaled_v[i][1] <0) {
				scaled_v[i][0] = - scaled_v[i][0];
				scaled_v[i][1] = - scaled_v[i][1];
			}
		}
		rslt =  vectToWaveVect(scaled_v);
2225
		//		rslt = dhalf_periods;
2226 2227 2228 2229 2230 2231 2232 2233
		// sort so first vector to second vector will be clockwise (positive y is downwards)
		{int j=0, k=1;
		if ((rslt[j][0] * rslt[k][1] - rslt[k][0] * rslt[j][1]) < 0) {
			double [] tmp = rslt[0];
			rslt[0] = rslt[1];
			rslt[1] = tmp;
		}
		}
2234 2235 2236 2237 2238 2239
		for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) {
			if (Double.isNaN(rslt[i][j]) || Double.isInfinite(rslt[i][j])) {
				System.out.println("got NaN/Infinity, nord = "+nord);
				return null;
			}
		}		return rslt;
2240 2241 2242 2243 2244 2245
	}

	//vect[0][] - 1-st vector connecting nodes, vect[1][] - second vector
	// return [0][] - first wave vector, orthogonal to vecgt[0][], return [1][] - to the second vect[1][]
	double [][] vectToWaveVect(double [][] vect){
		double a = 1.0/(vect[0][0]*vect[1][1] -vect[0][1]*vect[1][0]);
2246 2247 2248
		//		double [] rv0 = {-vect[0][1],  vect[0][0]};
		//		double [] rv1 = { vect[1][1], -vect[1][0]};
		//		double [][] wv= {{a*rv0[0], a*rv0[1]},{a*rv1[0], a*rv1[1]}};
2249 2250 2251 2252 2253 2254
		double [][] wv= {{-a*vect[0][1], a*vect[0][0]}, {a*vect[1][1], -a*vect[1][0]}};
		return wv;
	}



Andrey Filippov's avatar
Andrey Filippov committed
2255
	private	int [][] findFirst2MaxOnSpectrum (double [][][] pixels, // complex, top half, starting from 0,0
Andrey Filippov's avatar
Andrey Filippov committed
2256
			/* May need to reduce the skip_around to be able to handle smaller number of pattern periods? Or re-try if failed? Guess somehow?*/
2257
			int skip_around, // skip +- from (0,0) and previous max
Andrey Filippov's avatar
Andrey Filippov committed
2258 2259 2260 2261 2262
			double minOrtho) { // 0.5 - 30deg. orthogonality of 2 vectors - 1.0 - perpendicular, 0.0 - parallel
		int [][] max2={{0,0},{0,0}};
		double thisMax=0.0;
		int x,y,sx1,sx2;
		double p,a;
Andrey Filippov's avatar
Andrey Filippov committed
2263
		/* find first point */
Andrey Filippov's avatar
Andrey Filippov committed
2264 2265 2266
		for (y=0;y<pixels.length;y++) for (x=0;x<pixels[0].length; x++ ) {
			p=pixels[y][x][0]*pixels[y][x][0]+pixels[y][x][1]*pixels[y][x][1];
			if (p>thisMax) {
Andrey Filippov's avatar
Andrey Filippov committed
2267
				if ((y<=skip_around) && ((x<=skip_around) || (x>=pixels[0].length-skip_around))) continue; /* too close to [0,0] */
Andrey Filippov's avatar
Andrey Filippov committed
2268 2269 2270 2271 2272 2273
				max2[0][0]=x;
				max2[0][1]=y;
				thisMax=p;
			}
		}
		thisMax=0.0;
Andrey Filippov's avatar
Andrey Filippov committed
2274 2275
		sx1=(max2[0][0]>(pixels[0].length/2))?(max2[0][0]-pixels[0].length):max2[0][0]; /* y is always positive here */
		/* find second point */
Andrey Filippov's avatar
Andrey Filippov committed
2276 2277 2278 2279
		// Maybe also check if it is a local maximum (not on the border with protected area (0, first point, y=0, ...) ?
		for (y=0;y<pixels.length;y++) for (x=0;x<pixels[0].length; x++ ) {
			p=pixels[y][x][0]*pixels[y][x][0]+pixels[y][x][1]*pixels[y][x][1];
			if (p>thisMax) {
Andrey Filippov's avatar
Andrey Filippov committed
2280
				/* Is this a local maximum? */
Andrey Filippov's avatar
Andrey Filippov committed
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
				if (y>0) {
					if (                             p< (pixels[y-1][x  ][0]*pixels[y-1][x  ][0]+pixels[y-1][x  ][1]*pixels[y-1][x  ][1]))  continue;
					if ((x>0) &&                    (p< (pixels[y-1][x-1][0]*pixels[y-1][x-1][0]+pixels[y-1][x-1][1]*pixels[y-1][x-1][1]))) continue;
					if ((x<(pixels[0].length-1)) && (p< (pixels[y-1][x+1][0]*pixels[y-1][x+1][0]+pixels[y-1][x+1][1]*pixels[y-1][x+1][1]))) continue;
				}
				if (y< (pixels.length-1)) {
					if (                             p< (pixels[y+1][x  ][0]*pixels[y+1][x  ][0]+pixels[y+1][x  ][1]*pixels[y+1][x  ][1]))  continue;
					if ((x>0) &&                    (p< (pixels[y+1][x-1][0]*pixels[y+1][x-1][0]+pixels[y+1][x-1][1]*pixels[y+1][x-1][1]))) continue;
					if ((x<(pixels[0].length-1)) && (p< (pixels[y+1][x+1][0]*pixels[y+1][x+1][0]+pixels[y+1][x+1][1]*pixels[y+1][x+1][1]))) continue;
				}
				if ((x>0) &&                      (p< (pixels[y  ][x-1][0]*pixels[y  ][x-1][0]+pixels[y  ][x-1][1]*pixels[y  ][x-1][1]))) continue;
				if ((x<(pixels[0].length-1)) &&   (p< (pixels[y  ][x+1][0]*pixels[y  ][x+1][0]+pixels[y  ][x+1][1]*pixels[y  ][x+1][1]))) continue;
				if ((y<=skip_around) && ((x<=skip_around) || (x>=pixels[0].length-skip_around))) {
					if (this.debugLevel>5) {
						System.out.println("rejecting point ["+x+","+y+"] it is too close to [0,0]");
					}
Andrey Filippov's avatar
Andrey Filippov committed
2297
					continue; /* too close to [0,0] */
Andrey Filippov's avatar
Andrey Filippov committed
2298 2299 2300 2301 2302 2303
				}

				if ((y<=skip_around) && ((x<=skip_around) || (x>=pixels[0].length-skip_around))) {
					if (this.debugLevel>5) {
						System.out.println("rejecting point ["+x+","+y+"] it is too close to [0,0]");
					}
Andrey Filippov's avatar
Andrey Filippov committed
2304
					continue; /* too close to [0,0] */
Andrey Filippov's avatar
Andrey Filippov committed
2305 2306 2307
				}
				if (((y<=(max2[0][1]+skip_around)) && (y>=(max2[0][1]-skip_around))) &&
						(((x<=(max2[0][0]+skip_around)) && (x>=(max2[0][0]-skip_around))) ||
2308
								(x>=(max2[0][0]+pixels[0].length-skip_around)) ||
Andrey Filippov's avatar
Andrey Filippov committed
2309 2310 2311 2312
								(x<=(max2[0][0]-pixels[0].length+skip_around)))) {
					if (this.debugLevel>5) {
						System.out.println("rejecting point ["+x+","+y+"] as it is too close to the first one - ["+max2[0][0]+"("+sx1+"),"+max2[0][1]+"]");
					}
Andrey Filippov's avatar
Andrey Filippov committed
2313
					continue; /* too close to first maximum */
Andrey Filippov's avatar
Andrey Filippov committed
2314 2315 2316 2317
				}
				sx2=(x>(pixels[0].length/2))?(x-pixels[0].length):x;
				a=(sx1*y -max2[0][1]*sx2);
				a=a*a/(sx1*sx1+max2[0][1]*max2[0][1])/(sx2*sx2+y*y);
Andrey Filippov's avatar
Andrey Filippov committed
2318
				if (a < (minOrtho*minOrtho)) { /* vectors are too close to parallel */
Andrey Filippov's avatar
Andrey Filippov committed
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
					if (this.debugLevel>5) {
						System.out.println("rejecting point ["+x+"("+sx2+"),"+y+"] as the vector is too close to the first one - ["+max2[0][0]+"("+sx1+"),"+max2[0][1]+"]");
						System.out.println("pixels.length="+pixels.length+" pixels[0].length="+pixels[0].length);
					}
					continue;
				}
				max2[1][0]=x;
				max2[1][1]=y;
				thisMax=p;
			}
		}
		if (  thisMax==0.0) {
2331
			/*TODO: THat really happens on the real data */
2332
			System.out.println("Failed to find a second maximum");
Andrey Filippov's avatar
Andrey Filippov committed
2333 2334 2335 2336 2337 2338
			return null;
		}
		if (max2[0][0]>(pixels[0].length/2)) max2[0][0]-=pixels[0].length;
		if (max2[1][0]>(pixels[0].length/2)) max2[1][0]-=pixels[0].length;
		return max2;
	}
Andrey Filippov's avatar
Andrey Filippov committed
2339 2340
	/* ======================================================================== */
	/* Can it handle negative y if the refined maximum goes there? (maximal value on positive Y) */
2341 2342
	private double[][] findFirst2MaxOnCorrelation(
			double [] pixels,
Andrey Filippov's avatar
Andrey Filippov committed
2343 2344
			int [][] startPoints,
			PatternDetectParameters patternDetectParameters
2345
			) {
Andrey Filippov's avatar
Andrey Filippov committed
2346 2347 2348 2349
		double reasonbleFrequency=2.0; // reject frequencies below that
		int size =(int) Math.sqrt (pixels.length);
		int [][] imax =startPoints.clone();
		int [][] imax2 =new int [2*patternDetectParameters.multiplesToTry][2];
2350
		boolean  []maxDefined=new boolean [2*patternDetectParameters.multiplesToTry];  //.multiplesToTry = 4;
Andrey Filippov's avatar
Andrey Filippov committed
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369

		double  [] maxValues =new double [startPoints.length];
		double  [] max2Values =new double [2];
		double [][] max2 =new double [2*patternDetectParameters.multiplesToTry][2];
		int lim=size/2-2; // safety measure
		int nmax=0;
		int x=1;
		int y=0;
		int indx;
		int [] dirs = {-1, -size-1, -size, -size+1, 1, size+1, size, size-1};

		boolean isMax;
		int i,j,k, xmn,xmx,ymn,ymx;
		int halfSize=size/2;
		double [] vlengths={Math.sqrt(startPoints[0][0]*startPoints[0][0]+startPoints[0][1]*startPoints[0][1]),
				Math.sqrt(startPoints[1][0]*startPoints[1][0]+startPoints[1][1]*startPoints[1][1])};
		boolean tooFar=false;
		int numVect;

Andrey Filippov's avatar
Andrey Filippov committed
2370
		/* Look for the maximal values around startPoints (+/-diff_spectr_corr )*/
Andrey Filippov's avatar
Andrey Filippov committed
2371
		for (nmax=0; nmax<startPoints.length; nmax++) {
2372 2373
			ymn=imax[nmax][1]-patternDetectParameters.diffSpectrCorr; // .diffSpectrCorr=2
			ymx=imax[nmax][1]+patternDetectParameters.diffSpectrCorr;
Andrey Filippov's avatar
Andrey Filippov committed
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
			if (ymx>lim) ymx=lim;
			xmn=imax[nmax][0]-patternDetectParameters.diffSpectrCorr;
			if (xmn<-lim) xmx=-lim;
			xmx=imax[nmax][0]+patternDetectParameters.diffSpectrCorr;
			if (xmx>lim) xmx=lim;
			indx=(size+1)*size/2 + imax[nmax][1] *size+imax[nmax][0];
			if ((Math.abs(imax[nmax][0])>lim) || (Math.abs(imax[nmax][1])>lim)) {
				if (this.debugLevel>2) {
					System.out.println("Bad start point  imax[0][0]="+imax[0][0]+" imax[0][1]="+imax[0][1]+" imax[1][0]="+imax[1][0]+" imax[1][1]="+imax[1][1]+ " lim="+lim);
				}
				return null;
			}
			//       if ((indx<0) || (indx>=pixels.length)) {
			//          System.out.println(" imax[0][0]="+imax[0][0]+" imax[0][1]="+imax[0][1]+" imax[1][0]="+imax[1][0]+" imax[1][1]="+imax[1][1]+ " lim="+lim);
			//       }
			maxValues[nmax]=pixels[indx];
			for (y=ymn;y<=ymx;y++) for (x=xmn;x<=xmx; x++) {
				indx=(size+1)*size/2 + y *size+x;
				if (pixels[indx]>maxValues[nmax]) {
Andrey Filippov's avatar
Andrey Filippov committed
2393
					/* Make sure it is closer to this point than to any other or [0,0] */
Andrey Filippov's avatar
Andrey Filippov committed
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
					tooFar=false;
					for (numVect=0;numVect<startPoints.length;numVect++) {
						if (Math.abs((x-imax[nmax][0])*imax[numVect][0]+(y-imax[nmax][1])*imax[numVect][1])>0.5*vlengths[nmax]*vlengths[numVect]) {
							tooFar=true;
							break;
						}
					}
					if (tooFar) {
						if (this.debugLevel>5) {
							System.out.println("rejecting point ["+x+","+y+"] as the vector is closer to other max than ["+imax[nmax][0]+","+imax[nmax][0]+"]"+
									" in the (+/-) direction: ["+imax[numVect][0]+","+imax[numVect][0]+"]");
						}
						continue;
					}
					maxValues[nmax]=pixels[indx];
					imax[nmax][0]=x;
					imax[nmax][1]=y;
				}
			}
Andrey Filippov's avatar
Andrey Filippov committed
2413
			/* Make sure the maximum in the scanned area is also a local maximum */
Andrey Filippov's avatar
Andrey Filippov committed
2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
			isMax=true;
			indx=(size+1)*size/2 + imax[nmax][1] *size+imax[nmax][0];
			for (j=0;j<7;j++) if (pixels[indx]<pixels[indx+dirs[j]]) {
				isMax=false;
				break;
			}
			if (!isMax) {
				if (this.debugLevel>2) {
					System.out.println("This should not happen:");
					System.out.println("Maximum is not a local maximum - BUG or consider changing patternDetectParameters.diffSpectrCorr="+patternDetectParameters.diffSpectrCorr);
					System.out.println("point #"+(nmax+1)+" (of 2), x0="+startPoints[nmax][0]+" y0="+startPoints[nmax][1]+ " x="+imax[nmax][0]+" y="+imax[nmax][1]);
				}
Andrey Filippov's avatar
Andrey Filippov committed
2426
				/* Maybe return from here with null?*/
Andrey Filippov's avatar
Andrey Filippov committed
2427 2428 2429 2430 2431 2432
				while (!isMax && (indx>0) && (indx>pixels.length)) {
					isMax=true;
					for (j=0;j<7;j++) if (pixels[indx]<pixels[indx+dirs[j]]) {
						isMax=false;
						indx+=dirs[j];
						imax[nmax][1]=(indx / size) - halfSize;
2433
						imax[nmax][0]=(indx % size) - halfSize;
Andrey Filippov's avatar
Andrey Filippov committed
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
						break;
					}
				}
				if (!isMax) {
					if (this.debugLevel>2) {
						System.out.println("Maximum still not reached, bailing out");
						System.out.println("point #"+(nmax+1)+" (of 2), x0="+startPoints[nmax][0]+" y0="+startPoints[nmax][1]+ " x="+imax[nmax][0]+" y="+imax[nmax][1]);
					}
					return null;
				} else {
					if (this.debugLevel>2) {
						System.out.println("point #"+(nmax+1)+" (of 2), corrected local maximum is  x="+imax[nmax][0]+" y="+imax[nmax][1]);
					}
				}

			}
		}
Andrey Filippov's avatar
Andrey Filippov committed
2451
		/* Sort maximums so first vector to second vector will be clockwise (positive y is downwards) */
Andrey Filippov's avatar
Andrey Filippov committed
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
		j=0; k=1;
		if ((imax[j][0]*imax[k][1]-imax[k][0]*imax[j][1])<0) {
			j=1;k=0;
		}
		imax2[0][0]=imax[j][0];
		imax2[0][1]=imax[j][1];
		imax2[1][0]=imax[k][0];
		imax2[1][1]=imax[k][1];


Andrey Filippov's avatar
Andrey Filippov committed
2462
		/* Now define maximal radius of cluster (~0.7 of the average distance from 0,0 to the 2 start points */
Andrey Filippov's avatar
Andrey Filippov committed
2463 2464 2465 2466 2467 2468 2469
		int maxX2Y2=0;
		for (i=0;i<2;i++) for (j=0;j<2;j++) maxX2Y2+=imax2[i][j]*imax2[i][j];
		maxX2Y2/=4;
		//System.out.println("maxX2Y2="+maxX2Y2);

		for (i=0;i<2*patternDetectParameters.multiplesToTry;i++)  maxDefined[i]=(i<2);

Andrey Filippov's avatar
Andrey Filippov committed
2470
		nmax=2*patternDetectParameters.multiplesToTry; /* but only the first two are known by now */
Andrey Filippov's avatar
Andrey Filippov committed
2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
		for (i=0;i<2;i++) {
			if (this.debugLevel>5) System.out.println("i="+i+" x="+imax2[i][0]+" y="+imax2[i][1]+" value="+max2Values[i]);
		}

		int clusterNumber;
		List <Integer> pixelList=new ArrayList<Integer>(100);
		Integer Index, NewIndex, HillIndex;
		double cx,cy,cm,minInCluster,f;

		int []clusterMap=new int[pixels.length];
		for (i=0;i<clusterMap.length;i++) clusterMap[i]=0; /// 0 - unused, -1 - "do not use"
		int listIndex;
		boolean noHills;
		int clusterSize;
		boolean isLocalMax;
		int pair;
		for (clusterNumber=0;clusterNumber<nmax;clusterNumber++) {
			pair=clusterNumber/2+1;
			if (!maxDefined[clusterNumber] && maxDefined[clusterNumber-2]) {
Andrey Filippov's avatar
Andrey Filippov committed
2490
				/* We do not know the seed for this maximum, but the previous (of the same direction) may be known */
Andrey Filippov's avatar
Andrey Filippov committed
2491 2492 2493 2494
				x= (int) (max2[clusterNumber-2][0]*pair)/(pair-1);
				y= (int) (max2[clusterNumber-2][1]*pair)/(pair-1);
				if ((x>(-lim+1)) && (x<(lim-1)) && (y>(-lim+1)) && (y<(lim-1))) {
					Index=(y+halfSize)*size + x+halfSize;
Andrey Filippov's avatar
Andrey Filippov committed
2495
					/* there should be local maximum not more than "deviationSteps" steps from the x,y */
Andrey Filippov's avatar
Andrey Filippov committed
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
					isLocalMax=false;
					i=patternDetectParameters.deviationSteps;
					while ((i>0) && !isLocalMax) {
						isLocalMax=true;
						for (j=0;j<dirs.length;j++) {
							NewIndex=Index+dirs[j];
							if ((NewIndex>=0) && (NewIndex<clusterMap.length) && (pixels[NewIndex]>pixels[Index])) {
								isLocalMax=false;
								Index=NewIndex;
								i--;
								if (this.debugLevel>5) System.out.println("i="+i+" x="+((Index % size) - halfSize)+" y="+((Index / size) - halfSize)+" value="+pixels[Index]);
								break;
							}
						}
					}
					if (isLocalMax && (clusterMap[Index]==0)) { // not yet used
						imax2[clusterNumber][0]= (Index % size) - halfSize;
						imax2[clusterNumber][1]= (Index / size) - halfSize;
2514
						maxDefined[clusterNumber]=true;
Andrey Filippov's avatar
Andrey Filippov committed
2515 2516 2517 2518
					}
				}
			}
			if (maxDefined[clusterNumber]) { // skip if seed for the cluster is not defined
Andrey Filippov's avatar
Andrey Filippov committed
2519
				/* Grow cluster around maximum, find centroid */
Andrey Filippov's avatar
Andrey Filippov committed
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529
				Index=(imax2[clusterNumber][1]+halfSize)*size + imax2[clusterNumber][0]+halfSize;
				pixelList.clear();
				pixelList.add (Index);
				clusterMap[Index]=clusterNumber+1;
				listIndex=0;
				while (listIndex<pixelList.size() ) {
					Index=pixelList.get(listIndex++);
					for (j=0;j<dirs.length;j++) {
						NewIndex=Index+dirs[j];
						if ((NewIndex>=0) && (NewIndex<clusterMap.length) && (clusterMap[NewIndex]==0) && (pixels[NewIndex]<pixels[Index])) {
2530
							/* did we get too far?*/
Andrey Filippov's avatar
Andrey Filippov committed
2531 2532 2533 2534
							y=(NewIndex/size) - halfSize - imax2[clusterNumber][1];
							x=(NewIndex % size) - halfSize - imax2[clusterNumber][0];
							//System.out.println(" dy="+y+" dx="+x+" dx*dx+dy*dy="+(x*x+y*y));
							if ((x*x+y*y) <= maxX2Y2) {
Andrey Filippov's avatar
Andrey Filippov committed
2535
								/* See if there is any neighbor of the new pixel that is higher and not yet marked (prevent rivers flowing between hills) */
Andrey Filippov's avatar
Andrey Filippov committed
2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
								noHills=true;
								for (k=0;k<dirs.length;k++) {
									HillIndex=NewIndex+dirs[k];
									if ((HillIndex>=0) && (HillIndex<clusterMap.length) && (clusterMap[HillIndex]!=(clusterNumber+1)) && (pixels[HillIndex]>pixels[NewIndex])) {
										noHills=false;
										break;
									}
								}
								if (noHills) {
									pixelList.add (NewIndex);
									clusterMap[NewIndex]=clusterNumber+1;
									//System.out.println("NewIndex="+NewIndex+" y="+(NewIndex/size - halfSize)+" x="+((NewIndex % size) - halfSize)+" new pixel="+pixels[NewIndex]+" old pixel="+pixels[Index]);
								}
							}
						}
					}
				}
Andrey Filippov's avatar
Andrey Filippov committed
2553
				/* Shrink clusters to a fraction of initial size */
Andrey Filippov's avatar
Andrey Filippov committed
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576
				// TODO: shring to a value (between min and max) if there is a sharp maximum??
				//, double patternDetectParameters.shrinkClusters
				if (patternDetectParameters.shrinkClusters==0.0) { // use "smart" size
					clusterSize=(int) Math.sqrt(5* pixelList.size()); // use proportional size
				} else if (patternDetectParameters.shrinkClusters<0) {
					clusterSize=(int)(- patternDetectParameters.shrinkClusters ); // use specified size
				} else {
					clusterSize=(int) (pixelList.size()*patternDetectParameters.shrinkClusters); // use proportional size
				}
				if (clusterSize<5) clusterSize=5;
				while (pixelList.size()>clusterSize) {
					i=0;
					f=pixels[pixelList.get(i)];
					for (j=1;j<pixelList.size();j++) if (pixels[pixelList.get(j)]<f){
						i=j;
						f=pixels[pixelList.get(j)];
					}
					clusterMap[pixelList.get(i)]=-1; // Do not use looking for the next cluster
					pixelList.remove(i);
				}



Andrey Filippov's avatar
Andrey Filippov committed
2577
				/* now find centroid of the cluster */
Andrey Filippov's avatar
Andrey Filippov committed
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
				minInCluster=pixels[pixelList.get(0)];
				for (i=1;i<pixelList.size();i++) if (minInCluster>pixels[pixelList.get(i)]) minInCluster = pixels[pixelList.get(i)];
				cx=0.0; cy=0.0; cm=0.0;
				for (i=0;i<pixelList.size();i++) {
					j=pixelList.get(i);
					y=j / size - halfSize;
					x=j % size - halfSize;
					f=pixels[j]-minInCluster;
					cm+=f;
					cx+=f*x;
					cy+=f*y;
				}
				cx/=cm;
				cy/=cm;
				max2[clusterNumber][0]=cx;
				max2[clusterNumber][1]=cy;
				f=0.0;
				if (pair>1) {
					cx=max2[clusterNumber-2][0]*pair/(pair-1)-max2[clusterNumber][0];
					cy=max2[clusterNumber-2][1]*pair/(pair-1)-max2[clusterNumber][1];
					f=Math.sqrt(cx*cx+cy*cy);
Andrey Filippov's avatar
Andrey Filippov committed
2599
					/* Verify deviation here */
Andrey Filippov's avatar
Andrey Filippov committed
2600 2601 2602 2603 2604
					if (f>patternDetectParameters.deviation) maxDefined[clusterNumber]=false;
				}
				if (this.debugLevel>6) System.out.println("pixelList.size()="+pixelList.size()+" centroid sum="+cm);
				if (this.debugLevel>5) System.out.println("clusterNumber="+clusterNumber+" x="+max2[clusterNumber][0]+" y="+max2[clusterNumber][1] + " x0="+(max2[clusterNumber][0]/pair)+" y0="+(max2[clusterNumber][1]/pair)+" deviat="+f);
				if ((cm==0.0) || (pixelList.size()<3)) maxDefined[clusterNumber]=false;
Andrey Filippov's avatar
Andrey Filippov committed
2605
				/* Filter out unreasonably low frequencies*/
Andrey Filippov's avatar
Andrey Filippov committed
2606 2607 2608 2609 2610 2611
				if ((max2[clusterNumber][0]*max2[clusterNumber][0]+max2[clusterNumber][1]*max2[clusterNumber][1])<(reasonbleFrequency*reasonbleFrequency)) {
					if (this.debugLevel>2) System.out.println("Frequency too low:clusterNumber="+clusterNumber+" x="+max2[clusterNumber][0]+" y="+max2[clusterNumber][1]+ ", minimal allowed frequency is "+reasonbleFrequency);
					maxDefined[clusterNumber]=false;
				}
			}
		}
Andrey Filippov's avatar
Andrey Filippov committed
2612
		/* Average (or just use farthest?) multiple maximums */
Andrey Filippov's avatar
Andrey Filippov committed
2613 2614
		if (this.debugLevel>2){
			float [] dbg_pixels=new float[clusterMap.length];
2615
			for (j=0;j<dbg_pixels.length;j++) dbg_pixels[j]=clusterMap[j];
Andrey Filippov's avatar
Andrey Filippov committed
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643
			dbg_pixels[(size+1)*size/2]=-1; // mark center

			ImageProcessor ip=new FloatProcessor(size,size);
			ip.setPixels(dbg_pixels);
			ip.resetMinAndMax();
			ImagePlus imp=  new ImagePlus("clusters", ip);
			imp.show();
		}

		//    for (i=0;i<2;i++) for (j=0;j<2;j++) max2[i][j]=imax2[i][j];
		double [][]maxFinal=new double[2][2];
		boolean [] definedFinal={false,false};
		for (i=0;i<2;i++) {
			maxFinal[i][0]=0.0;
			maxFinal[i][1]=0.0;
			j=0;
			for (pair=0;pair<nmax/2;pair++) {
				if (maxDefined[i+2*pair]) {
					j++;
					maxFinal[i][0]+=max2[i+2*pair][0]/(pair+1);
					maxFinal[i][1]+=max2[i+2*pair][1]/(pair+1);
					definedFinal[i]=true;
					//          System.out.println("i="+i+" pair="+pair+" j="+j+" maxFinal["+i+"][0]="+maxFinal[i][0]+" maxFinal["+i+"][1]="+maxFinal[i][1]);
				}
			}
			maxFinal[i][0]/=j;
			maxFinal[i][1]/=j;
			if (j==0) definedFinal[i]=false;
Andrey Filippov's avatar
Andrey Filippov committed
2644
			/* These two vectors correspond to checker diagonals and they are calculated for the correlation space.
Andrey Filippov's avatar
Andrey Filippov committed
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
 Actual frequencies for the checker board will be 1/4 of these, also divide by FFT size so the result will be in cycles per pixel */
			maxFinal[i][0]/=size*4;
			maxFinal[i][1]/=size*4;
		}
		if (this.debugLevel>2) {
			System.out.println("Checkerboard frequency[0]  x="+IJ.d2s(maxFinal[0][0],4)+" y="+IJ.d2s(maxFinal[0][1],4));
			System.out.println("Checkerboard frequency[1]  x="+IJ.d2s(maxFinal[1][0],4)+" y="+IJ.d2s(maxFinal[1][1],4));
			//      System.out.println();
		}
		if (!definedFinal[0] || !definedFinal[1]) {
			if (this.debugLevel>2) {
				System.out.println("Undefined frequency(ies)");
			}
			return null;
		}
		return maxFinal;
	}
Andrey Filippov's avatar
Andrey Filippov committed
2662
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
	private double [] findCheckerPhases(double [][] WVectors, double [] P) {
		double [][] DWVectors = {{WVectors[0][0]-WVectors[1][0],WVectors[0][1]-WVectors[1][1]},
				{WVectors[0][0]+WVectors[1][0],WVectors[0][1]+WVectors[1][1]}};
		if (this.debugLevel>3)  System.out.println("      DWVectors[0][0]="+IJ.d2s(DWVectors[0][0],4)+"  DWVectors[0][1]="+IJ.d2s(DWVectors[0][1],4));
		if (this.debugLevel>3)  System.out.println("      DWVectors[1][0]="+IJ.d2s(DWVectors[1][0],4)+"  DWVectors[1][1]="+IJ.d2s(DWVectors[1][1],4));
		double [] DWVectorsAbs2={DWVectors[0][0]*DWVectors[0][0]+DWVectors[0][1]*DWVectors[0][1],
				DWVectors[1][0]*DWVectors[1][0]+DWVectors[1][1]*DWVectors[1][1]};
		if (this.debugLevel>3)  System.out.println("      sqrt(DWVectorsAbs2[0])="+IJ.d2s(Math.sqrt(DWVectorsAbs2[0]),4)+"  sqrt(DWVectorsAbs2[1])="+IJ.d2s(Math.sqrt(DWVectorsAbs2[1]),4));
		double [][] DL= {{DWVectors[0][0]/DWVectorsAbs2[0],DWVectors[0][1]/DWVectorsAbs2[0]},
				{DWVectors[1][0]/DWVectorsAbs2[1],DWVectors[1][1]/DWVectorsAbs2[1]}};
		if (this.debugLevel>3)  System.out.println("      DL[0][0]="+IJ.d2s(DL[0][0],4)+"  DL[0][1]="+IJ.d2s(DL[0][1],4));
		if (this.debugLevel>3)  System.out.println("      DL[1][0]="+IJ.d2s(DL[1][0],4)+"  DL[1][1]="+IJ.d2s(DL[1][1],4));

		double v= (DL[0][0]*(DL[1][0]*P[1]-DL[0][0]*P[0]) +  DL[0][1]*(DL[1][1]*P[1]-DL[0][1]*P[0]))/(DL[0][0]*DL[1][1]-DL[0][1]*DL[1][0])/(2*Math.PI);
		if (this.debugLevel>2)  System.out.println("v="+IJ.d2s(v,4));


		double [] WC={-DL[1][0] * P[1] / (2*Math.PI) + DL[1][1] * v,
				-DL[1][1] * P[1] / (2*Math.PI) - DL[1][0] * v};
		if (this.debugLevel>2)  System.out.println("WC[0]="+IJ.d2s(WC[0],4)+"  WC[1]="+IJ.d2s(WC[1],4));

		double [] phases={-2*Math.PI *(WC[0] * WVectors[0][0] + WC[1] * WVectors[0][1]),
				-2*Math.PI *(WC[0] * WVectors[1][0] + WC[1] * WVectors[1][1])};
		if (this.debugLevel>2)  System.out.println("phases[0]="+IJ.d2s(phases[0],4)+"  phases[1]="+IJ.d2s(phases[1],4));
		return phases;
	}
Andrey Filippov's avatar
Andrey Filippov committed
2689
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
	private	boolean matchPatterns(double [][][] patterns, double [][] targetPattern) {
		int n,i,j;
		double [][] sp=new double[2][2];
		double [] target_lengths= new double[2];
		double [] current_lengths=new double[2];
		double [] swap_wv;
		double    swap;
		boolean noCorrectionWasNeeded=true;
		if (patterns==null) return false;
		if (targetPattern==null) return false;
		for (n=0;n<patterns.length; n++) {
			for (i=0;i<2;i++) {
				target_lengths[i]= Math.sqrt(targetPattern[i][0]*targetPattern[i][0]+targetPattern[i][1]*targetPattern[i][1]);
				current_lengths[i]=Math.sqrt(patterns[n][i][0]*patterns[n][i][0]+patterns[n][i][1]*patterns[n][i][1]);

			}
			for (i=0;i<2;i++) for (j=0;j<2;j++){
				sp[i][j]=    (patterns[n][i][0]*targetPattern[j][0]+patterns[n][i][1]*targetPattern[j][1])/current_lengths[i]/ target_lengths[j];
			}
Andrey Filippov's avatar
Andrey Filippov committed
2709
			/* Swap vectors regardless of sign (parallel/anti-parallel)*/
Andrey Filippov's avatar
Andrey Filippov committed
2710 2711 2712 2713 2714 2715 2716
			if ((Math.abs(sp[0][0])<Math.abs(sp[0][1])) || (Math.abs(sp[0][0])<Math.abs(sp[1][0]))) {
				noCorrectionWasNeeded=false;
				if (this.debugLevel>0) System.out.println("Swapped wave vectors in quadrant "+n);
				swap_wv=patterns[n][0];   patterns[n][0]=patterns[n][1]; patterns[n][1]=swap_wv;
				swap=sp[0][0];            sp[0][0]=sp[1][0];             sp[1][0]=swap;
				swap=sp[0][1];            sp[0][1]=sp[1][1];             sp[1][1]=swap;
			}
Andrey Filippov's avatar
Andrey Filippov committed
2717
			/* Now correct vector signs if needed */
Andrey Filippov's avatar
Andrey Filippov committed
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741
			for (i=0;i<2;i++) {
				if (sp[i][i] <0) {
					noCorrectionWasNeeded=false;
					if (this.debugLevel>0) System.out.println("Changing wave vector "+(i+1)+" direction in quadrant "+n);
					for (j=0;j<patterns[n][i].length;j++) patterns[n][i][j]=-patterns[n][i][j]; /// Will negate phase if available
				}
			}
		}
		return noCorrectionWasNeeded;
	}
	/*	** converts 2 wave vectors (WVx,WVy,phase) into two checker pattern vectors (VPx,VPy, phase)
	phase is in the point x=0,y=0*/
	private	double [][] waveVectorsToPatternVectors(double [] wv0, double [] wv1) {
		double [][] v=new double [2][3];
		double vect_wv0_x_wv1=wv0[0]*wv1[1]-wv0[1]*wv1[0];
		//v[0][0]= wv0[1]/vect_wv0_x_wv1;
		//v[0][1]=-wv0[0]/vect_wv0_x_wv1;
		//v[1][0]=-wv1[1]/vect_wv0_x_wv1;
		//v[1][1]= wv1[0]/vect_wv0_x_wv1;

		v[0][0]= wv1[1]/vect_wv0_x_wv1;
		v[0][1]=-wv1[0]/vect_wv0_x_wv1;
		v[1][0]=-wv0[1]/vect_wv0_x_wv1;
		v[1][1]= wv0[0]/vect_wv0_x_wv1;
2742 2743
		if (wv0.length>2) v[0][2]=wv0[2];
		if (wv1.length>2) v[1][2]=wv1[2];
Andrey Filippov's avatar
Andrey Filippov committed
2744
		/* "white square" center had coordinates
Andrey Filippov's avatar
Andrey Filippov committed
2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
	-wv0[0]
		 */
		return v;
	}
	/**
	  Matching non-linear mesh using wave vectors in the centers of four quadrants, phases are ignored here
	  processes wave vectors for the four quadrants (top-left,tr,bl,br) and calculates second degree polynominal correction
	  X1= Ax*X^2 + Bx*Y^2 + 2Cx*X*Y
	  Y1= Ay*X^2 + By*Y^2 + 2Cy*X*Y
	  where X and Y are normalized so top left corner is (-1,-1), top right - (+1,-1) , bottom left - (-1,+1), bottom right - (+1,+1)
	  returns array of 6 elements {Ax,Bx,Cx,Ay,By,Cy}
	 */
	private	double [] calcPatternNonLinear(double [][][] qp) {
		int iq;
Andrey Filippov's avatar
Andrey Filippov committed
2759
		/* Calculate center WV */
Andrey Filippov's avatar
Andrey Filippov committed
2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807

		double [][][] mCorners=    new double [4][][] ; // only two 2x2 are needed, other two - just for debugging
		mCorners[0]= matrixToConvertTwoPairs(qp[0][0], qp[0][1], qp[4][0], qp[4][1]);
		mCorners[1]= matrixToConvertTwoPairs(qp[1][0], qp[1][1], qp[4][0], qp[4][1]);
		mCorners[2]= matrixToConvertTwoPairs(qp[2][0], qp[2][1], qp[4][0], qp[4][1]);
		mCorners[3]= matrixToConvertTwoPairs(qp[3][0], qp[3][1], qp[4][0], qp[4][1]);

		/**
	x,y - image coordinates (distorted), with no center shift (later use effective radius)
	x1,y1 - linear mesh

	x1=x+Ax*x^2+Bx*y^2+2*Cx*x*y+Dx*x+Ex*y
	y1=y+Ay*x^2+By*y^2+2*Cy*x*y+Dy*x+Ey*y

	dx1/dx=1+2*Ax*x+2*Cx*y+Dx
	dx1/dy=0+2*Cx*x+2*Bx*y+Ex

	dy1/dx=0+2*Ay*x+2*Cy*y+Dy
	dy1/dy=1+2*Cy*x+2*By*y+Ey

	| dx1 | = |  1+2*Ax*x+2*Cx*y+Dx     2*Cx*x+2*Bx*y+Ex | * | dx |
	| dy1 |   |    2*Ay*x+2*Cy*y+Dy   1+2*Cy*x+2*By*y+Ey |   | dy |

	for x=-1,y=-1
	M[0]=|  1-2*Ax-2*Cx+Dx    -2*Cx-2*Bx+Ex |
	   |   -2*Ay-2*Cy+Dy   1-2*Cy-2*By+Ey |

	for x=+1,y=-1
	M[1]=|  1+2*Ax-2*Cx+Dx    +2*Cx-2*Bx+Ex |
	   |   +2*Ay-2*Cy+Dy   1+2*Cy-2*By+Ey |

	for x=-1,y=+1
	M[2]=|  1-2*Ax+2*Cx+Dx    -2*Cx+2*Bx+Ex |
	   |   -2*Ay+2*Cy+Dy   1-2*Cy+2*By+Ey |

	for x=+1,y=+1
	M[2]=|  1+2*Ax+2*Cx+Dx    +2*Cx+2*Bx+Ex |
	   |   +2*Ay+2*Cy+Dy   1+2*Cy+2*By+Ey |


		 */
		double [] det=  {mCorners[0][0][0]*mCorners[0][1][1]-mCorners[0][0][1]*mCorners[0][1][0],
				mCorners[1][0][0]*mCorners[1][1][1]-mCorners[1][0][1]*mCorners[1][1][0],
				mCorners[2][0][0]*mCorners[2][1][1]-mCorners[2][0][1]*mCorners[2][1][0],
				mCorners[3][0][0]*mCorners[3][1][1]-mCorners[3][0][1]*mCorners[3][1][0]};

		double [][][] M={{{ mCorners[0][1][1]/det[0], -mCorners[0][1][0]/det[0]},
			{-mCorners[0][0][1]/det[0],  mCorners[0][0][0]/det[0]}},
2808
				{{ mCorners[1][1][1]/det[1], -mCorners[1][1][0]/det[1]},
Andrey Filippov's avatar
Andrey Filippov committed
2809 2810 2811
				{-mCorners[1][0][1]/det[1],  mCorners[1][0][0]/det[1]}},
				{{ mCorners[2][1][1]/det[2], -mCorners[2][1][0]/det[2]},
					{-mCorners[2][0][1]/det[2],  mCorners[2][0][0]/det[2]}},
2812
				{{ mCorners[3][1][1]/det[3], -mCorners[3][1][0]/det[3]},
Andrey Filippov's avatar
Andrey Filippov committed
2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876
						{-mCorners[3][0][1]/det[3],  mCorners[3][0][0]/det[3]}}};
		/**
	 Overdefined - 16 equations for 10 unknowns
	1   M[0][0][0]=1-2*Ax-2*Cx+Dx
	2   M[0][0][1]= -2*Cx-2*Bx+Ex
	3   M[0][1][0]= -2*Ay-2*Cy+Dy
	4   M[0][1][1]=1-2*Cy-2*By+Ey
	5   M[1][0][0]=1+2*Ax-2*Cx+Dx
	6   M[1][0][1]=  2*Cx-2*Bx+Ex
	7   M[1][1][0]=  2*Ay-2*Cy+Dy
	8   M[1][1][1]=1+2*Cy-2*By+Ey

	9   M[2][0][0]=1-2*Ax+2*Cx+Dx
	10   M[2][0][1]= -2*Cx+2*Bx+Ex
	11   M[2][1][0]= -2*Ay+2*Cy+Dy
	12   M[2][1][1]=1-2*Cy+2*By+Ey
	13   M[3][0][0]=1+2*Ax+2*Cx+Dx
	14   M[3][0][1]=  2*Cx+2*Bx+Ex
	15   M[3][1][0]=  2*Ay+2*Cy+Dy
	16   M[3][1][1]=1+2*Cy+2*By+Ey

	( 1)   M[0][0][0]=1-2*Ax     -2*Cx                +Dx
	( 2)   M[0][0][1]=      -2*Bx-2*Cx                      +Ex
	( 3)   M[0][1][0]=                -2*Ay     -2*Cy    +Dy
	( 4)   M[0][1][1]=1                    -2*By-2*Cy          +Ey
	( 5)   M[1][0][0]=1+2*Ax     -2*Cx                +Dx
	( 6)   M[1][0][1]=      -2*Bx+2*Cx                      +Ex
	( 7)   M[1][1][0]=                +2*Ay     -2*Cy    +Dy
	( 8)   M[1][1][1]=1                    -2*By+2*Cy          +Ey

	( 9)   M[0][0][0]=1-2*Ax     +2*Cx                +Dx
	(10)   M[0][0][1]=      +2*Bx-2*Cx                      +Ex
	(11)   M[0][1][0]=                -2*Ay     +2*Cy    +Dy
	(12)   M[0][1][1]=1                    +2*By-2*Cy          +Ey
	(13)   M[1][0][0]=1+2*Ax     +2*Cx                +Dx
	(14)   M[1][0][1]=      +2*Bx+2*Cx                      +Ex
	(15)   M[1][1][0]=                +2*Ay     +2*Cy    +Dy
	(16)   M[1][1][1]=1                    +2*By+2*Cy          +Ey

	( 1)+( 5)+( 9)+(13)                     Dx=( M[0][0][0]+M[1][0][0]+M[2][0][0]+M[3][0][0])/4-1
	( 2)+( 6)+(10)+(14)                     Ex=( M[0][0][1]+M[1][0][1]+M[2][0][1]+M[3][0][1])/4
	( 3)+( 7)+(11)+(15)                     Dy=( M[0][1][0]+M[1][1][0]+M[2][1][0]+M[3][1][0])/4
	( 4)+( 8)+(12)+(16)                     Ey=( M[0][1][1]+M[1][1][1]+M[2][1][1]+M[3][1][1])/4-1

	-( 1)+( 5)-( 9)+(13)                     Ax=(-M[0][0][0]+M[1][0][0]-M[2][0][0]+M[3][0][0])/8
	-( 2)-( 6)+(10)+(14)                     Bx=(-M[0][0][1]-M[1][0][1]+M[2][0][1]+M[3][0][1])/8
	-( 2)+( 6)-(10)+(14)-( 1)-( 5)+( 9)+(13) Cx=(-M[0][0][1]+M[1][0][1]-M[2][0][1]+M[3][0][1]-M[0][0][0]-M[1][0][0]+M[2][0][0]+M[3][0][0])/16
	-( 3)+( 7)-(11)+(15)                     Ay=(-M[0][1][0]+M[1][1][0]-M[2][1][0]+M[3][1][0])/8
	-( 4)-( 8)+(12)+(16)                     By=(-M[0][1][1]-M[1][1][1]+M[2][1][1]+M[3][1][1])/8
	-( 4)+( 8)-(12)+(16)-( 3)-( 7)+(11)+(15) Cy=(-M[0][1][1]+M[1][1][1]-M[2][1][1]+M[3][1][1]-M[0][1][0]-M[1][1][0]+M[2][1][0]+M[3][1][0])/16
		 */

		double [] rslt = {(-M[0][0][0]+M[1][0][0]-M[2][0][0]+M[3][0][0])/8, // Ax
				(-M[0][0][1]-M[1][0][1]+M[2][0][1]+M[3][0][1])/8, // Bx
				(-M[0][0][1]+M[1][0][1]-M[2][0][1]+M[3][0][1]-M[0][0][0]-M[1][0][0]+M[2][0][0]+M[3][0][0])/16,  // Cx
				(-M[0][1][0]+M[1][1][0]-M[2][1][0]+M[3][1][0])/8,  // Ay
				(-M[0][1][1]-M[1][1][1]+M[2][1][1]+M[3][1][1])/8,  // By
				(-M[0][1][1]+M[1][1][1]-M[2][1][1]+M[3][1][1]-M[0][1][0]-M[1][1][0]+M[2][1][0]+M[3][1][0])/16,   // Cy
				( M[0][0][0]+M[1][0][0]+M[2][0][0]+M[3][0][0])/4-1, // Dx
				( M[0][0][1]+M[1][0][1]+M[2][0][1]+M[3][0][1])/4,   // Ex
				( M[0][1][0]+M[1][1][0]+M[2][1][0]+M[3][1][0])/4,   // Dy
				( M[0][1][1]+M[1][1][1]+M[2][1][1]+M[3][1][1])/4-1  // Ey
		};

Andrey Filippov's avatar
Andrey Filippov committed
2877
		if (this.debugLevel>2) { /* increase LEVEL later */
Andrey Filippov's avatar
Andrey Filippov committed
2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896
			/*          System.out.println("Center   "+
	                           " W0x="+     IJ.d2s(centerWV[0][0],4)+
	                           " W0y="+     IJ.d2s(centerWV[0][1],4)+
	                           " W1x="+     IJ.d2s(centerWV[1][0],4)+
	                           " W1y="+     IJ.d2s(centerWV[1][1],4));*/
			for (iq=0; iq<4;iq++) {
				System.out.println("Matrix= "+iq+
						" M00="+     IJ.d2s(mCorners[iq][0][0],4)+
						" M01="+     IJ.d2s(mCorners[iq][0][1],4)+
						" M10="+     IJ.d2s(mCorners[iq][1][0],4)+
						" M11="+     IJ.d2s(mCorners[iq][1][1],4));
			}
			for (iq=0; iq<4;iq++) {
				System.out.println(" M["+iq+"][0][0]="+  IJ.d2s(M[iq][0][0],4)+
						" M["+iq+"][0][1]="+  IJ.d2s(M[iq][0][1],4)+
						" M["+iq+"][1][0]="+  IJ.d2s(M[iq][1][0],4)+
						" M["+iq+"][1][1]="+  IJ.d2s(M[iq][1][1],4));
			}
		}
Andrey Filippov's avatar
Andrey Filippov committed
2897
		if (this.debugLevel>2) { /* increase LEVEL later */
Andrey Filippov's avatar
Andrey Filippov committed
2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
			System.out.println("Corr   "+
					" Ax="+     IJ.d2s(rslt[0],5)+
					" Bx="+     IJ.d2s(rslt[1],5)+
					" Cx="+     IJ.d2s(rslt[2],5)+
					" Ay="+     IJ.d2s(rslt[3],5)+
					" By="+     IJ.d2s(rslt[4],5)+
					" Cy="+     IJ.d2s(rslt[5],5)+
					" Dx="+     IJ.d2s(rslt[6],5)+
					" Ex="+     IJ.d2s(rslt[7],5)+
					" Dy="+     IJ.d2s(rslt[8],5)+
					" Ey="+     IJ.d2s(rslt[9],5));
		}
		return rslt;
	};
Andrey Filippov's avatar
Andrey Filippov committed
2912 2913 2914
	/* ======================================================================== */
	/* TODO: REPLACE doubleFHT  */
	/* converts FHT results (frequency space) to complex numbers of [fftsize/2+1][fftsize] */
Andrey Filippov's avatar
Andrey Filippov committed
2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
	private double[][][] FHT2FFTHalf (FHT fht, int fftsize) {
		float[] fht_pixels=(float[])fht.getPixels();
		double[][][] fftHalf=new double[(fftsize>>1)+1][fftsize][2];
		int row1,row2,col1,col2;

		for (row1=0;row1<=(fftsize>>1);row1++) {
			row2=(fftsize-row1) %fftsize;
			for (col1=0;col1<fftsize;col1++) {
				col2=(fftsize-col1) %fftsize;
				fftHalf[row1][col1][0]=   0.5*(fht_pixels[row1*fftsize+col1] + fht_pixels[row2*fftsize+col2]);
				fftHalf[row1][col1][1]=   0.5*(fht_pixels[row2*fftsize+col2] - fht_pixels[row1*fftsize+col1]);
			}
		}
		return fftHalf;
	}
Andrey Filippov's avatar
Andrey Filippov committed
2930
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944
	public double[][][] FHT2FFTHalf (double [] fht_pixels, int fftsize) {
		double[][][] fftHalf=new double[(fftsize>>1)+1][fftsize][2];
		int row1,row2,col1,col2;

		for (row1=0;row1<=(fftsize>>1);row1++) {
			row2=(fftsize-row1) %fftsize;
			for (col1=0;col1<fftsize;col1++) {
				col2=(fftsize-col1) %fftsize;
				fftHalf[row1][col1][0]=   0.5*(fht_pixels[row1*fftsize+col1] + fht_pixels[row2*fftsize+col2]);
				fftHalf[row1][col1][1]=   0.5*(fht_pixels[row2*fftsize+col2] - fht_pixels[row1*fftsize+col1]);
			}
		}
		return fftHalf;
	}
Andrey Filippov's avatar
Andrey Filippov committed
2945 2946
	/* ======================================================================== */
	/* converts FFT arrays of complex numbers of [fftsize/2+1][fftsize] to FHT arrays */
Andrey Filippov's avatar
Andrey Filippov committed
2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
	private float[] floatFFTHalf2FHT (double [][][] fft, int fftsize) {
		float[] fht_pixels=new float [fftsize*fftsize];
		int row1,row2,col1,col2;
		for (row1=0;row1<=(fftsize>>1);row1++) {
			row2=(fftsize-row1) %fftsize;
			for (col1=0;col1 < fftsize;col1++) {
				col2=(fftsize-col1) %fftsize;
				fht_pixels[row1*fftsize+col1]=(float)(fft[row1][col1][0]-fft[row1][col1][1]);
				fht_pixels[row2*fftsize+col2]=(float)(fft[row1][col1][0]+fft[row1][col1][1]);
			}
		}
		return fht_pixels;
	}
Andrey Filippov's avatar
Andrey Filippov committed
2960
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
	public	 double[][] normalizeAndWindow (double [][] pixels, double [] windowFunction) {
		return normalizeAndWindow (pixels, windowFunction, true);
	}
	private	 double[] normalizeAndWindow (double [] pixels, double [] windowFunction) {
		return normalizeAndWindow (pixels, windowFunction, true);
	}
	private	 double[][] normalizeAndWindow (double [][] pixels, double [] windowFunction, boolean removeDC) {
		int i;
		for (i=0;i<pixels.length;i++)  if (pixels[i]!=null) pixels[i]=normalizeAndWindow (pixels[i],  windowFunction, removeDC);
		return pixels;
	}
	private	 double[] normalizeAndWindow (double [] pixels, double [] windowFunction, boolean removeDC) {
		int j;
		if (pixels==null) return null;
		double s=0.0,s0=0.0;
		if (removeDC) {
			for (j=0;j<pixels.length;j++){
				s+=pixels[j]*windowFunction[j];
				s0+=windowFunction[j];
2980

Andrey Filippov's avatar
Andrey Filippov committed
2981 2982 2983 2984 2985 2986
			}
			s/=s0;
		}
		for (j=0;j<pixels.length;j++) pixels[j]=(pixels[j]-s)*windowFunction[j];
		return pixels;
	}
Andrey Filippov's avatar
Andrey Filippov committed
2987
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024
	public	double [][] matrix2x2_invert(double [][] m ){
		double det=m[0][0]*m[1][1]-m[0][1]*m[1][0];
		double [][] rslt= {{ m[1][1]/det,  -m[0][1]/det},
				{-m[1][0]/det,   m[0][0]/det}};
		return rslt;
	}
	public	double [][] matrix2x2_mul(double [][] a, double [][] b ){
		double [][] rslt={{a[0][0]*b[0][0]+a[0][1]*b[1][0], a[0][0]*b[0][1]+a[0][1]*b[1][1]},
				{a[1][0]*b[0][0]+a[1][1]*b[1][0], a[1][0]*b[0][1]+a[1][1]*b[1][1]}};
		return rslt;
	}
	public	double []   matrix2x2_mul(double [][] a, double [] b ){
		double [] rslt={a[0][0]*b[0]+a[0][1]*b[1],
				a[1][0]*b[0]+a[1][1]*b[1]};
		return rslt;
	}
	public	double [][] matrix2x2_scale(double [][] a, double  b ){
		double [][] rslt={{a[0][0]*b, a[0][1]*b},
				{a[1][0]*b, a[1][1]*b}};
		return rslt;
	}
	public	double [] matrix2x2_scale(double [] a, double  b ){
		double [] rslt={a[0]*b, a[1]*b};
		return rslt;
	}
	public	double [][] matrix_add(double [][] a, double [][]  b ){
		double [][] rslt= new double [a.length][a[0].length];
		int i,j;
		for (i=0;i<rslt.length;i++) for (j=0;j<rslt[0].length;j++) rslt[i][j]=a[i][j]+b[i][j];
		return rslt;
	}
	public	double []   vector_add(double [] a, double [] b ){
		double [] rslt= new double [a.length];
		int i;
		for (i=0;i<rslt.length;i++) rslt[i]=a[i]+b[i];
		return rslt;
	}
Andrey Filippov's avatar
Andrey Filippov committed
3025
	/* calculates 2x2 matrix that converts two pairs of vectors: u2=M*u1, v2=M*v1*/
Andrey Filippov's avatar
Andrey Filippov committed
3026 3027 3028
	private	double [][] matrixToConvertTwoPairs(double [] u1, double [] v1, double [] u2, double [] v2) {
		double [][] rslt= {{(u2[0]*v1[1]-v2[0]*u1[1])/(u1[0]*v1[1]-v1[0]*u1[1]),
			(v2[0]*u1[0]-u2[0]*v1[0])/(u1[0]*v1[1]-v1[0]*u1[1])},
3029
				{(u2[1]*v1[1]-v2[1]*u1[1])/(u1[0]*v1[1]-v1[0]*u1[1]),
Andrey Filippov's avatar
Andrey Filippov committed
3030 3031 3032 3033 3034 3035
				(v2[1]*u1[0]-u2[1]*v1[0])/(u1[0]*v1[1]-v1[0]*u1[1])}};
		return rslt;
	}

	public double [][] matrix2x2_add(double [][] a, double [][] b ){
		double [][] rslt={{a[0][0]+b[0][0], a[0][1]+b[0][1]},
3036
				{a[1][0]+b[1][0], a[1][1]+b[1][1]}};
Andrey Filippov's avatar
Andrey Filippov committed
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046
		return rslt;
	}

	public double [] matrix2x2_add(double [] a, double [] b ){
		double [] rslt={a[0]+b[0], a[1]+b[1]};
		return rslt;
	}

	public int [][] matrix2x2_add(int [][] a, int [][] b ){
		int [][] rslt={{a[0][0]+b[0][0], a[0][1]+b[0][1]},
3047
				{a[1][0]+b[1][0], a[1][1]+b[1][1]}};
Andrey Filippov's avatar
Andrey Filippov committed
3048 3049 3050 3051 3052 3053 3054 3055 3056 3057
		return rslt;
	}

	public int [] matrix2x2_add(int [] a, int [] b ){
		int [] rslt={a[0]+b[0], a[1]+b[1]};
		return rslt;
	}

	public double [][] matrix2x2_transp(double [][] m ){
		double [][] rslt= {{ m[0][0],  m[1][0]},
3058
				{ m[0][1],  m[1][1]}};
Andrey Filippov's avatar
Andrey Filippov committed
3059 3060
		return rslt;
	}
3061

Andrey Filippov's avatar
Andrey Filippov committed
3062
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
	private	 double [] combineDiagonalGreens (double [] green0, double []green3, int half_width, int half_height) {
		int y,x,base;
		int base_b=0;
		double [] result= new double [green0.length];
		for (y=0;y<half_height/2; y++){
			base=half_height*half_width/2+ y* (half_width+1);
			for (x=0; x<half_width/2; x++) {
				result[base_b++]=green0[base];
				base-=half_width;
				result[base_b++]=green3[base++];
			}
			base=half_height*half_width/2+ y* (half_width+1);
			for (x=0; x<half_width/2; x++) {
				//System.out.println("2:y="+y+" x="+x+" base_b="+base_b+" base="+base);
				result[base_b++]=green3[base++];
				result[base_b++]=green0[base];
				base-=half_width;
			}
		}
		return result;
	}
Andrey Filippov's avatar
Andrey Filippov committed
3084
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109
	public double[] initWindowFunction(int size, double gaussWidth) {
		return initWindowFunction(size, gaussWidth, 0);
	}
	public double[] initWindowFunction(int size, double gaussWidth, int zeros) {
		double [] windowFunction =new double [size*size];
		double [] windowFunction_line=new double [size];
		double a,k;
		int i,j;
		int size1=size-zeros;
		int i0=(zeros+1)/2;
		if (gaussWidth==0) {
			for (i=0; i<size; i++) windowFunction_line[i]= 1.0;
		} else if (gaussWidth<0) {
			for (i=0; i<size1; i++) windowFunction_line[i+i0]= (0.54-0.46*Math.cos((i*2.0*Math.PI)/size1));
		} else {
			k=2.0/(size*gaussWidth);
			for (i=i0; i<i0+size1; i++) {
				a=(i-size/2)*k;
				windowFunction_line[i]= Math.exp( - a*a);
				if (zeros>0) windowFunction_line[i]*=(0.54-0.46*Math.cos(((i-i0)*2.0*Math.PI)/size1)); // additionally multiply by Hamming
			}
		}
		if (zeros>0){ // make window to be exact zero for certain number of samples (for correlation)
			for (i=0;i<i0;i++) windowFunction_line[i]=0.0;
			for (i=i0+size1;i<size;i++) windowFunction_line[i]=0.0;
3110 3111


Andrey Filippov's avatar
Andrey Filippov committed
3112 3113 3114 3115 3116 3117
		}
		for (i=0; i<size; i++) for (j=0; j<size; j++){
			windowFunction[size*i+j]=windowFunction_line[i]*windowFunction_line[j];
		}
		return windowFunction;
	}
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171
	/* ============================= Distortions ===================================*/
	public void distortionsTest (
			final DistortionParameters distortionParameters, //
			final MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			final	SimulationPattern.SimulParameters  simulParameters,
			final boolean equalizeGreens,
			final ImagePlus imp, // image to process
			final int threadsMax,
			final boolean updateStatus,
			final int debug_level){// debug level used inside loops

		if (imp==null) return;

		Roi roi= imp.getRoi();
		final Rectangle selection;
		if (roi==null){
			selection=new Rectangle(0, 0, imp.getWidth(), imp.getHeight());
		} else {
			selection=roi.getBounds();
		}
		MatchSimulatedPattern matchSimulatedPattern=new MatchSimulatedPattern(distortionParameters.FFTSize);
		matchSimulatedPattern.debugLevel=debugLevel;
		MatchSimulatedPattern matchSimulatedPatternCorr=new MatchSimulatedPattern(distortionParameters.correlationSize);
		matchSimulatedPatternCorr.debugLevel=debugLevel;
		final SimulationPattern.SimulParameters  thisSimulParameters=simulParameters.clone();
		thisSimulParameters.subdiv=     distortionParameters.patternSubdiv;
		thisSimulParameters.bPatternSigma=distortionParameters.bPatternSigma;
		thisSimulParameters.barraySigma=distortionParameters.barraySigma;
		SimulationPattern simulationPattern= new SimulationPattern(thisSimulParameters);
		final double [] bPattern= simulationPattern.patternGenerator(simulParameters); // reuse pattern for next time
		//find center of the selection (to be used to find initial pattern approximation)
		if (debugLevel>2)	System.out.println("bPattern.length="+bPattern.length);
		int xc,yc;
		xc=2*((2*selection.x+selection.width+1)/4);
		yc=2*((2*selection.y+selection.height+1)/4);
		Rectangle initialPatternCell=new Rectangle(xc-distortionParameters.FFTSize,
				yc-distortionParameters.FFTSize,
				2*distortionParameters.FFTSize,2*distortionParameters.FFTSize);
		//create diagonal green selection around xc,yc
		double [][] input_bayer=splitBayer (imp,initialPatternCell,equalizeGreens);
		if (debugLevel>2) SDFA_INSTANCE.showArrays(input_bayer,  true, "selection-bayer-distortionsTest");
		double [] windowFunction=initWindowFunction(distortionParameters.FFTSize, distortionParameters.fftGaussWidth);
		final double [] windowFunctionCorr=initWindowFunction(distortionParameters.correlationSize,distortionParameters.correlationGaussWidth,distortionParameters.zeros);
		double [] greens=normalizeAndWindow (input_bayer[4], windowFunction);

		double [][] pattern=matchSimulatedPattern.findPattern(
				null, // 			DoubleFHT doubleFHT,
				greens,
				distortionParameters.FFTSize,
				patternDetectParameters,
				patternDetectParameters.minGridPeriod/2,
				patternDetectParameters.maxGridPeriod/2,
				true, // this is a pattern for combined greens (diagonal), adjust results accordingly
				"Pattern"); // title - will not be used
Andrey Filippov's avatar
Andrey Filippov committed
3172 3173
		if (pattern==null) {
			System.out.println("Error - pattern not found");
3174 3175
			IJ.showMessage("Error","Failed to find pattern");
			return;
Andrey Filippov's avatar
Andrey Filippov committed
3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
		}
		if (debugLevel>2) System.out.println("FX1="+pattern[0][0]+"  FY1="+pattern[0][1]+"  phase1="+pattern[0][2]);
		if (debugLevel>2) System.out.println("FX2="+pattern[1][0]+"  FY2="+pattern[1][1]+"  phase2="+pattern[1][2]);
		double [] ll2=new double[2];
		double [][] dxy=new double[2][2];
		double [][] phases=new double[2][2];
		int i,j,k;
		for (i=0;i<2;i++) {
			ll2[i]=(pattern[i][0]*pattern[i][0]+pattern[i][1]*pattern[i][1])*2*Math.PI;
		}

		if (debugLevel>2) System.out.println("phase1/2pi="+(pattern[0][2]/2/Math.PI)+"  phase2/2pi="+(pattern[1][2]/2/Math.PI));
		for (k=0;k<2;k++) {
3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201
			for (j=0;j<2;j++) {
				phases[k][j]= pattern[j][2]-Math.PI/2+((k>0)? Math.PI:0.0);
				while (phases[k][j]<-Math.PI) phases[k][j]+=2*Math.PI;
				while (phases[k][j]>Math.PI)  phases[k][j]-=2*Math.PI;
			}
			if (debugLevel>2) System.out.println("phase1/2pi="+(phases[k][0]/2/Math.PI)+"  phase2/2pi="+(phases[k][1]/2/Math.PI));
			for (i=0;i<2;i++) {
				dxy[k][i]=0;
				for (j=0;j<2;j++) {
					dxy[k][i]+=(phases[k][j])*pattern[j][i]/ll2[j];
				}
			}
			if (debugLevel>2) System.out.println("dX["+k+"]="+dxy[k][0]+" dY["+k+"]="+dxy[k][1]);
Andrey Filippov's avatar
Andrey Filippov committed
3202 3203 3204 3205
		}
		int phaseSel=((dxy[0][0]*dxy[0][0]+dxy[0][1]*dxy[0][1])<(dxy[1][0]*dxy[1][0]+dxy[1][1]*dxy[1][1]))?0:1;
		if (debugLevel>1) System.out.println("xc="+xc+" yc="+yc);
		if (debugLevel>1) System.out.println("dX="+dxy[phaseSel][0]+" dY="+dxy[phaseSel][1]);
3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310
		double [] centerXY0={xc-dxy[phaseSel][0],yc-dxy[phaseSel][1]};
		if (debugLevel>1) System.out.println("+++ Initial center x="+IJ.d2s(centerXY0[0],3)+" y="+ 	IJ.d2s(centerXY0[1],3));

		// debug mode - scan correlation around center point, show result and exit:
		SDFA_INSTANCE.showArrays(simulationPattern.bPattern, "bPattern");
		//			 double [] barray= new double [simulationPattern.barray.length*simulationPattern.barray[0].length];
		//			 for (i=0;i<barray.length;i++) {
		//				 barray[i]=simulationPattern.barray[i/simulationPattern.barray[0].length][i % simulationPattern.barray[0].length];
		//			 }
		//			 SDFA_INSTANCE.showArrays(barray, simulationPattern.barray[0].length, simulationPattern.barray.length,"barray");
		SDFA_INSTANCE.showArrays(simulationPattern.barray, "barray");

		double [][][] scanXY=scanPatternCrossLocation(
				distortionParameters.correlationDx, // range
				(int) Math.round(distortionParameters.correlationDx/distortionParameters.correlationDy)+1,
				centerXY0, // initial coordinates of the pattern cross point
				pattern[0][0],
				pattern[0][1],
				pattern[1][0],
				pattern[1][1],
				imp,                       // image data (Bayer mosaic)
				distortionParameters,      //
				patternDetectParameters,
				matchSimulatedPatternCorr, // correlationSize
				thisSimulParameters,
				equalizeGreens,
				windowFunctionCorr,        // window function
				simulationPattern,
				false, // if true - invert pattern
				null); // will create new instance of DoubleFHT class
		double [][] scanImg= new double [4][scanXY.length*scanXY[0].length];
		//			  System.out.println("scanImg[0].length="+scanImg[0].length);
		for (i=0;i<scanImg[0].length;i++) {
			scanImg[0][i]=scanXY[i/scanXY[0].length][i % scanXY[0].length][0];
			scanImg[1][i]=scanXY[i/scanXY[0].length][i % scanXY[0].length][1];
			scanImg[2][i]=scanXY[i/scanXY[0].length][i % scanXY[0].length][2];
			scanImg[3][i]=scanXY[i/scanXY[0].length][i % scanXY[0].length][3];
		}
		SDFA_INSTANCE.showArrays(scanImg, true, "scan_correlation");
		SDFA_INSTANCE.showArrays(scanImg, false,"scan_correlation");
		return;
	}
	/*
	 *  Try point x,y, test for pattern, return x,y, contrast (or null)
	 *   [0][0] - x
	 *   [0][1] - y
	 *   [0][2] - contrast
	 *   [1][0] - Wave vector 1 x component
	 *   [1][1] - Wave vector 1 y component
	 *   [1][2] - Wave vector 1 phase (not used here)
	 *   [2][0] - Wave vector 2 x component
	 *   [2][1] - Wave vector 2 y component
	 *   [2][2] - Wave vector 2 phase (not used here)
	 */
	public double[][] tryPattern (
			LwirReaderParameters lwirReaderParameters, // null is OK
			DoubleFHT doubleFHT,
			double [] point, // xy to try
			final DistortionParameters distortionParameters, //
			final MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			final double    min_half_period,
			final double    max_half_period,
			final SimulationPattern.SimulParameters  thisSimulParameters,
			final MatchSimulatedPattern matchSimulatedPattern,
			final MatchSimulatedPattern matchSimulatedPatternCorr,
			final SimulationPattern simulationPattern,
			final boolean equalizeGreens,
			final ImagePlus imp, // image to process
			double [] bPattern,
			double [] windowFunction,
			double [] windowFunctionCorr,
			double [] windowFunctionCorr2,
			double [] windowFunctionCorr4,
			double[][] locsNeib, // which neibors to try (here - just the center)
			String dbgStr
			){

//		this.debugLevel = 3;
		if (this.debugLevel == 3) {
			System.out.println("tryPattern(): this.debugLevel = 3");
		}
		int debug_threshold = 2;
		if (imp==null) {
			if (dbgStr!=null) System.out.println(dbgStr+" imp==null");
			return null;
		}
		boolean is_lwir = ((lwirReaderParameters != null) && lwirReaderParameters.is_LWIR(imp));
		int     fft_size = is_lwir ? distortionParameters.FFTSize_lwir : distortionParameters.FFTSize;

		int xc= (int)(2*Math.round(0.5*point[0]));
		int yc= (int)(2*Math.round(0.5*point[1]));
		Roi roi= imp.getRoi();
		final Rectangle selection;
		if (roi==null){
			selection=new Rectangle(0, 0, imp.getWidth(), imp.getHeight());
		} else {
			selection=roi.getBounds();
		}
		Rectangle initialPatternCell=new Rectangle(xc-fft_size,
				yc-fft_size,
				2*fft_size,2*fft_size);
		if (is_lwir) {// move to to all? It requires twice FFT size to be able to select, maybe single is still OK?
			initialPatternCell=new Rectangle(xc-fft_size/2,
					yc-fft_size/2,
					fft_size,fft_size);
3311

3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327
		}
		if (!selection.contains(initialPatternCell)) {
			if (dbgStr!=null) System.out.println(dbgStr+" selection ("+
					selection.x+","+selection.y+","+selection.width+","+selection.height+ ") does not contain cell ("+
					initialPatternCell.x+","+initialPatternCell.y+","+initialPatternCell.width+","+initialPatternCell.height+ ")");
			return null; // area for FFT is not inside the initial selection
		}
		double [][] pattern = null;
		if (is_lwir) { // monochrome
			double [] dpixels =  getNoBayer(imp, initialPatternCell);
			double [] dbg_dpixels = dpixels.clone();
			normalizeAndWindow (dpixels, windowFunction);
			if (debugLevel > (debug_threshold + 0)) {
				double [][] dbg_img = {dbg_dpixels, dpixels};
				SDFA_INSTANCE.showArrays(dbg_img,  true, "selection-input"+
				(initialPatternCell.x+initialPatternCell.width/2)+":"+(initialPatternCell.y+initialPatternCell.height/2));
Andrey Filippov's avatar
Andrey Filippov committed
3328
			}
3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369
			pattern=matchSimulatedPattern.findPattern(
					doubleFHT,
					dpixels,
					fft_size,
					patternDetectParameters,
					min_half_period,
					max_half_period,
					false, // this is a pattern for combined greens (diagonal), adjust results accordingly
					"Pattern"); // title - will not be used
		} else { // Bayer - extract green
			//create diagonal green selection around xc,yc
			double [][] input_bayer=splitBayer (imp,initialPatternCell,equalizeGreens);
			if (debugLevel > (debug_threshold + 0)) {
				SDFA_INSTANCE.showArrays(input_bayer,  true, "selection--bayer");
			}
			double [] greens=normalizeAndWindow (input_bayer[4], windowFunction);
			pattern=matchSimulatedPattern.findPattern(
					doubleFHT,
					greens,
					fft_size,
					patternDetectParameters,
					min_half_period,
					max_half_period,
					true, // this is a pattern for combined greens (diagonal), adjust results accordingly
					"Pattern"); // title - will not be used
		}
		if (pattern==null) {
			//				System.out.println("Error - pattern not found");
			//			  	IJ.showMessage("Error","Failed to find pattern");
			if (dbgStr!=null) System.out.println(dbgStr+" matchSimulatedPattern.findPattern->null");
			return null;
		}
		if (debugLevel > (debug_threshold + 0)) System.out.println("FX1="+pattern[0][0]+"  FY1="+pattern[0][1]+"  phase1="+pattern[0][2]);
		if (debugLevel > (debug_threshold + 0)) System.out.println("FX2="+pattern[1][0]+"  FY2="+pattern[1][1]+"  phase2="+pattern[1][2]);
		double [] ll2=new double[2];
		double [][] dxy=new double[2][2];
		double [][] phases=new double[2][2];
		int i,j,k;
		for (i=0;i<2;i++) {
			ll2[i]=(pattern[i][0]*pattern[i][0]+pattern[i][1]*pattern[i][1])*2*Math.PI;
		}
3370

3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426
		if (debugLevel > (debug_threshold + 0)) System.out.println("phase1/2pi="+(pattern[0][2]/2/Math.PI)+"  phase2/2pi="+(pattern[1][2]/2/Math.PI));
		for (k=0;k<2;k++) {
			for (j=0;j<2;j++) {
				phases[k][j]= pattern[j][2]-Math.PI/2+((k>0)? Math.PI:0.0);
				while (phases[k][j]<-Math.PI) phases[k][j]+=2*Math.PI;
				while (phases[k][j]>Math.PI)  phases[k][j]-=2*Math.PI;
			}
			if (debugLevel > (debug_threshold + 0)) System.out.println("phase1/2pi="+(phases[k][0]/2/Math.PI)+"  phase2/2pi="+(phases[k][1]/2/Math.PI));
			for (i=0;i<2;i++) {
				dxy[k][i]=0;
				for (j=0;j<2;j++) {
					dxy[k][i]+=(phases[k][j])*pattern[j][i]/ll2[j];
				}
			}
			if (debugLevel > (debug_threshold + 0)) System.out.println("dX["+k+"]="+dxy[k][0]+" dY["+k+"]="+dxy[k][1]);
		}
		int phaseSel=((dxy[0][0]*dxy[0][0]+dxy[0][1]*dxy[0][1])<(dxy[1][0]*dxy[1][0]+dxy[1][1]*dxy[1][1]))?0:1;
		if (debugLevel > (debug_threshold + 0)) System.out.println("xc="+xc+" yc="+yc);
		if (debugLevel > (debug_threshold + 0)) System.out.println("dX="+dxy[phaseSel][0]+" dY="+dxy[phaseSel][1]);
		double [] centerXY0={xc-dxy[phaseSel][0],yc-dxy[phaseSel][1]};
		if (debugLevel > (debug_threshold - 1)) System.out.println("+++ Initial center x="+IJ.d2s(centerXY0[0],3)+" y="+ 	IJ.d2s(centerXY0[1],3));
/*
		if (debugLevel > (debug_threshold + 0)){
			double [] test_error_offset = {0.0, 2.0};
			centerXY0[0] += test_error_offset[0];
			centerXY0[1] += test_error_offset[1];
			System.out.println("+++ Initial center with intentional error offset: x="+IJ.d2s(centerXY0[0],3)+" y="+ 	IJ.d2s(centerXY0[1],3));
		}
*/
		double [] centerXY=correctedPatternCrossLocation(
				lwirReaderParameters, // LwirReaderParameters lwirReaderParameters, // null is OK
				centerXY0, // initial coordinates of the pattern cross point
				pattern[0][0],
				pattern[0][1],
				pattern[1][0],
				pattern[1][1],
				null, // correction
				imp,      // image data (Bayer mosaic)
				distortionParameters, //
				patternDetectParameters,
				matchSimulatedPatternCorr, // correlationSize
				thisSimulParameters,
				equalizeGreens,
				windowFunctionCorr,   // window function
				windowFunctionCorr2,   // window function
				windowFunctionCorr4,   // window function
				simulationPattern,
				false, // is_lwir, // false, // if true - invert pattern TODO: Put is_lwir here?
				null, // will create new instance of DoubleFHT class
				distortionParameters.fastCorrelationOnFirstPass,
				locsNeib,
				debugLevel,
				dbgStr);
		if (debugLevel > (debug_threshold - 1)) System.out.println("--- Initial center x="+IJ.d2s(centerXY0[0],3)+" y="+ 	IJ.d2s(centerXY0[1],3)+
				" -> "+((centerXY==null)?" NULL ":(IJ.d2s(centerXY[0],3)+" : "+ 	IJ.d2s(centerXY[1],3))));
		// TODO: check statistics of correction - see if there is any dc bias
3427

3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456
/*
		if ((debugLevel > (debug_threshold + 0)) && (centerXY != null)) {
			// trying second time - watch for conversion
			double [] centerXY1=correctedPatternCrossLocation(
					lwirReaderParameters, // LwirReaderParameters lwirReaderParameters, // null is OK
					centerXY, // initial coordinates of the pattern cross point
					pattern[0][0],
					pattern[0][1],
					pattern[1][0],
					pattern[1][1],
					null, // correction
					imp,      // image data (Bayer mosaic)
					distortionParameters, //
					patternDetectParameters,
					matchSimulatedPatternCorr, // correlationSize
					thisSimulParameters,
					equalizeGreens,
					windowFunctionCorr,   // window function
					windowFunctionCorr2,   // window function
					windowFunctionCorr4,   // window function
					simulationPattern,
					false, // is_lwir, // false, // if true - invert pattern TODO: Put is_lwir here?
					null, // will create new instance of DoubleFHT class
					distortionParameters.fastCorrelationOnFirstPass,
					locsNeib,
					debugLevel,
					dbgStr);
			System.out.println("--- Second run: initial center x="+IJ.d2s(centerXY[0],3)+" y="+ 	IJ.d2s(centerXY[1],3)+
					" -> "+((centerXY1==null)?" NULL ":(IJ.d2s(centerXY1[0],3)+" : "+ 	IJ.d2s(centerXY1[1],3))));
3457 3458 3459



3460 3461
		}
*/
3462 3463 3464



Andrey Filippov's avatar
Andrey Filippov committed
3465

3466 3467 3468 3469 3470 3471 3472
		double [][] node = {centerXY,pattern[0],pattern[1]};
		if (dbgStr!=null) {
			if (centerXY==null) System.out.println(dbgStr+" correctedPatternCrossLocation->null");
			else System.out.println(dbgStr+" matchSimulatedPattern.findPattern->{{"+centerXY[0]+","+centerXY[1]+","+centerXY[2]+"}..}");
		}
		return node;
	}
Andrey Filippov's avatar
Andrey Filippov committed
3473

3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511
	// ============= end of public double[][] tryPattern() ==================

	/* ================================================================*/
	// Optionally remove the outer (possibly corrupted) layer of the detected pattern nodes, extrapolate new layers of the nodes
	// without pattern matching

	public double[][][][] finalizeDistortionsBorder (
			//			   final double [][][][] patternGrid,
			final DistortionParameters distortionParameters, //
			final boolean updateStatus,
			final int debug_level){// debug level used inside loops
		//		    double[][][][] patternGrid=this.PATTERN_GRID;

		//	        final int [][] directionsUV=  {{1,0},{0,1},{-1,0},{0,-1}}; // should have opposite direction shifted by half
		final int [][] directionsUV8= {{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,1},{-1,-1},{1,-1}}; // first 8 should be the same as in directionsUV

		final List <Integer> waveFrontList=new ArrayList<Integer>(1000);
		// create list of all nodes that have undefined neigbors (up/down/right/left)
		int umax=0, vmax=0, vmin=this.PATTERN_GRID.length, umin=this.PATTERN_GRID[0].length;
		for (int i=0;i<this.PATTERN_GRID.length;i++) for (int j=0;j<this.PATTERN_GRID[i].length;j++) {
			if ((this.PATTERN_GRID[i][j]!=null) && (this.PATTERN_GRID[i][j][0]!=null)) {
				if (vmin > i) vmin = i;
				if (vmax < i) vmax = i;
				if (umin > j) umin = j;
				if (umax < j) umax = j;
			}
		}
		int [] uvNew=new int [2];
		int [] iUV=  new int [2];
		int [] uvdir; // (u,v,direction}
		double [][][] wave;
		for (uvNew[1]=vmin;uvNew[1]<=vmax;uvNew[1]++) for (uvNew[0]=umin;uvNew[0]<=umax;uvNew[0]++) if (isCellDefined(this.PATTERN_GRID,uvNew)){
			for (int dir=0;dir<directionsUV8.length;dir++) {
				iUV[0]=uvNew[0]+directionsUV8[dir][0];
				iUV[1]=uvNew[1]+directionsUV8[dir][1];
				if (!isCellDefined(this.PATTERN_GRID,iUV)){
					putInWaveList (waveFrontList, uvNew, dir); // direction does not matter here
					break;
Andrey Filippov's avatar
Andrey Filippov committed
3512 3513
				}
			}
3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705

		}
		final double [][] extrapolationWeights=generateWeights (
				distortionParameters.extrapolationSigma,
				distortionParameters.correlationRadiusScale); //  if 0 - use sigma as radius, inside - 1.0, outside 0.0. If >0 - size of array n*sigma

		if (debugLevel>1) System.out.println("***** finalizeDistortionsBorder, initial wave length="+waveFrontList.size());
		// optionally remove outer (possibly corruopted) layer of nodes
		if (distortionParameters.removeLast) for (int i=0;i<waveFrontList.size();i++){
			if (distortionParameters.numberExtrapolated==0)	invalidatePatternGridCell(this.PATTERN_GRID, getWaveList (waveFrontList,i));
			else 	                                              initPatternGridCell(this.PATTERN_GRID, getWaveList (waveFrontList,i));
		}
		for (int layer=0;(layer<distortionParameters.numberExtrapolated) && (waveFrontList.size()>0);layer++){

			if ((layer>0) || !distortionParameters.removeLast) { // build new layer around the current one
				while (waveFrontList.size()>0) { // will normally break out of the cycle
					uvdir= getWaveList (waveFrontList,0);
					//							if (this.PATTERN_GRID[uvdir[1]][uvdir[0]]==null) break; // finished adding new layer
					if (!isCellDefined(this.PATTERN_GRID,uvdir)) break; // finished adding new layer, hit one of the newely added
					for (int dir=0;dir<directionsUV8.length;dir++) {
						iUV[0]=uvdir[0]+directionsUV8[dir][0];
						iUV[1]=uvdir[1]+directionsUV8[dir][1];
						if ((iUV[0]<0) || (iUV[1]<0) ||
								(iUV[0]>=distortionParameters.gridSize) || (iUV[1]>=distortionParameters.gridSize)) continue; // don't fit into UV grid
						if (!isCellNew(this.PATTERN_GRID,iUV)) continue; // already processed
						// add uv and dir to the list
						putInWaveList (waveFrontList, iUV, dir); //  direction is not used
						initPatternGridCell(this.PATTERN_GRID, iUV);
						//								if (debugLevel>1) System.out.println("-->iUV= "+iUV[0]+",  "+iUV[1]+",  "+((dir+(directionsUV.length/2))%directionsUV.length));
					}
					waveFrontList.remove(0);  // remove first element from the list
					//		    			if (debugLevel>1) System.out.println("xx> remove(0), (waveFrontList.size()="+(waveFrontList.size()));
				}
			}
			// extrapolate x,y for the new layer of pixels (not yet using the new pixels in this layer)
			if (updateStatus) IJ.showStatus("Extrapolating border, layer "+(layer+1)+", length "+waveFrontList.size());
			if (debugLevel>1) System.out.println("Extrapolating border, layer "+(layer+1)+", length "+waveFrontList.size());
			wave = new double [waveFrontList.size()][][];
			for (int i=0;i<wave.length;i++) {
				wave[i]=estimateCell(
						this.PATTERN_GRID,
						getWaveList (waveFrontList,i),
						extrapolationWeights, // quadrant of sample weights
						true, // useContrast
						!distortionParameters.useQuadratic,  // use linear approximation (instead of quadratic)
						1.0E-10,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
						1.0E-20  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
						);
				if (wave[i]==null) { // try w/o contrast, just x,y
					wave[i]=estimateCell(
							this.PATTERN_GRID,
							getWaveList (waveFrontList,i),
							extrapolationWeights, // quadrant of sample weights
							false, // do not use Contrast, keep old contrast (even if it is NaN)
							!distortionParameters.useQuadratic,  // use linear approximation (instead of quadratic)
							1.0E-10,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
							1.0E-20  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
							);

				}
			}
			// set new values, removed failed cells (normally should not be any)
			for (int i=wave.length-1;i>=0;i--) {
				uvdir=getWaveList (waveFrontList,i);
				this.PATTERN_GRID[uvdir[1]][uvdir[0]]=wave[i]; // null OK
				if (wave[i]==null) {
					if (debugLevel>0) System.out.println("Removing failed node (normally should not happen!), u="+uvdir[0]+", v="+uvdir[1]);
					waveFrontList.remove(i);
				}

			}

		}

		return null;
	}
	/* ================================================================*/
	// it now can start with non-empty Grid
	public int distortions( // returns number of grid cells
			final LwirReaderParameters lwirReaderParameters, // null is OK
			final boolean [] triedIndices,
			final DistortionParameters distortionParameters, //
			final MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			final double    min_half_period,
			final double    max_half_period,
			final SimulationPattern.SimulParameters  simulParameters,
			final boolean equalizeGreens,
			final ImagePlus imp, // image to process
			final int threadsMax,
			final boolean updateStatus,
			final int debug_level, // debug level used inside loops
			final int global_debug_level){
		if (imp==null) return 0;
		final int debugThreshold = 1;
		final boolean is_lwir = ((lwirReaderParameters != null) && lwirReaderParameters.is_LWIR(imp));
		final int      fft_size =             is_lwir ? distortionParameters.FFTSize_lwir :               distortionParameters.FFTSize;
		final int      correlation_size =     is_lwir ? distortionParameters.correlationSizeLwir :        distortionParameters.correlationSize;
		final int      max_correlation_size = is_lwir ? distortionParameters.maximalCorrelationSizeLwir : distortionParameters.maximalCorrelationSize;
		final int      minimal_pattern_cluster = is_lwir ? distortionParameters.minimalPatternClusterLwir : distortionParameters.minimalPatternCluster;
		final int [][] directionsUV=  {{1,0},{0,1},{-1,0},{0,-1}}; // should have opposite direction shifted by half
		final int [][] directionsUV8= {{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,1},{-1,-1},{1,-1}}; // first 8 should be the same as in directionsUV
		final int [] directionsBits8= {1,4,1,4,2,8,2,8}; // should match directionsUV8
		int neibBits;
		final Thread[] threads = newThreadArray(threadsMax);
		final AtomicInteger cellNum = new AtomicInteger(0);
		final List <Integer> waveFrontList=new ArrayList<Integer>(1000);
		final int [] centerUV= {distortionParameters.gridSize/2, distortionParameters.gridSize/2};

		final double [][] locsNeib=calcNeibLocsWeights (distortionParameters,false); // no neighbors to average
		Roi roi= imp.getRoi();
		final Rectangle selection;
		if (PATTERN_GRID==null) {
			if (roi==null){
				selection=new Rectangle(0, 0, imp.getWidth(), imp.getHeight());
			} else {
				selection=roi.getBounds();
			}
		} else {
			if ((getImageHeight()!=imp.getHeight()) || (getImageWidth()!=imp.getWidth())){
				String msg="Supplied image does not match in dimensions "+imp.getWidth()+"x"+imp.getHeight()+
						" the one for wich grid was calculated ("+getImageWidth()+"x"+getImageHeight()+")";
				IJ.showMessage("Error",msg);
				throw new IllegalArgumentException (msg);
			}
			selection=new Rectangle(0, 0, getImageWidth(), getImageHeight());
		}
		MatchSimulatedPattern matchSimulatedPattern=new MatchSimulatedPattern(fft_size);
		matchSimulatedPattern.debugLevel=debugLevel;
		MatchSimulatedPattern matchSimulatedPatternCorr=new MatchSimulatedPattern(correlation_size);
		matchSimulatedPatternCorr.debugLevel=debugLevel;
		final SimulationPattern.SimulParameters  thisSimulParameters=simulParameters.clone();
		thisSimulParameters.subdiv=     distortionParameters.patternSubdiv;
		thisSimulParameters.bPatternSigma=distortionParameters.bPatternSigma;
		thisSimulParameters.barraySigma=distortionParameters.barraySigma;
		SimulationPattern simulationPattern= new SimulationPattern(thisSimulParameters);
		final double [] bPattern= simulationPattern.patternGenerator(simulParameters); // reuse pattern for next time
		//find center of the selection (to be used to find initial pattern approximation)
		if (debugLevel>2)	System.out.println("bPattern.length="+bPattern.length);
		double [] windowFunction=initWindowFunction(fft_size, distortionParameters.fftGaussWidth);
		// may need to decrease relative Gaussian width for larger windows
		//			public boolean absoluteCorrelationGaussWidth=false; // do not scale correlationGaussWidth when the FFT size is increased
		///(distortionParameters.absoluteCorrelationGaussWidth?0.5:1.0)
		final double [] windowFunctionCorr= initWindowFunction(
				correlation_size,
				distortionParameters.correlationGaussWidth,
				distortionParameters.zeros);
		final double [] windowFunctionCorr2=initWindowFunction(
				2*correlation_size,
				(distortionParameters.absoluteCorrelationGaussWidth?0.5:1.0)*distortionParameters.correlationGaussWidth,
				distortionParameters.zeros);
		final double [] windowFunctionCorr4=initWindowFunction(
				4*correlation_size,
				(distortionParameters.absoluteCorrelationGaussWidth?0.25:1.0)*distortionParameters.correlationGaussWidth,
				distortionParameters.zeros);

		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);
		final boolean useFocusMask=updating && (focusMask!=null); // do not expand wave beyound 1 grid step from needed image regions
		Queue<GridNode> nodeQueue = new ConcurrentLinkedQueue<GridNode>();
		boolean fromVeryBeginning=true;
		if (!updating) {
			for (int i=3;i<triedIndices.length;i++) if (triedIndices[i]){ // do not count first three NPE
				fromVeryBeginning=false;
				break;
			}
			double [] point = new double[2];
			int tryHor=0,tryVert=0;
			//				distortionParameters.searchOverlap=goniometerParameters.searchOverlap;
			// with distortionParameters.searchOverlap==0.5 (default) step will be FFTSize original pixels, so half of the (2xFFTSize) square processed simultaneously
			if (distortionParameters.searchOverlap<0.1) distortionParameters.searchOverlap=0.1;
			int effectiveWidth=(int) (selection.width*0.5/distortionParameters.searchOverlap);
			int effectiveHeight=(int) (selection.height*0.5/distortionParameters.searchOverlap);

			for (int i = fft_size; i < effectiveWidth;i*=2) tryHor++;
			for (int i = fft_size; i < effectiveHeight;i*=2) tryVert++;

			int numTries=triedIndices.length-1; // Should be equal to	   int numTries=1<<(tryHor+tryVert);

			int nbv,nbh,nh,nv,nb;
			if (debugLevel>1) System.out.println("selection.x="+selection.x+" selection.y="+selection.y+" selection.width="+selection.width+" selection.height="+selection.height);
			if (debugLevel>1) System.out.println("numTries="+numTries+" tryHor="+tryHor+" tryVert="+tryVert);
			boolean oldMode=false; //true; // false;

			if (oldMode) { // old (single-threaded) mode
				for (int startScanIndex=3;startScanIndex<=numTries;startScanIndex++) if (!triedIndices[startScanIndex]){
					if (startScanIndex==numTries){
						triedIndices[startScanIndex]=true; // all done
Andrey Filippov's avatar
Andrey Filippov committed
3706 3707
						break;
					}
3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854
					nbh=tryHor-1;
					nbv=tryVert-1;
					nh=0;
					nv=0;
					nb=0;
					while (nb<(tryHor+tryVert)) {
						if (nbh>=0) {
							if ((startScanIndex & (1<<nb))!=0) nh |= 1<<nbh;
							nbh--;
							nb++;
						}
						if (nbv>=0) {
							if ((startScanIndex & (1<<nb))!=0) nv |= 1<<nbv;
							nbv--;
							nb++;
						}
					}
					if (debugLevel>2) System.out.println("Searching, n="+startScanIndex+", nv="+nv+", nh="+nh+", nb="+nb );
					if ((nv>0) && (nh>0)) {
						point[0]=(selection.x+nh*selection.width/(1<<tryHor)) & ~1;
						point[1]=(selection.y+nv*selection.height/(1<<tryVert)) & ~1;
						if (debugLevel>2) System.out.println("trying xc="+point[0]+", yc="+point[1]+"(nv="+nv+", nh="+nh+")");
						//						   System.out.println("### trying xc="+point[0]+", yc="+point[1]+"(nv="+nv+", nh="+nh+")");
						if ((debugLevel>2) && (startScanIndex==3)) debugLevel=3; // show debug images for the first point only
						double [][] node=tryPattern (
								lwirReaderParameters,
								null,
								point, // xy to try
								thisDistortionParameters, //no control of the displacement
								patternDetectParameters,
								min_half_period,
								max_half_period,
								thisSimulParameters,
								matchSimulatedPattern,
								matchSimulatedPatternCorr,
								simulationPattern,
								equalizeGreens,
								imp, // image to process
								bPattern,
								windowFunction,
								windowFunctionCorr,
								windowFunctionCorr2,
								windowFunctionCorr4,
								locsNeib, // which neighbors to try (here - just the center)
								null // dbgStr
								);
						debugLevel=was_debug_level;
						if ((node!=null) && (node[0]!=null)) {
							nodeQueue.add(new GridNode(node));
							break;
						}
					}
					triedIndices[startScanIndex]=true;
				}
			} else { // new multithreaded mode
				int startScanIndex=3;
				for (;(startScanIndex<numTries) && triedIndices[startScanIndex];startScanIndex++); // skip already tried indices
				if ((global_debug_level > debugThreshold) && (startScanIndex>3)) System.out.println("distortions(): startScanIndex="+startScanIndex+" > 3 ####");

				if (startScanIndex<numTries) {
					nodeQueue =  findPatternCandidates(
							lwirReaderParameters, // LwirReaderParameters lwirReaderParameters, // null is OK
							triedIndices,
							startScanIndex, // [0] will be updated
							tryHor,
							tryVert,
							//						   numTries,
							selection,
							thisDistortionParameters, //no control of the displacement
							patternDetectParameters,
							min_half_period,
							max_half_period,
							thisSimulParameters,
							matchSimulatedPattern,
							matchSimulatedPatternCorr,
							simulationPattern,
							equalizeGreens,
							imp, // image to process
							bPattern,
							windowFunction,
							windowFunctionCorr,
							windowFunctionCorr2,
							windowFunctionCorr4,
							locsNeib, // which neighbors to try (here - just the center)
							threadsMax,
							updateStatus,
							this.debugLevel
							);
					if (nodeQueue.isEmpty()) { // nodes==null){
						//						   if (debugLevel>1) System.out.println("All start points tried");
						if (global_debug_level > (debugThreshold+1)) {
							System.out.println("All start points tried");
							int numLeft=0;
							for (boolean b:triedIndices) if (!b) numLeft++;
							System.out.println("nodeQueue.isEmpty(), startScanIndex="+startScanIndex+" numTries="+numTries+" numLeft="+numLeft);
						}
						triedIndices[numTries]=true; // all tried
					} else {
						//						   if (debugLevel>1) System.out.println("Found "+nodes.length+" candidates");
						//						   if (debugLevel>1) System.out.println("distortions: Found "+nodeQueue.size()+" candidates");
						if (global_debug_level > debugThreshold){
							System.out.println("distortions: Found "+nodeQueue.size()+" candidates");
						}
					}
				} else {
					System.out.println("All start points tried before - should not get here");
					triedIndices[numTries]=true; // all tried
				}
			}
			//				   if (global_debug_level>0) System.out.println("distortions(): startScanIndex="+startScanIndex);

			//			   if (startScanIndex[0]>=numTries) startScanIndex[0]=-1; // all indices used
			//			   if ((nodes==null) || (nodes[0]==null) || (nodes[0][0]==null)) {
			if ((nodeQueue.isEmpty()) || (nodeQueue.peek().getNode()[0]==null)) {
				if (debugLevel>1) System.out.println("*** Pattern not found");
				this.PATTERN_GRID=null;
				return 0;
			}
			if (global_debug_level> (debugThreshold + 1)) {
				System.out.println("distortions(): found "+nodeQueue.size()+" grid candidates");
				//				   System.out.println("*** distortions: Center x="+IJ.d2s(centerXY[0],3)+" y="+ 	IJ.d2s(centerXY[1],3));
			}

			debugLevel=debug_level; // ????

		} else { // create initial wave from the border nodes of existent grid
			// start with clearing all invalid nodes
			for (iUV[1]=0;iUV[1]<this.PATTERN_GRID.length;iUV[1]++) for (iUV[0]=0;iUV[0]<this.PATTERN_GRID[0].length;iUV[0]++)
				if (!isCellDefined(this.PATTERN_GRID,iUV))clearPatternGridCell(this.PATTERN_GRID,iUV);
			//		   int [] iUV=  new int [2];
			// probably start with clearing all invalid nodes
			//// 	public boolean [] focusMask=null; // array matching image pixels, used with focusing (false outside sample areas)
			int [] iUV1=new int[2];
			for (iUV[1]=0;iUV[1]<this.PATTERN_GRID.length;iUV[1]++) for (iUV[0]=0;iUV[0]<this.PATTERN_GRID[0].length;iUV[0]++)
				if (isCellDefined(this.PATTERN_GRID,iUV)){ // see if it has any new undefined neighbors
					boolean hasNewNeib=false;
					for (int dir=0;dir<directionsUV.length;dir++) {
						iUV1[0]=iUV[0]+directionsUV[dir][0];
						iUV1[1]=iUV[1]+directionsUV[dir][1];
						if (isCellNew(this.PATTERN_GRID,iUV1)){
							hasNewNeib=true;
							break;
						}
					}
					if (hasNewNeib) {
						putInWaveList(waveFrontList, iUV, 0);
					}
Andrey Filippov's avatar
Andrey Filippov committed
3855
				}
3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871
			double [][] node={null};
			nodeQueue.add(new GridNode(node)); // will not be used, any element
		}
		int numDefinedCells=0;
		int debug_left=nodeQueue.size();
		for (GridNode gn:nodeQueue){ // trying candidates as grid seeds - until found or nothing left
			debug_left--;
			if (global_debug_level > (debugThreshold + 1)) {
				System.out.println("distortions: nodeQueue has "+(debug_left)+" candidates left (excluding this one)");
			}
			if (!updating){
				double [][] node=gn.getNode();
				double [] centerXY=node[0];
				if (global_debug_level > debugThreshold) {
					//					   System.out.println("distortions: node X/Y are "+centerXY[0]+"/"+centerXY[1]);
					System.out.println("distortions: nodeQueue has "+(debug_left)+" candidates left (excluding this one) :node X/Y are "+centerXY[0]+"/"+centerXY[1]);
Andrey Filippov's avatar
Andrey Filippov committed
3872

3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894
				}
				//				   if (debugLevel>1) {
				if (global_debug_level > (debugThreshold + 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]; // Change debug coordinates to the initial node
					patternDetectParameters.debugY=centerXY[1]; //patternDetectParameters.debugRadius);
				}
				debugLevel=debug_level;
				// Reset pattern grid
				this.PATTERN_GRID=setPatternGridArray(distortionParameters.gridSize); // global to be used with threads?
				setPatternGridCell(
						this.PATTERN_GRID,
						centerUV,
						centerXY, // contrast OK?
						node[1],
						node[2]);
				waveFrontList.clear();
				putInWaveList(waveFrontList, centerUV, 0);
				if (global_debug_level > debugThreshold) { //1) {
					System.out.println("putInWaveList(waveFrontList, {"+centerUV[0]+","+centerUV[1]+"}, 0);");
				}
Andrey Filippov's avatar
Andrey Filippov committed
3895
			}
3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908

			// Each layer processing may be multi-threaded, they join before going to the next layer
			// When looking for the next cells, the position is estimated knowing the neighbor that has wave vectors defined
			// after the layer pass is over, the wave vectors are calculated from the distances to neighbors (one or both vectors may have
			// to use those from the neighbor?
			if (debugLevel>1) System.out.println("-->centerUV= "+centerUV[0]+",  "+centerUV[1]+",  0");
			int [] uvdir; // (u,v,direction}
			int layer=0;
			int dir;
			double [][][] neibors=new double [8][][]; // uv and xy vectors to 8 neibors (some may be null
			double [][] thisCell;
			double [][] otherCell;
//			final int debugThreshold=2;
Andrey Filippov's avatar
Andrey Filippov committed
3909
			final double [][] extrapolationWeights=generateWeights (
3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038
					distortionParameters.correlationWeightSigma,
					distortionParameters.correlationRadiusScale); //  if 0 - use sigma as radius, inside - 1.0, outside 0.0. If >0 - size of array n*sigma

			int umax,vmax,vmin,umin;
			final AtomicInteger addedCells = new AtomicInteger(0); // cells added at cleanup stage
			final AtomicBoolean cleanup=new AtomicBoolean(false); // after the wave dies, it will be restored for all cells with defined neigbors to try again. maybe - try w/o threads?

			final AtomicInteger debugCellSet= new AtomicInteger(0); // cells added at cleanup stage
			// special case (most common, actually) when initial wave has 1  node. Remove it after processing
			//first cell(s) will need large correction and so may fail during "refine", so trying to recalculate it right after the first layer)
			ArrayList<Integer> initialWave=new ArrayList<Integer>();
			for (Integer I:waveFrontList) initialWave.add(I);
			while (waveFrontList.size()>0) {
				// process current list, add new wave layer (moving in one of the 4 directions)
				// proceed until the entry is undefined on the grid (or list is empty
				while (waveFrontList.size()>0) { // will normally break out of the cycle
					uvdir= getWaveList (waveFrontList,0);
					if (this.debugLevel > (debugThreshold + 1)) System.out.println("<--uvdir= "+uvdir[0]+",  "+uvdir[1]+",  "+uvdir[2]);
					if (this.PATTERN_GRID[uvdir[1]][uvdir[0]]==null) break; // finished adding new layer
					if (!isCellDefined(this.PATTERN_GRID,uvdir)) break; // finished adding new layer, hit one of the newely added
					boolean hasNeededNeighbor=true;
					if (useFocusMask) {
						int ix=(int) Math.round(this.PATTERN_GRID[uvdir[1]][uvdir[0]][0][0]);
						int iy=(int) Math.round(this.PATTERN_GRID[uvdir[1]][uvdir[0]][0][1]);
						int indx=iy*getImageWidth()+ix;
						if ((indx<0) || (indx>focusMask.length)){
							System.out.println("distortions(): this.PATTERN_GRID["+uvdir[1]+"]["+uvdir[0]+"][0][0]="+this.PATTERN_GRID[uvdir[1]][uvdir[0]][0][0]);
							System.out.println("distortions(): this.PATTERN_GRID["+uvdir[1]+"]["+uvdir[0]+"][0][1]="+this.PATTERN_GRID[uvdir[1]][uvdir[0]][0][1]);
							System.out.println("distortions(): ix="+ix);
							System.out.println("distortions(): iy="+iy);
							System.out.println("distortions(): focusMask.length="+focusMask.length);
						}
						// TODO: find how it could get negative coordinates
						if ((ix<0) || (iy<0) || (ix>=distortionParameters.gridSize) || (iy>=distortionParameters.gridSize)) hasNeededNeighbor=false; //???
						else hasNeededNeighbor=focusMask[iy*getImageWidth()+ix]; //* OOB -1624 java.lang.ArrayIndexOutOfBoundsException: -1624,  at MatchSimulatedPattern.distortions(MatchSimulatedPattern.java:3063),  at LensAdjustment.updateFocusGrid(LensAdjustment.java:121),  at Aberration_Calibration.measurePSFMetrics(Aberration_Calibration.java:5994)
					}
					for (dir=0;dir<directionsUV.length;dir++) {
						iUV[0]=uvdir[0]+directionsUV[dir][0];
						iUV[1]=uvdir[1]+directionsUV[dir][1];

						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 (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
						if (hasNeededNeighbor) {
							putInWaveList (waveFrontList, iUV, (dir+(directionsUV.length/2))%directionsUV.length); // opposite direction
							initPatternGridCell(PATTERN_GRID, iUV);
							if (debugLevel>1) System.out.println("-->iUV= "+iUV[0]+",  "+iUV[1]+",  "+((dir+(directionsUV.length/2))%directionsUV.length));
						}
					}
					waveFrontList.remove(0);  // remove first element from the list
					if (debugLevel>1) System.out.println("xx> remove(0), (waveFrontList.size()="+(waveFrontList.size()));
				}
				if (waveFrontList.size()==0) break; // not really needed?
				layer++;
				if (updateStatus) IJ.showStatus("Correlating patterns, layer "+layer+(cleanup.get()?"(cleanup)":"")+", length "+waveFrontList.size());
				//				   if (debugLevel>1) System.out.println("Correlating patterns, layer "+layer+", length "+waveFrontList.size());
				if (global_debug_level > (debugThreshold + 1)) System.out.println("Correlating patterns, layer "+layer+", length "+waveFrontList.size());
				// starting layer
				cellNum.set(0);
				for (int ithread = 0; ithread < threads.length; ithread++) {
					threads[ithread] = new Thread() {
						@Override
						public void run() {
							SimulationPattern simulationPattern= new SimulationPattern(bPattern);
							MatchSimulatedPattern matchSimulatedPatternCorr=new MatchSimulatedPattern(distortionParameters.correlationSize);
							DoubleFHT fht_instance =new DoubleFHT(); // provide DoubleFHT instance to save on initializations (or null)
							String dbgStr="";
							for (int ncell=cellNum.getAndIncrement(); ncell<waveFrontList.size();ncell=cellNum.getAndIncrement()){
								int [] iUVdir=getWaveList (waveFrontList,ncell);
								if (debugLevel>debugThreshold) {
									dbgStr="";
									dbgStr+="<--iUVdir= "+iUVdir[0]+",  "+iUVdir[1]+",  "+iUVdir[2];
								}
								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?**** u="+iUVRef[0]+" v="+iUVRef[1]+
											" current="+iUVdir[0]+"/"+iUVdir[1]+" len="+iUVdir.length);
									continue;
								} else if ((refCell[0]!=null) && (refCell[0].length>3)){
									double dbg_contrast=(refCell[0].length>2)?refCell[0][2]:Double.NaN;
									System.out.println("**** refCell was deleted **** u="+iUVRef[0]+" v="+iUVRef[1]+
											" current="+iUVdir[0]+"/"+iUVdir[1]+
											" ncell="+ncell+" waveFrontList.size()="+waveFrontList.size()+
											" ref_x="+IJ.d2s(refCell[0][0],3)+" ref_y="+IJ.d2s(refCell[0][1],3)+
											" contrast="+IJ.d2s(dbg_contrast,3));
								}
								//found reference cell, calculate x/y, make sure it is inside the selection w/o borders
								double [][] wv=new double [2][];
								wv[0]=refCell[1];
								wv[1]=refCell[2];
								double [][]uv2xy=matrix2x2_invert(wv);

								double [] dUV = new double [2];
								dUV[0]=0.5*(iUVdir[0]-iUVRef[0]);
								dUV[1]=0.5*(iUVdir[1]-iUVRef[1]);
								double []dXY=matrix2x2_mul(uv2xy,dUV);
								double [] expectedXY=matrix2x2_add(refCell[0],dXY);

								// Try new extrapolation, debug print both
								//extrapolationWeights
								double [][] estimatedCell=estimateCell(
										PATTERN_GRID,
										iUVdir,
										extrapolationWeights, // quadrant of sample weights
										true, // useContrast
										!distortionParameters.useQuadratic,  // use linear approximation (instead of quadratic)
										1.0E-10,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
										1.0E-20  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
										);
								double [][] simulPars=null;
								if (debugLevel>debugThreshold) {
									dbgStr+=" ExpectedXY(old)= "+IJ.d2s(expectedXY[0],3)+" / "+IJ.d2s(expectedXY[1],3)+",  "+
											" vw00="+IJ.d2s(wv[0][0],5)+" vw01="+IJ.d2s(wv[0][1],5)+
											" vw10="+IJ.d2s(wv[1][0],5)+" vw11="+IJ.d2s(wv[1][1],5);
									if (estimatedCell==null) {
										dbgStr+=" -- ExpectedXY(new)= ***** NULL **** ";
									} else {
										dbgStr+=" -- ExpectedXY(new)= "+IJ.d2s(estimatedCell[0][0],3)+" / "+IJ.d2s(estimatedCell[0][1],3)+",  "+
												" vw00="+IJ.d2s(estimatedCell[1][0],5)+" vw01="+IJ.d2s(estimatedCell[1][1],5)+
												" vw10="+IJ.d2s(estimatedCell[2][0],5)+" vw11="+IJ.d2s(estimatedCell[2][1],5);
									}
4039

4040

4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130
								}
								if (estimatedCell!=null) {
									expectedXY=estimatedCell[0];
									wv[0]=estimatedCell[1];
									wv[1]=estimatedCell[2];

									simulPars=getSimulationParametersFromGrid(
											PATTERN_GRID,
											iUVdir,          // U,V of the center point (for which the simulation pattern should be built
											expectedXY,          // x,y of the center point (or null to use grid)
											extrapolationWeights, // quadrant of sample weights
											!distortionParameters.useQuadratic,  // use linear approximation (instead of quadratic)
											1.0E-10,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
											1.0E-20  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
											);
									if (debugLevel>debugThreshold) {
										dbgStr+=" {"+IJ.d2s(simulPars[0][0],5)+"/"+IJ.d2s(simulPars[0][1],5)+"/"+IJ.d2s(simulPars[0][2],5);
										if (simulPars[0].length>3) dbgStr+="/"+IJ.d2s(simulPars[0][3],7)+"/"+IJ.d2s(simulPars[0][4],7)+"/"+IJ.d2s(simulPars[0][5],7)+"}";
										dbgStr+=" {"+IJ.d2s(simulPars[1][0],5)+"/"+IJ.d2s(simulPars[1][1],5)+"/"+IJ.d2s(simulPars[1][2],5);
										if (simulPars[1].length>3) dbgStr+="/"+IJ.d2s(simulPars[1][3],7)+"/"+IJ.d2s(simulPars[1][4],7)+"/"+IJ.d2s(simulPars[1][5],7)+"}";
									}
								}
								if (!selection.contains((int) Math.round(expectedXY[0]),(int) Math.round(expectedXY[1]))) { // just the center point
									invalidatePatternGridCell(
											PATTERN_GRID,
											iUVdir);
									if (debugLevel>debugThreshold) {
										dbgStr+=" -- not in selection ";
										System.out.println(dbgStr);
									}
									continue; // the correlation selection does not fit into WOI selection
								}
								//Proceed with correlation
								//TODO: add contrast verification ? Maximal distance from expected? (return null if failed)
								double [] centerXY=correctedPatternCrossLocation(
										lwirReaderParameters,
										expectedXY, // initial coordinates of the pattern cross point
										wv[0][0],
										wv[0][1],
										wv[1][0],
										wv[1][1],
										simulPars,
										imp,      // image data (Bayer mosaic)
										distortionParameters, //
										patternDetectParameters,
										matchSimulatedPatternCorr, // correlationSize
										thisSimulParameters,
										equalizeGreens,
										windowFunctionCorr,
										windowFunctionCorr2,
										windowFunctionCorr4,
										simulationPattern,
										((iUVdir[0]^iUVdir[1])&1)!=0, // if true - invert pattern
										fht_instance,
										distortionParameters.fastCorrelationOnFirstPass,
										locsNeib,
										debugLevel,
										null);
								//	    						 System.out.println("*+*debugLevel="+debugLevel);
								if (centerXY==null){
									invalidatePatternGridCell(
											PATTERN_GRID,
											iUVdir);
									if (debugLevel>debugThreshold) {
										dbgStr+=" -- FAILED";
										System.out.println(dbgStr);
									}
									continue; // failed to find pattern in the cell TODO: implement
								}
								if (debugCellSet.getAndIncrement()==0){ // First cell
									if (passNumber==1) {
										debugUV[0]=iUVdir[0];
										debugUV[1]=iUVdir[1];
										if (debugLevel>debugThreshold) System.out.println("debugUV[] set to {"+debugUV[0]+","+debugUV[1]+"}");
										passNumber=2; // global passNumber
									}
								}
								//Found new cell, save info and increment counter
								setPatternGridCell(
										PATTERN_GRID,
										iUVdir,
										centerXY,
										// specify wave vectors from the parent cell, will recalculate (if possible)
										wv[0], //null, //  double [] wv1,
										wv[1]); //null); //  double [] wv2);
								if (cleanup.get()) addedCells.getAndIncrement();
								if (debugLevel>debugThreshold) { //was no "-2"
									dbgStr+="==>added"+iUVdir[0]+"/"+iUVdir[1]+", dir"+iUVdir[2];
									System.out.println(dbgStr);
								}
Andrey Filippov's avatar
Andrey Filippov committed
4131

4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193
							}
						}
					};
				}
				startAndJoin(threads);
				// remove invalid cells from the list
				for (int i=waveFrontList.size()-1;i>=0;i--) {
					if (!isCellValid(PATTERN_GRID,getWaveList (waveFrontList,i))) {
						// Make that cell "new", so it will be tried again, until wave will not touch it. So when more neigbors will be defined, previously failed
						// cell will be retried
						clearPatternGridCell(PATTERN_GRID,getWaveList (waveFrontList,i));
						waveFrontList.remove(i);
						if (debugLevel > (debugThreshold + 1)) System.out.println("XXX->clear invalid ("+i+")");
					}
				}
				//If anything was added during the layer - calculate and fill in wave vectors here (they are set to the same as in the parent cell)
				// this code is not needed now, the wave vectors are recalculated from x/y locations, the stored ones are not used
				if (waveFrontList.size()>0) {
					for (int listIndex=0;listIndex<waveFrontList.size();listIndex++) {
						uvdir= getWaveList (waveFrontList,listIndex);
						if (debugLevel > (debugThreshold + 1)) System.out.println("<---= uvdir= "+uvdir[0]+",  "+uvdir[1]+",  "+uvdir[2]);
						thisCell=PATTERN_GRID[uvdir[1]][uvdir[0]];
						neibBits=0;
						for (dir=0;dir<directionsUV8.length;dir++) {
							neibors[dir]=null;
							iUV[0]=uvdir[0]+directionsUV8[dir][0];
							iUV[1]=uvdir[1]+directionsUV8[dir][1];
							if ((iUV[0]<0) || (iUV[1]<0) ||
									(iUV[0]>=distortionParameters.gridSize) || (iUV[1]>=distortionParameters.gridSize)) continue; // don't fit into UV grid
							if (isCellValid(PATTERN_GRID,iUV)) {
								neibors[dir]= new double [2][2];
								otherCell=PATTERN_GRID[iUV[1]][iUV[0]];
								neibors[dir][0][0]=0.5*directionsUV8[dir][0];  // u
								neibors[dir][0][1]=0.5*directionsUV8[dir][1];  // v
								neibors[dir][1][0]=otherCell[0][0]-thisCell[0][0];  // x
								neibors[dir][1][1]=otherCell[0][1]-thisCell[0][1];  // y
								neibBits |= directionsBits8[dir];
							}
						}
						int i=Integer.bitCount(neibBits);
						if (debugLevel > (debugThreshold + 1)) System.out.println("neibBits="+neibBits+", number of bits= "+i);
						if (i>1) {
							double[][] wv= waveVectorsFromNeib(neibors);
							setPatternGridCell(
									PATTERN_GRID,
									uvdir,
									null, // XY already set
									wv[0],
									wv[1]);

							if (debugLevel > (debugThreshold + 1)) System.out.println("==+> number of bits:"+i+
									" vw00="+IJ.d2s(wv[0][0],5)+" vw01="+IJ.d2s(wv[0][1],5)+
									" vw10="+IJ.d2s(wv[1][0],5)+" vw11="+IJ.d2s(wv[1][1],5));
							//				    	wv= WaveVectorsFromNeib(neibors);
							//
							// 		   vectors: [num_vector][0][0] - U
							//                  [num_vector][0][1] - V
							//                  [num_vector][1][0] - X
							//                  [num_vector][1][1] - Y
							//                  [num_vector] == null - skip
							//
						}
Andrey Filippov's avatar
Andrey Filippov committed
4194

4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242
					}
				} else if (initialWave!=null){
					if ((global_debug_level > (debugThreshold + 1)) && (initialWave!=null)) {
						System.out.println("No sense to initiate clenaup during first layer"); // problems heer?
					}
				} else if (!cleanup.get() || (addedCells.get()>0)) { // create list of the defined cells on the border (if wave died)
					cleanup.set(true);
					// debug
					//					   if ((global_debug_level>0) && (initialWave!=null)) {
					//						   System.out.println("clenaup during first layer"); // problems heer?
					//						   System.out.println("Added "+addedCells.get()+" during border cleanup on first layer");
					//					   }
					if ((debugLevel > (debugThreshold + 1)) && !cleanup.get())  System.out.println("Added "+addedCells.get()+" during border cleanup"); // can not get here
					addedCells.set(0);
					umax=0;
					vmax=0;
					vmin=PATTERN_GRID.length;
					umin=PATTERN_GRID[0].length;
					for (int i=0;i<PATTERN_GRID.length;i++) for (int j=0;j<PATTERN_GRID[i].length;j++) {
						if ((PATTERN_GRID[i][j]!=null) && (PATTERN_GRID[i][j][0]!=null)) {
							if (vmin > i) vmin = i;
							if (vmax < i) vmax = i;
							if (umin > j) umin = j;
							if (umax < j) umax = j;
						}
					}
					int [] uvNew=new int [2];
					for (uvNew[1]=vmin;uvNew[1]<=vmax;uvNew[1]++) for (uvNew[0]=umin;uvNew[0]<=umax;uvNew[0]++) if (isCellDefined(PATTERN_GRID,uvNew)){
						for (dir=0;dir<directionsUV.length;dir++) {
							iUV[0]=uvNew[0]+directionsUV[dir][0];
							iUV[1]=uvNew[1]+directionsUV[dir][1];
							if (!isCellDefined(PATTERN_GRID,iUV) && !isCellDeleted(PATTERN_GRID,iUV)){
								putInWaveList (waveFrontList, uvNew, dir); // direction does not matter here
								break;
							}
						}
					}
					if (global_debug_level > (debugThreshold + 1)) System.out.println("***** Starting cleanup, wave length="+waveFrontList.size()); //????
				}
				// end of layer - it is a hack below, marking initial wave to recalculate it from neighbors
				if (initialWave!=null){ // just after the first layer (usually one cell) - delete it and add next time - otherwise first one needs large correction
					if (global_debug_level > (debugThreshold + 1)) {
						System.out.println("Removing "+initialWave.size()+" initial wave cells, waveFrontList.size()="+waveFrontList.size());
						for (int listIndex=0;listIndex<waveFrontList.size();listIndex++) {
							int [] dbg_uvdir= getWaveList (waveFrontList,listIndex);
							System.out.println("waveFrontList["+listIndex+"]: "+dbg_uvdir[0]+"/"+dbg_uvdir[1]+" dir="+dbg_uvdir[2]);
						}
					}
4243

4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256
					while (initialWave.size()>0){
						uvdir= getWaveList (initialWave,0);
						//						   clearPatternGridCell(PATTERN_GRID, uvdir);
						if (global_debug_level > (debugThreshold + 1))
							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;
			/*
4257 4258 4259
		   if (updating){
			   return PATTERN_GRID; // no need to crop the array, it should not change
		   }
4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274
			 */
			umax=0;
			vmax=0;
			vmin=PATTERN_GRID.length;
			umin=PATTERN_GRID[0].length;
			numDefinedCells=0;
			for (int i=0;i<PATTERN_GRID.length;i++) for (int j=0;j<PATTERN_GRID[i].length;j++) {
				if ((PATTERN_GRID[i][j]!=null) && (PATTERN_GRID[i][j][0]!=null)) {
					if (vmin > i) vmin = i;
					if (vmax < i) vmax = i;
					if (umin > j) umin = j;
					if (umax < j) umax = j;
					numDefinedCells++;
				}
			}
4275

4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311
			//			   if (updating){
			//				   return numDefinedCells; // no need to crop the array, it should not change
			//			   }
			if (!updating){
				if (vmin>vmax){
					this.PATTERN_GRID=null;
					continue; // try next in queue if available
					//					   return 0; // null; // nothing found
				}
				// Add extra margins for future extrapolation
				int extra=distortionParameters.numberExtrapolated -((distortionParameters.removeLast)?1:0);
				vmin-=extra;
				if (vmin<0) vmin=0;
				umin-=extra;
				if (umin<0) umin=0;
				vmax+=extra;
				if (vmax>=PATTERN_GRID.length) vmax=PATTERN_GRID.length-1;
				umax+=extra;
				if (umax>=PATTERN_GRID[0].length) umax=PATTERN_GRID[0].length-1;

				// make sure the odd/even uv does not change (and so the cross phases defined by U & V)
				vmin &= ~1;
				umin &= ~1;
				// make width/height even (not needed)
				umax |=1;
				vmax |=1;
				// remove  margins
				double [][][][] result = new double [vmax-vmin+1][umax-umin+1][][];
				for (int i=vmin;i<=vmax;i++) for (int j=umin;j<=umax;j++) {
					if ((PATTERN_GRID[i][j]!=null) && (PATTERN_GRID[i][j][0]!=null)) {
						result[i-vmin][j-umin]=PATTERN_GRID[i][j];
					} else result[i-vmin][j-umin]=null;
				}
				this.debugUV[0]-=umin;
				this.debugUV[1]-=umin;
				if (debugLevel>(debugThreshold+2)) System.out.println("debugUV[] updated to {"+this.debugUV[0]+","+this.debugUV[1]+"}");
4312

4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364
				if (debugLevel > (debugThreshold+1)) System.out.println("Total number of defined cells="+numDefinedCells);
				this.PATTERN_GRID=result;
			}
			// more tests here (moved from the caller) that result is good
			double averageGridPeriod=Double.NaN;
			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 > debugThreshold){
				System.out.println("Pattern period="+averageGridPeriod+" {"+gridPeriods[0]+","+gridPeriods[1]+"}"+
						" limits are set to :"+patternDetectParameters.minGridPeriod+","+patternDetectParameters.maxGridPeriod);
			}
			if (!Double.isNaN(averageGridPeriod)) {
				if (!Double.isNaN(patternDetectParameters.minGridPeriod) &&
						(patternDetectParameters.minGridPeriod>0.0) &&
						(averageGridPeriod<patternDetectParameters.minGridPeriod)){
					if (debugLevel>0){
						System.out.println("Pattern is too small, period="+averageGridPeriod+
								" minimal="+patternDetectParameters.minGridPeriod);
					}
					continue; // bad grid
				}
				if (!Double.isNaN(patternDetectParameters.maxGridPeriod) &&
						(patternDetectParameters.maxGridPeriod>0.0) &&
						(averageGridPeriod>patternDetectParameters.maxGridPeriod)){
					if (debugLevel>0){
						System.out.println("Pattern is too large, period="+averageGridPeriod+
								" maximal="+patternDetectParameters.maxGridPeriod);
					}
					continue; // bad grid
				}
			}
			if (
					(minimal_pattern_cluster <= 0) || // minimal cluster size is disabled
					(distortionParameters.scaleMinimalInitialContrast<=0) || // minimal cluster size is disabled
					((numDefinedCells  == 0) && fromVeryBeginning)|| // no cells detected at all, starting from the very beginning
					(numDefinedCells   >= minimal_pattern_cluster)  // detected enough cells
					)  {
				return numDefinedCells;
			}
			if ( (numDefinedCells < minimal_pattern_cluster) &&
					(numDefinedCells > 10))   // detected enough cells
			{
				if (global_debug_level > (debugThreshold-1)) {
					System.out.println("***** Initial cluster has "+numDefinedCells+ " cells that is less than "+
							"minimal_pattern_cluster = "+minimal_pattern_cluster+
							" *****");
				}
				//				   return numDefinedCells;
			}
4365 4366


4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419
			if (roi!=null){ // don't use this feature with ROI as it can be small
				if (global_debug_level>0) System.out.println("Initial pattern cluster is small ("+numDefinedCells+"), but ROI is set - no retries");
				{
					return numDefinedCells;
				}
			}
		} // next node in queue
		return 0; // none
	}
	// ================= end of public int distortions() ===================

	public double [][] findPatternCandidate_old(
			//			   final int [] startScanIndex, // [0] will be updated
			final boolean [] triedIndices, // which indices are already tried
			final int startScanIndex,
			final int tryHor,
			final int tryVert,
			//			   final int numTries,
			final Rectangle selection,
			final DistortionParameters distortionParameters, //
			final MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			final double    min_half_period,
			final double    max_half_period,
			final SimulationPattern.SimulParameters  thisSimulParameters,
			final MatchSimulatedPattern matchSimulatedPattern,
			final MatchSimulatedPattern matchSimulatedPatternCorr,
			final SimulationPattern simulationPattern,
			final boolean equalizeGreens,
			final ImagePlus imp, // image to process
			final double [] bPattern,
			final double [] windowFunction,
			final double [] windowFunctionCorr,
			final double [] windowFunctionCorr2,
			final double [] windowFunctionCorr4,
			final double[][] locsNeib, // which neibors to try (here - just the center)
			final int threadsMax,
			final boolean updateStatus,
			final int debugLevel
			){
		final Thread[] threads = newThreadArray(threadsMax);
		//		   final AtomicInteger seqNumber = new AtomicInteger(3);
		//		   final AtomicInteger seqNumber = new AtomicInteger(startScanIndex[0]);
		final AtomicInteger seqNumber = new AtomicInteger(startScanIndex);
		//		   startScanIndex
		final AtomicBoolean nodeSet=new AtomicBoolean(false);
		final double [][][] nodeRef= new double[1][][];
		nodeRef[0]=null;
		//		   System.out.println("===== findPatternCandidate(): startScanIndex="+startScanIndex);
		//		   for (int i=0;i<triedIndices.length;i++) System.out.print(triedIndices[i]?"+":"-");
		//		   System.out.println();
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				@Override
4420
				public void run() {
4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497
					int nbh, nbv, nh, nv, nb;
					double [] point = new double[2];
					DoubleFHT doubleFHT = new DoubleFHT();
					//					   for (int n=seqNumber.getAndIncrement(); n< numTries;n=seqNumber.getAndIncrement()){
					for (int n=seqNumber.getAndIncrement(); n<(triedIndices.length-1); n=seqNumber.getAndIncrement()) if (!triedIndices[n]){
						if (nodeSet.get()) break; // already set
						nbh=tryHor-1;
						nbv=tryVert-1;
						nh=0;
						nv=0;
						nb=0;
						while (nb<(tryHor+tryVert)) {
							if (nbh>=0) {
								if ((n & (1<<nb))!=0) nh |= 1<<nbh;
								nbh--;
								nb++;
							}
							if (nbv>=0) {
								if ((n & (1<<nb))!=0) nv |= 1<<nbv;
								nbv--;
								nb++;
							}
						}
						if (debugLevel>2) System.out.println("Searching, n="+n+", nv="+nv+", nh="+nh+", nb="+nb);
						if ((nv>0) && (nh>0)) {
							point[0]=(selection.x+nh*selection.width/(1<<tryHor)) & ~1;
							point[1]=(selection.y+nv*selection.height/(1<<tryVert)) & ~1;
							if (debugLevel>2) System.out.println("trying xc="+point[0]+", yc="+point[1]+"(nv="+nv+", nh="+nh+")");
							//							   if ((debugLevel>2) && (n==3)) debugLevel=3; // show debug images for the first point
							double [][] node=tryPattern (
									null, // LwirReaderParameters lwirReaderParameters, // null is OK
									doubleFHT,
									point, // xy to try
									distortionParameters, //no control of the displacement
									patternDetectParameters,
									min_half_period,
									max_half_period,
									thisSimulParameters,
									matchSimulatedPattern,
									matchSimulatedPatternCorr,
									simulationPattern,
									equalizeGreens,
									imp, // image to process
									bPattern,
									windowFunction,
									windowFunctionCorr,
									windowFunctionCorr2,
									windowFunctionCorr4,
									locsNeib, // which neibors to try (here - just the center)
									null // dbgStr
									);
							if ((node!=null) && (node[0]!=null)) {
								if (nodeSet.compareAndSet(false,true)) {
									nodeRef[0]=node;
									//									   startScanIndex[0]=seqNumber.get();
									triedIndices[n]=true; //found and will be processed
									if (debugLevel>1)  System.out.println("probing "+n);
									break;
								} else {
									if (debugLevel>1)  System.out.println("missed "+n);
								}
							} else {
								triedIndices[n]=true; // tried, but nothing found
								//								   System.out.println("empty "+n);
							}
						} else {
							triedIndices[n]=true; // tried, but nothing found
							//							   System.out.println("wrong "+n);
						}
					}
				}
			};
		}
		startAndJoin(threads);
		//		   if (nodeRef[0]==null) startScanIndex[0]=numTries; // all used
		return nodeRef[0];
	}
4498

4499 4500 4501 4502 4503 4504 4505 4506 4507
	class GridNode {
		double [][] node;
		public GridNode(double [][] node){
			this.node=node;
		}
		public double [][] getNode(){
			return this.node;
		}
	}
4508

4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544
	private  Queue<GridNode> findPatternCandidates(
			final LwirReaderParameters lwirReaderParameters, // null is OK
			final boolean [] triedIndices, // which indices are already tried
			final int startScanIndex,
			final int tryHor,
			final int tryVert,
			final Rectangle selection,
			final DistortionParameters distortionParameters, //
			final MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			final double    min_half_period,
			final double    max_half_period,
			final SimulationPattern.SimulParameters  thisSimulParameters,
			final MatchSimulatedPattern matchSimulatedPattern,
			final MatchSimulatedPattern matchSimulatedPatternCorr,
			final SimulationPattern simulationPattern,
			final boolean equalizeGreens,
			final ImagePlus imp, // image to process
			final double [] bPattern,
			final double [] windowFunction,
			final double [] windowFunctionCorr,
			final double [] windowFunctionCorr2,
			final double [] windowFunctionCorr4,
			final double[][] locsNeib, // which neibors to try (here - just the center)
			final int threadsMax,
			final boolean updateStatus,
			final int debugLevel
			){
		final int debugThreshold=2; // -1; // 1; ** Restore 1
		if (debugThreshold < 0) {
			System.out.println("findPatternCandidates(): debugThreshold < 0 - restore when done");
		}
		if ((debugLevel>debugThreshold) && ((debugLevel>1) || (startScanIndex>3))) {
			int debugNumLeft=0;
			for (boolean b:triedIndices) if (!b) debugNumLeft++;
			System.out.println("findPatternCandidates(), startScanIndex= "+startScanIndex+",triedIndices.length="+triedIndices.length+" debugNumLeft="+debugNumLeft);
		}
4545

4546 4547
		final Thread[] threads = newThreadArray(threadsMax);
		final AtomicInteger seqNumber = new AtomicInteger(startScanIndex);
4548

4549
		final AtomicInteger debugNumThreadAtomic= new AtomicInteger(0);
4550

4551 4552 4553 4554
		final Queue<GridNode> nodeQueue = new ConcurrentLinkedQueue<GridNode>();
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				@Override
4555
				public void run() {
4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625
					int nbh, nbv, nh, nv, nb;
					double [] point = new double[2];
					DoubleFHT doubleFHT = new DoubleFHT();
					int debugNumThread=debugNumThreadAtomic.getAndIncrement();
					for (int n=seqNumber.getAndIncrement(); n<(triedIndices.length-1); n=seqNumber.getAndIncrement()) if (!triedIndices[n]){
						if (!nodeQueue.isEmpty()) break; // already set at least one element - does it work?
						nbh=tryHor-1;
						nbv=tryVert-1;
						nh=0;
						nv=0;
						nb=0;
						while (nb<(tryHor+tryVert)) {
							if (nbh>=0) {
								if ((n & (1<<nb))!=0) nh |= 1<<nbh;
								nbh--;
								nb++;
							}
							if (nbv>=0) {
								if ((n & (1<<nb))!=0) nv |= 1<<nbv;
								nbv--;
								nb++;
							}
						}
						if (debugLevel>2) System.out.println("Searching, n="+n+", nv="+nv+", nh="+nh+", nb="+nb );
						if ((nv>0) && (nh>0)) {
							point[0]=(selection.x+nh*selection.width/(1<<tryHor)) & ~1;
							point[1]=(selection.y+nv*selection.height/(1<<tryVert)) & ~1;
							if (debugLevel>2) System.out.println("trying xc="+point[0]+", yc="+point[1]+"(nv="+nv+", nh="+nh+")");
							if (debugLevel>2) System.out.println(debugNumThread+":"+n+" >> ");
							double [][] node=tryPattern (
									lwirReaderParameters, // LwirReaderParameters lwirReaderParameters, // null is OK
									doubleFHT,
									point, // xy to try
									distortionParameters, //no control of the displacement
									patternDetectParameters,
									min_half_period,
									max_half_period,
									thisSimulParameters,
									matchSimulatedPattern,
									matchSimulatedPatternCorr,
									simulationPattern,
									equalizeGreens,
									imp, // image to process
									bPattern,
									windowFunction,
									windowFunctionCorr,
									windowFunctionCorr2,
									windowFunctionCorr4,
									locsNeib, // which neighbors to try (here - just the center)
									(debugLevel>debugThreshold)?(""+debugNumThread+":"+n+", nv="+nv+", nh="+nh+", nb="+nb+" "+point[0]+"/"+point[1]):null
									);
							if ((node!=null) && (node[0]!=null)) {
								nodeQueue.add(new GridNode(node));
								if (debugLevel>debugThreshold-1)  System.out.println("adding candidate "+n+" x0="+point[0]+" y0="+point[1]+" -> "+ node[0][0]+"/"+node[0][1]+" seqNumber.get()="+seqNumber.get()+" n="+n);
							}
						} else {
							if (debugLevel>debugThreshold) System.out.println("-----"+debugNumThread+":"+n+", nv="+nv+", nh="+nh);
						}
						triedIndices[n]=true; // regardless - good or bad
					}
				}
			};
		}
		startAndJoin(threads);
		if (debugLevel>debugThreshold){
			System.out.println("seqNumber after join is "+seqNumber.get());
		}
		if (seqNumber.get()>=(triedIndices.length-1) ) triedIndices[triedIndices.length-1]=true; // all tried
		return nodeQueue; // never null, may be empty
	}
4626

4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659
	/* ================================================================*/
	public void scaleContrast(double scale){
		for (double [][][] patternRow:this.PATTERN_GRID){
			if (patternRow!=null) for (double [][] node:patternRow){
				if ((node!=null) && (node.length>0) && (node[0]!=null) && (node[0].length>2)) {
					node[0][2]*=scale;
				}
			}
		}
	}
	/* ================================================================*/
	public double refineDistortionCorrelation (
			final LwirReaderParameters lwirReaderParameters, // null is OK
			final DistortionParameters distortionParameters, //
			final MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			final SimulationPattern.SimulParameters  simulParameters,
			final boolean equalizeGreens,
			final ImagePlus imp, // image to process
			final double maxCorr, // maximal allowed correction, in pixels (0.0) - any
			final int threadsMax,
			final boolean updateStatus,
			final int debug_level){// debug level used inside loops
		scaleContrast(distortionParameters.scaleFirstPassContrast);
		final double [][][][] patternGrid=this.PATTERN_GRID;
		final int debugThreshold=1;
		final Rectangle selection=new Rectangle(0, 0, imp.getWidth(), imp.getHeight());
		MatchSimulatedPattern matchSimulatedPatternCorr=new MatchSimulatedPattern(distortionParameters.correlationSize);
		matchSimulatedPatternCorr.debugLevel=debugLevel;
		SimulationPattern simulationPattern= new SimulationPattern();
		final SimulationPattern.SimulParameters  thisSimulParameters=simulParameters.clone();
		thisSimulParameters.subdiv=distortionParameters.patternSubdiv;
		final double [] bPattern= simulationPattern.patternGenerator(simulParameters); // reuse pattern for next time
		/*
Andrey Filippov's avatar
Andrey Filippov committed
4660 4661 4662 4663 4664
			final double [] windowFunctionCorr= initWindowFunction(  distortionParameters.correlationSize,distortionParameters.correlationGaussWidth);
			final double [] windowFunctionCorr2=initWindowFunction(2*distortionParameters.correlationSize,
					 (distortionParameters.absoluteCorrelationGaussWidth?0.5:1.0)*distortionParameters.correlationGaussWidth);
			final double [] windowFunctionCorr4=initWindowFunction(4*distortionParameters.correlationSize,
					 (distortionParameters.absoluteCorrelationGaussWidth?0.25:1.0)*distortionParameters.correlationGaussWidth);
4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707
		 */
		final double [] windowFunctionCorr= initWindowFunction(
				distortionParameters.correlationSize,
				distortionParameters.correlationGaussWidth,
				distortionParameters.zeros);
		final double [] windowFunctionCorr2=initWindowFunction(
				2*distortionParameters.correlationSize,
				(distortionParameters.absoluteCorrelationGaussWidth?0.5:1.0)*distortionParameters.correlationGaussWidth,
				distortionParameters.zeros);
		final double [] windowFunctionCorr4=initWindowFunction(
				4*distortionParameters.correlationSize,
				(distortionParameters.absoluteCorrelationGaussWidth?0.25:1.0)*distortionParameters.correlationGaussWidth,
				distortionParameters.zeros);
		final int height=patternGrid.length;
		final int width=(height>0)?patternGrid[0].length:0; // oob 0??
		final Thread[] threads = newThreadArray(threadsMax);
		int was_debug_level=debugLevel;
		final int debugOnLevel=(debug_level>0)?3:0;
		final double [][] locsNeib=calcNeibLocsWeights (
				distortionParameters,
				distortionParameters.correlationAverageOnRefine);
		debugLevel=debug_level;
		if (debugLevel>1)  System.out.println("Refining correlations, width= "+width+", height= "+height);
		final double [][] extrapolationWeights=generateWeights (
				distortionParameters.correlationWeightSigma,
				distortionParameters.correlationRadiusScale); //  if 0 - use sigma as radius, inside - 1.0, outside 0.0. If >0 - size of array n*sigma
		int i=-1;
		int [] iUV=  new int [2];

		for (i=0;i<(width*height);i++) {
			iUV[0]=i % width;
			iUV[1]=i / width;
			if (isCellDefined(patternGrid,iUV)) break;
		}
		if (i<0) return Double.NaN; // no defined nodes at all
		final int startCell=i;
		final AtomicInteger cellNum = new AtomicInteger(startCell);
		final AtomicInteger cellNumDoneAtomic = new AtomicInteger(startCell);
		int dc=i;
		//Debug only
		if (debugOnLevel>debugThreshold) {
			final int [][] directionsUV8= {{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,1},{-1,-1},{1,-1}}; // first 8 should be the same as in directionsUV
			for (i=dc;i<(width*height);i++) {
Andrey Filippov's avatar
Andrey Filippov committed
4708 4709
				iUV[0]=i % width;
				iUV[1]=i / width;
4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739
				if ((iUV[0]>0) && (iUV[1]>0) && (iUV[0]<(width-1)) && (iUV[1]<(height-1)) &&
						isCellDefined(patternGrid,iUV) &&
						isCellDefined(patternGrid,matrix2x2_add(iUV,directionsUV8[0])) &&
						isCellDefined(patternGrid,matrix2x2_add(iUV,directionsUV8[1])) &&
						isCellDefined(patternGrid,matrix2x2_add(iUV,directionsUV8[2])) &&
						isCellDefined(patternGrid,matrix2x2_add(iUV,directionsUV8[3])) &&
						isCellDefined(patternGrid,matrix2x2_add(iUV,directionsUV8[4])) &&
						isCellDefined(patternGrid,matrix2x2_add(iUV,directionsUV8[5])) &&
						isCellDefined(patternGrid,matrix2x2_add(iUV,directionsUV8[6])) &&
						isCellDefined(patternGrid,matrix2x2_add(iUV,directionsUV8[7]))) {
					dc=i;
					System.out.println("not used: debug U="+iUV[0]+" V="+iUV[1]+" index="+dc);
					break;
				}
			}
		}

		final int debugCell=debugUV[0]+debugUV[1]*width;
		/**
		 * That was wrong to update currently calculated grid, so the newGrid will be calculated instead, then copied altogether
		 */
		final double [][][] newGrid=new double [height][width][];
		final boolean refineInPlace=distortionParameters.refineInPlace;
		for (int v=0;v<height;v++) for (int u=0;u<width;u++) newGrid[v][u]=null;
		IJ.showProgress(0);
		if (updateStatus) IJ.showStatus("Refining correlations");
		//           MinMaxSync  minMaxSync=new MinMaxSync();
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				@Override
4740
				public void run() {
4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808
					SimulationPattern simulationPattern= new SimulationPattern(bPattern);
					MatchSimulatedPattern matchSimulatedPatternCorr=new MatchSimulatedPattern(distortionParameters.correlationSize);
					DoubleFHT fht_instance =new DoubleFHT(); // provide DoubleFHT instance to save on initializations (or null)
					int [] iUV=  new int [2];
					boolean nowDebugCell=false;
					for (int ncell=cellNum.getAndIncrement(); ncell<(width*height);ncell=cellNum.getAndIncrement()){
						nowDebugCell=(ncell==debugCell);
						int thisDebug= (nowDebugCell)?debugOnLevel:debugLevel;
						iUV[0]=ncell % width;
						iUV[1]=ncell / width;

						if (nowDebugCell && (thisDebug>1))  System.out.println(">>>>>>>>>>> Debug cell, thisDebug="+thisDebug+", iUV={"+iUV[0]+","+iUV[1]+"}");
						//        				   if ((updateStatus) && (iUV[0]==0)) IJ.showStatus("Refining correlations, row "+(iUV[1]+1)+" of "+height);
						if ((thisDebug>1) && ((iUV[0]==0) || (nowDebugCell))) System.out.println("Refining correlations, row "+(iUV[1]+1)+" of "+height);
						if (!isCellDefined(patternGrid,iUV)) {
							cellNumDoneAtomic.getAndIncrement();
							continue;
						}
						Rectangle centerCross=correlationSelection(
								patternGrid[iUV[1]][iUV[0]][0], // initial coordinates of the pattern cross point
								distortionParameters.correlationSize);
						if (!selection.contains(centerCross)) {
							cellNumDoneAtomic.getAndIncrement();
							continue; // the correlation selection does not fit into WOI selection ??? WOI is now full image
						}
						//Proceed with correlation
						//TODO: add contrast verification ? Maximal distance from expected? (return null if failed)

						double [][] simulPars=getSimulationParametersFromGrid(
								PATTERN_GRID,
								iUV,          // U,V of the center point (for which the simulation pattern should be built
								null,          // x,y of the center point (or null to use grid)
								extrapolationWeights, // quadrant of sample weights
								!distortionParameters.useQuadratic,  // use linear approximation (instead of quadratic)
								1.0E-10,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
								1.0E-20  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
								);
						if ((thisDebug>debugThreshold) && (simulPars!=null)) {
							String dbgStr="";
							dbgStr+=" {"+IJ.d2s(simulPars[0][0],5)+"/"+IJ.d2s(simulPars[0][1],5)+"/"+IJ.d2s(simulPars[0][2],5);
							if (simulPars[0].length>3) dbgStr+="/"+IJ.d2s(simulPars[0][3],7)+"/"+IJ.d2s(simulPars[0][4],7)+"/"+IJ.d2s(simulPars[0][5],7)+"}";
							dbgStr+=" {"+IJ.d2s(simulPars[1][0],5)+"/"+IJ.d2s(simulPars[1][1],5)+"/"+IJ.d2s(simulPars[1][2],5);
							if (simulPars[1].length>3) dbgStr+="/"+IJ.d2s(simulPars[1][3],7)+"/"+IJ.d2s(simulPars[1][4],7)+"/"+IJ.d2s(simulPars[1][5],7)+"}";
							System.out.println(dbgStr);
							if (nowDebugCell && (thisDebug>3)) {
								double [] XY={PATTERN_GRID[iUV[1]][iUV[0]][0][0]-32.0,PATTERN_GRID[iUV[1]][iUV[0]][0][1]-32.0};
								System.out.println("iUV[0]="+iUV[0]+"iUV[1]="+iUV[1]);
								System.out.println("CC : "+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]  ][0][0]-XY[0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]  ][0][1]-XY[1],3));
								System.out.println("TL : "+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]-1][0][0]-XY[0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]-1][0][1]-XY[1],3)); // sometimes throws
								System.out.println("TC : "+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]  ][0][0]-XY[0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]  ][0][1]-XY[1],3));
								System.out.println("TR : "+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]+1][0][0]-XY[0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]+1][0][1]-XY[1],3));
								System.out.println("CR : "+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]+1][0][0]-XY[0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]+1][0][1]-XY[1],3));
								System.out.println("BR : "+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]+1][0][0]-XY[0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]+1][0][1]-XY[1],3));
								System.out.println("BC : "+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]  ][0][0]-XY[0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]  ][0][1]-XY[1],3));
								System.out.println("BL : "+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]-1][0][0]-XY[0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]-1][0][1]-XY[1],3));
								System.out.println("CL : "+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]-1][0][0]-XY[0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]-1][0][1]-XY[1],3));

								System.out.println("CC : "+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]  ][0][0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]  ][0][1],3));
								System.out.println("TL : "+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]-1][0][0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]-1][0][1],3));
								System.out.println("TC : "+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]  ][0][0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]  ][0][1],3));
								System.out.println("TR : "+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]+1][0][0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]-1][iUV[0]+1][0][1],3));
								System.out.println("CR : "+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]+1][0][0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]+1][0][1],3));
								System.out.println("BR : "+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]+1][0][0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]+1][0][1],3));
								System.out.println("BC : "+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]  ][0][0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]  ][0][1],3));
								System.out.println("BL : "+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]-1][0][0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]+1][iUV[0]-1][0][1],3));
								System.out.println("CL : "+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]-1][0][0],3)+"/"+IJ.d2s(PATTERN_GRID[iUV[1]  ][iUV[0]-1][0][1],3));
							}
						}
Andrey Filippov's avatar
Andrey Filippov committed
4809

4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853
						//							if (nowDebugCell)correctedPatternCrossLocationAverage4(

						double [] centerXY=correctedPatternCrossLocation(
								lwirReaderParameters, // LwirReaderParameters lwirReaderParameters, // null is OK
								patternGrid[iUV[1]][iUV[0]][0], // initial coordinates of the pattern cross point
								patternGrid[iUV[1]][iUV[0]][1][0],
								patternGrid[iUV[1]][iUV[0]][1][1],
								patternGrid[iUV[1]][iUV[0]][2][0],
								patternGrid[iUV[1]][iUV[0]][2][1],
								simulPars,
								imp,      // image data (Bayer mosaic)
								distortionParameters, //
								patternDetectParameters,
								matchSimulatedPatternCorr, // correlationSize
								thisSimulParameters,
								equalizeGreens,
								windowFunctionCorr,
								windowFunctionCorr2,
								windowFunctionCorr4,
								simulationPattern,
								((iUV[0]^iUV[1])&1)!=0, // if true - invert pattern
								fht_instance,
								distortionParameters.fastCorrelationOnFinalPass, //
								locsNeib,
								thisDebug, //thisDebug
								null);


						if (centerXY!=null){
							if (thisDebug>2) System.out.println("==>iUV={"+iUV[0]+",  "+iUV[1]+
									"}. "+patternGrid[iUV[1]][iUV[0]][0][0]+" / "+patternGrid[iUV[1]][iUV[0]][0][1]+" -> "+centerXY[0]+" / "+centerXY[1]);
							if (refineInPlace)	setPatternGridCell(
									patternGrid,
									iUV,
									centerXY,
									null, //  double [] wv1,
									null); //  double [] wv2);
							else newGrid[iUV[1]][iUV[0]]=centerXY.clone();

						} else {
							if (debug_level>0){
								System.out.println("refineDistortionCorrelation(): failed to refine grid for U="+iUV[0]+" V="+iUV[1]+
										" X="+patternGrid[iUV[1]][iUV[0]][0][0]+" Y="+patternGrid[iUV[1]][iUV[0]][0][1]);
							}
Andrey Filippov's avatar
Andrey Filippov committed
4854

4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900
						}
						final int numFinished=cellNumDoneAtomic.getAndIncrement();
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								// Here, we can safely update the GUI
								// because we'll be called from the
								// event dispatch thread
								IJ.showProgress(numFinished,width*height-1);
							}
						});
					}
				}
			};
		}
		startAndJoin(threads);
		IJ.showProgress(1.0); // turn off
		double maxActualCorr=0.0;
		double dx,dy,dist;
		if (!refineInPlace) {
			//maxCorr
			if (maxCorr>0.0) {
				// make sure there are no new undefined cells that were initially defined and no correction more than the limit
				int numUndefined=0,numFar=0;
				//					double maxCorr2=maxCorr*maxCorr;
				for (iUV[1]=0;iUV[1]<height;iUV[1]++) for (iUV[0]=0;iUV[0]<width;iUV[0]++) if (isCellDefined(patternGrid, iUV)){
					if (newGrid[iUV[1]][iUV[0]]==null){
						numUndefined++;
					} else {
						dx=newGrid[iUV[1]][iUV[0]][0] - patternGrid[iUV[1]][iUV[0]][0][0];
						dy=newGrid[iUV[1]][iUV[0]][1] - patternGrid[iUV[1]][iUV[0]][0][1];
						dist=Math.sqrt(dx*dx+dy*dy);
						if (dist>maxActualCorr) maxActualCorr=dist;
						if (dist>maxCorr) {
							numFar++;
							newGrid[iUV[1]][iUV[0]]=null;
						}
					}
				}
				if ((numUndefined>0) || (numFar>0)) {
					if (debug_level>0){
						System.out.println("refineDistortionCorrelation(): failed, number of undefined cells="+numUndefined+", number of too far cells="+numFar+" maxActualCorr="+maxActualCorr );
					}
					if (numUndefined>0) return -numUndefined; // negative - some cells undefined, no info about maximal correction returned
					return maxActualCorr; // no correction performed
				}
Andrey Filippov's avatar
Andrey Filippov committed
4901

4902 4903 4904 4905 4906 4907 4908 4909
			} else {
				// only calculate maximal distance
				for (iUV[1]=0;iUV[1]<height;iUV[1]++) for (iUV[0]=0;iUV[0]<width;iUV[0]++) if (isCellDefined(patternGrid, iUV) && (newGrid[iUV[1]][iUV[0]]!=null)){
					dx=newGrid[iUV[1]][iUV[0]][0] - patternGrid[iUV[1]][iUV[0]][0][0];
					dy=newGrid[iUV[1]][iUV[0]][1] - patternGrid[iUV[1]][iUV[0]][0][1];
					dist=Math.sqrt(dx*dx+dy*dy);
					if (dist>maxActualCorr) maxActualCorr=dist;
				}
Andrey Filippov's avatar
Andrey Filippov committed
4910

4911 4912 4913 4914 4915 4916 4917 4918 4919
			}
			// Copy new values for the grid cells
			for (iUV[1]=0;iUV[1]<height;iUV[1]++) for (iUV[0]=0;iUV[0]<width;iUV[0]++) if (newGrid[iUV[1]][iUV[0]]!=null){
				setPatternGridCell(
						patternGrid,
						iUV,
						newGrid[iUV[1]][iUV[0]],
						null, //  double [] wv1,
						null); //  double [] wv2);
4920

4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959
			}
			// correction is only calculated for simultaneous update (not for in-place)
			if (debug_level>1){
				System.out.println("refineDistortionCorrelation(): maximal correction="+ maxActualCorr+" pixels");
			}
		}
		debugLevel=was_debug_level;
		return maxActualCorr;
	}


	public class MinMaxSync {
		private double min;
		private double max;
		private boolean defined;
		public MinMaxSync(){
			defined=false;
			min=Double.NaN;;
			max=Double.NaN;;
		}
		public void reset(){
			defined=false;
		}
		public synchronized void minMax(double d){
			if (!defined){
				min=d;
				max=d;
				defined=true;
			} else {
				if (d>max) max=d;
				else if (d<min) min=d;
			}
		}
		public double getMin() {return min;}
		public double getMax() {return max;}
		public boolean isDefined() {return defined;}
	}
	/* ================================================================*/
	/*
Andrey Filippov's avatar
Andrey Filippov committed
4960 4961 4962
        public boolean flatFieldCorrection=true; // compensate grid uneven intensity (vignetting, illumination)
        public double flatFieldExtarpolate=1.0;  // extrapolate flat field intensity map (relative to the average grid period)
        public double flatFieldBlur=1.0;   // blur the intensity map (relative to the average grid period)
4963

4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026
	 */
	public ImagePlus equalizeGridIntensity(
			ImagePlus imp,
			double [][][][] patternGrid,
			DistortionParameters distortionParameters, //
			boolean equalizeGreens,
			int debugLevel,
			boolean updateStatus,
			int threadsMax
			){
		int dbgThreshold=1;
		double [][] gridIntensity=calcGridIntensity(
				4, //bayerComponent
				distortionParameters.correlationSize, // size
				distortionParameters, //
				equalizeGreens,
				imp, // image to process
				patternGrid,
				threadsMax);// debug level used inside loops
		if (debugLevel>(dbgThreshold+2)){
			double [] testGI=new double [gridIntensity.length*gridIntensity[0].length];
			int index=0;
			for (int v=0;v<gridIntensity.length;v++) for (int u=0;u<gridIntensity[0].length;u++)testGI[index++]=gridIntensity[v][u];
			this.SDFA_INSTANCE.showArrays(testGI, gridIntensity[0].length, gridIntensity.length, imp.getTitle()+"-GI");
		}
		double [] fffg=calcFlatFieldForGrid(
				gridIntensity,
				patternGrid,
				imp.getWidth(),
				imp.getHeight());

		double averageGridPeriod=averageGridPeriod( patternGrid);

		int preShrink= (int) (averageGridPeriod * distortionParameters.flatFieldShrink);
		int expand=    (int) (averageGridPeriod * distortionParameters.flatFieldExpand);
		double extrapolateSigma=averageGridPeriod * distortionParameters.flatFieldSigmaRadius;
		double extrapolateKSigma=distortionParameters.flatFieldExtraRadius;
		if (debugLevel>=(dbgThreshold+2)){
			this.SDFA_INSTANCE.showArrays(fffg.clone(), imp.getWidth(), imp.getHeight(), imp.getTitle()+"-fffg");
		}
		extrapolatePatternFlatFieldCorrection(
				fffg, //fieldXY,
				imp.getWidth(),
				preShrink,
				expand,
				extrapolateSigma,
				extrapolateKSigma,
				threadsMax,     //   100; // testing multi-threading, limit maximal number of threads
				updateStatus);

		if (debugLevel>(dbgThreshold+2)){
			this.SDFA_INSTANCE.showArrays(fffg.clone(), imp.getWidth(), imp.getHeight(), imp.getTitle()+"-extrapolated");
		}
		if (distortionParameters.flatFieldBlur>0.0) {
			DoubleGaussianBlur gb=new DoubleGaussianBlur();
			gb.blurDouble(
					fffg,
					imp.getWidth(),
					imp.getHeight(),
					distortionParameters.flatFieldBlur*averageGridPeriod,
					distortionParameters.flatFieldBlur*averageGridPeriod,
					0.01);
		}
5027

5028 5029 5030
		double max=0.0;
		for (int i=0;i<fffg.length;i++) if (max<fffg[i]) max =fffg[i];
		double k=1.0/max;
5031

5032 5033 5034 5035
		for (int i=0;i<fffg.length;i++) {
			fffg[i]*=k;
			if (fffg[i]<distortionParameters.flatFieldMin)fffg[i]=0.0;
		}
Andrey Filippov's avatar
Andrey Filippov committed
5036

5037
		if (debugLevel>1) System.out.println("averageGridPeriod="+averageGridPeriod);
Andrey Filippov's avatar
Andrey Filippov committed
5038

5039 5040 5041
		if (debugLevel>(dbgThreshold+1)){
			this.SDFA_INSTANCE.showArrays(fffg, imp.getWidth(), imp.getHeight(), imp.getTitle()+"-blured");
		}
Andrey Filippov's avatar
Andrey Filippov committed
5042

5043
		this.flatFieldForGrid=fffg;
Andrey Filippov's avatar
Andrey Filippov committed
5044

5045 5046 5047 5048
		ImagePlus imp_eq=  applyFlatField (imp, fffg);
		if (debugLevel>dbgThreshold) imp_eq.show();
		return imp_eq;
	}
5049

5050 5051 5052 5053 5054 5055 5056 5057 5058
	public ImagePlus applyFlatField (ImagePlus imp){
		if (this.PATTERN_GRID==null) return imp;
		if ((getImageHeight()!=imp.getHeight()) || (getImageWidth()!=imp.getWidth())){
			String msg="applyFlatField (): Supplied image does not match in dimensions "+imp.getWidth()+"x"+imp.getHeight()+
					" the one for wich grid was calculated ("+getImageWidth()+"x"+getImageHeight()+")";
			//			   IJ.showMessage("Error",msg);
			throw new IllegalArgumentException (msg);
		}
		return applyFlatField (imp, this.flatFieldForGrid);
5059

5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070
	}
	public ImagePlus applyFlatField (ImagePlus imp, double [] ff){
		if (ff==null) return imp; // nothing to apply
		float [] pixels=(float []) imp.getProcessor().getPixels();

		if (pixels.length!=ff.length){
			String msg="Supplied image does not match in dimensions "+(pixels.length)+
					" the one for wich grid was calculated ("+(ff.length)+")";
			//			   IJ.showMessage("Error",msg);
			throw new IllegalArgumentException (msg);
		}
5071 5072 5073



5074 5075 5076
		float [] eqPixels=new float [pixels.length];
		for (int i=0;i<pixels.length;i++) if (ff[i]>0) eqPixels[i]=(float) (pixels[i]/ff[i]);
		else eqPixels[i]=0.0f;
Andrey Filippov's avatar
Andrey Filippov committed
5077

5078 5079 5080 5081 5082 5083
		ImageProcessor ip=new FloatProcessor(imp.getWidth(), imp.getHeight());
		ip.setPixels(eqPixels);
		ip.resetMinAndMax();
		ImagePlus imp_eq=  new ImagePlus(imp.getTitle()+"-flat", ip);
		return imp_eq;
	}
Andrey Filippov's avatar
Andrey Filippov committed
5084

5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108
	public double [][][] calcGridIntensities(
			final DistortionParameters distortionParameters, //
			final boolean equalizeGreens,
			final ImagePlus imp, // image to process
			final int threadsMax){
		double dSize=averageGridPeriod(this.PATTERN_GRID)*distortionParameters.averagingAreaScale;
		int size=  ~1 & ((int) Math.round(dSize)); // should be even
		int size4= ~1 & ((int) Math.round(dSize*Math.sqrt(2.0))); // larger when using diagonal greens (component 4)
		int [] bayerIndices={-1,1,4,2}; // -1 - contrast, 1 - R, 4 - G, 2 - B
		this.gridContrastBrightness=new double [bayerIndices.length][][];
		if (this.debugLevel>1) System.out.println("Calculating grid intensities, average period="+IJ.d2s(averageGridPeriod(this.PATTERN_GRID),2)+
				" pixels, using square sample "+size+"x"+size+" for all colors ,but (diagonal) greens, for greens - "+size4+"x"+size4);
		for (int i=0;i<bayerIndices.length;i++){
			this.gridContrastBrightness[i]=calcGridIntensity (
					bayerIndices[i], //final int bayerComponent,
					((bayerIndices[i]==4)?size4:size), //final int size,
					distortionParameters, //final DistortionParameters distortionParameters, //
					equalizeGreens,
					imp, // image to process
					this.PATTERN_GRID,
					threadsMax);
		}
		return this.gridContrastBrightness;
	}
5109 5110


5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144
	public double [][] calcGridIntensity (
			final int bayerComponent,
			final int size,
			final DistortionParameters distortionParameters, //
			final boolean equalizeGreens,
			final ImagePlus imp, // image to process
			final double [][][][] patternGrid,
			final int threadsMax){// debug level used inside loops
		final double [][] gridIntensity=new double[patternGrid.length][patternGrid[0].length];
		for (int i=0;i<gridIntensity.length;i++) for (int j=0;j<gridIntensity[0].length;j++)gridIntensity[i][j]=(bayerComponent>=0)?-1.0:0.0; // undefined
		MatchSimulatedPattern matchSimulatedPatternCorr=new MatchSimulatedPattern(distortionParameters.correlationSize);
		matchSimulatedPatternCorr.debugLevel=debugLevel;
		final double [] windowFunctionCorr= initWindowFunction(
				size, // distortionParameters.correlationSize,
				distortionParameters.correlationGaussWidth,
				distortionParameters.zeros);
		final int width=patternGrid[0].length;
		final int height=patternGrid.length;
		final Thread[] threads = newThreadArray(threadsMax);
		if (debugLevel>1)  System.out.println("Calculating average intensity at grid nodes, width= "+width+", height= "+height+ ", bayerComponent="+bayerComponent+", size="+size);
		int [] iUV=  new int [2];
		int i;
		for (i=0;i<(width*height);i++) {
			iUV[0]=i % width;
			iUV[1]=i / width;
			if (isCellDefined(patternGrid,iUV)) break;
		}
		final int startCell=i;
		final AtomicInteger cellNum = new AtomicInteger(startCell);
		final double [][][] newGrid=new double [height][width][];
		for (int v=0;v<height;v++) for (int u=0;u<width;u++) newGrid[v][u]=null;
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				@Override
5145
				public void run() {
5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170
					int [] iUV=  new int [2];
					for (int ncell=cellNum.getAndIncrement(); ncell<(width*height);ncell=cellNum.getAndIncrement()){
						iUV[0]=ncell % width;
						iUV[1]=ncell / width;
						if (!isCellDefined(patternGrid,iUV)) continue;
						if (bayerComponent>=0){
							Rectangle centerCross=correlationSelection(
									patternGrid[iUV[1]][iUV[0]][0], // initial coordinates of the pattern cross point
									size); // distortionParameters.correlationSize);
							double[][] input_bayer=splitBayer (imp,centerCross,equalizeGreens);
							double sum=0.0, sumW=0.0;
							for (int i=0;i<input_bayer[bayerComponent].length;i++){
								sum+= input_bayer[bayerComponent][i]*windowFunctionCorr[i];
								sumW+=                  windowFunctionCorr[i];
							}
							gridIntensity[iUV[1]][iUV[0]]=sum/sumW;
						} else {
							// trying alternative
							//							   double [][][][] patternGrid_same=patternGrid;
							gridIntensity[iUV[1]][iUV[0]]=Double.NaN;
							if (isCellDefined(patternGrid,iUV[0],iUV[1])) {
								double [][] patternCell=patternGrid[iUV[1]][iUV[0]];
								if (patternCell[0].length>2) gridIntensity[iUV[1]][iUV[0]]=patternCell[0][2];
							}
							/*
Andrey Filippov's avatar
Andrey Filippov committed
5171 5172 5173 5174 5175 5176
							   gridIntensity[iUV[1]][iUV[0]]=localGridContrast(
									   imp,
									   equalizeGreens,
									   patternGrid,
									   iUV[0],
									   iUV[1]);
5177 5178 5179 5180 5181 5182 5183 5184 5185 5186
							 */
						}
					}
				}
			};
		}
		startAndJoin(threads);
		return gridIntensity;
	}
	/* ======================================================================== */
5187

5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253
	public double localGridContrast(
			ImagePlus imp,
			boolean equalizeGreens,
			final double [][][][] patternGrid,
			int u,
			int v){
		if (!isCellDefined(patternGrid,u,v))   return 0.0;
		if (!isCellDefined(patternGrid,u+1,v)) return 0.0;
		if (!isCellDefined(patternGrid,u,v+1)) return 0.0;
		if (!isCellDefined(patternGrid,u-1,v)) return 0.0;
		if (!isCellDefined(patternGrid,u,v-1)) return 0.0;
		double [][] deltas={
				{0.25*(patternGrid[v+1][u][0][0]-patternGrid[v-1][u][0][0]),
					0.25*(patternGrid[v+1][u][0][1]-patternGrid[v-1][u][0][1])},
				{0.25*(patternGrid[v][u+1][0][0]-patternGrid[v][u-1][0][0]),
						0.25*(patternGrid[v][u+1][0][1]-patternGrid[v][u-1][0][1])} };
		double delta=Math.sqrt(0.5*(deltas[0][0]*deltas[0][0]+
				deltas[0][1]*deltas[0][1]+
				deltas[1][0]*deltas[1][0]+
				deltas[1][1]*deltas[1][1]));
		int range=(int) Math.round(0.25*delta); // center of the white/black;
		int [][] iDeltas={
				{(int) Math.round(deltas[0][0]),
					(int) Math.round(deltas[0][1])},
				{(int) Math.round(deltas[1][0]),
						(int) Math.round(deltas[1][1])}};

		int [][] centersOnBayer4=
			{
					{ iDeltas[0][0], iDeltas[0][1]},
					{-iDeltas[0][0],-iDeltas[0][1]},
					{ iDeltas[1][0], iDeltas[1][1]},
					{-iDeltas[1][0],-iDeltas[1][1]} };

		double diff=0.0;
		double sum=0.0;
		int maxDxy=0;
		for (int n=0;n<4;n++) for (int i=0;i<2;i++) if (centersOnBayer4[n][i]>maxDxy) maxDxy=centersOnBayer4[n][i];
		int size=2*(maxDxy+range+1); // this will include all needed pixels
		//		   size+=2;
		int hSize=size/2;
		boolean debug=false; //((u==30) && (v==30));
		Rectangle centerCross=correlationSelection(
				patternGrid[v][u][0], // initial coordinates of the pattern cross point
				size); // distortionParameters.correlationSize);

		//		   Rectangle thisSel=new Rectangle(centerCross.x,centerCross.y,2*size,2*size); // "2" - sensor pixels, befor split to components
		double[][] input_bayer=splitBayer (imp,centerCross,equalizeGreens);
		if (debug) this.SDFA_INSTANCE.showArrays(input_bayer, size,size, true, imp.getTitle()+"-bayer");

		double [] bayer4=input_bayer[4];
		for (int dv=-range;dv<=range;dv++) for (int du=-range;du<=range;du++) {
			int [] indices= new int[4];
			for (int n=0;n<4;n++) indices[n]=size*(centersOnBayer4[n][1]+dv+hSize)+(centersOnBayer4[n][0]+du+hSize);
			if ((indices[0]>bayer4.length) || (indices[1]>bayer4.length) || (indices[2]>bayer4.length) || (indices[3]>bayer4.length)
					|| (indices[0]<0) || (indices[1]<0) || (indices[2]<0) || (indices[3]<0)||
					debug){
				System.out.println("centersOnBayer4[0]={"+centersOnBayer4[0][0]+", "+centersOnBayer4[0][1]+"}");
				System.out.println("centersOnBayer4[1]={"+centersOnBayer4[1][0]+", "+centersOnBayer4[1][1]+"}");
				System.out.println("centersOnBayer4[2]={"+centersOnBayer4[2][0]+", "+centersOnBayer4[2][1]+"}");
				System.out.println("centersOnBayer4[3]={"+centersOnBayer4[3][0]+", "+centersOnBayer4[3][1]+"}");
				System.out.println("range="+range);
				System.out.println("dv="+dv+" du="+du);
				System.out.println("maxDxy="+maxDxy+" size="+size+" hSize="+hSize);
				System.out.println("indices=={"+indices[0]+", "+indices[1]+", "+indices[2]+", "+indices[3]+"}, bayer4.length="+bayer4.length);
			}
5254

Andrey Filippov's avatar
Andrey Filippov committed
5255

5256 5257
			sum+= bayer4[indices[0]]+bayer4[indices[1]]+bayer4[indices[2]]+bayer4[indices[3]];
			diff+=bayer4[indices[0]]+bayer4[indices[1]]-bayer4[indices[2]]-bayer4[indices[3]];
Andrey Filippov's avatar
Andrey Filippov committed
5258

5259 5260 5261 5262 5263
		}
		if (((u ^ v) & 1)!=0) diff=-diff;
		if (sum==0.0) return 0.0;
		return diff/sum;
	}
Andrey Filippov's avatar
Andrey Filippov committed
5264

5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284
	/* ======================================================================== */
	public double averageGridPeriod(
			double [][][][] patternGrid){
		int n=0;
		double sum=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[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++;
Andrey Filippov's avatar
Andrey Filippov committed
5285
				}
5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412
			}
		}
		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(
			double [][] gridIntensity,
			double [][][][] patternGrid,
			int sWidth,
			int sHeight){
		int width=patternGrid[0].length;
		int height=patternGrid.length;
		int [][] uvInc={{0,0},{1,0},{0,1},{1,1}}; // four corners as u,v pair
		int [][] cycles={ // counter-clockwise corners bounding the area  (only orthogonal sides?)
				{1,0,2},
				{2,3,1},
				{0,2,3},
				{3,1,0}};
		double [] fffg=   new double [sWidth*sHeight];
		int    [] fffgNum=new int [sWidth*sHeight];
		for (int i=0;i<fffg.length;i++){
			fffg[i]=0.0;
			fffgNum[i]=0;
		}
		int [] iUV=new int[2];
		for (int v=0;v<(height-1); v++) for (int u=0; u<(width-1);u++){
			double [][] cornerXY =new double[4][];
			for (int i=0;i<uvInc.length;i++){
				iUV[0]=u+uvInc[i][0];
				iUV[1]=v+uvInc[i][1];

				if (isCellDefined(patternGrid,iUV)){
					cornerXY[i]=new double[3];
					cornerXY[i][0]=patternGrid[iUV[1]][iUV[0]][0][0];
					cornerXY[i][1]=patternGrid[iUV[1]][iUV[0]][0][1];
					cornerXY[i][2]=gridIntensity[iUV[1]][iUV[0]];
				} else cornerXY[i]=null;
			}
			boolean [] cycleFits=new boolean[cycles.length];
			for (int i=0;i<cycles.length;i++){
				cycleFits[i]=true;
				for (int j=0;j<cycles[i].length;j++) if (cornerXY[cycles[i][j]]==null) {
					cycleFits[i]=false;
					break;
				}
			}
			if (cycleFits[0]&&cycleFits[1]){ // remove overlaps
				cycleFits[2]=false;
				cycleFits[3]=false;
			}
			boolean minMaxUndefined=true;
			double minX=0,maxX=0,minY=0,maxY=0;
			// find bounding rectangle;
			for (int nCycle=0;nCycle<cycles.length;nCycle++) if (cycleFits[nCycle]){
				int [] cycle=cycles[nCycle];
				for (int corner=0; corner<cycle.length;corner++){
					if (minMaxUndefined || (minX>cornerXY[cycle[corner]][0])) minX=cornerXY[cycle[corner]][0];
					if (minMaxUndefined || (maxX<cornerXY[cycle[corner]][0])) maxX=cornerXY[cycle[corner]][0];
					if (minMaxUndefined || (minY>cornerXY[cycle[corner]][1])) minY=cornerXY[cycle[corner]][1];
					if (minMaxUndefined || (maxY<cornerXY[cycle[corner]][1])) maxY=cornerXY[cycle[corner]][1];
					minMaxUndefined=false;
				}
			}
			int iMinX=(int) Math.floor(minX);
			int iMinY=(int) Math.floor(minY);
			int iMaxX=(int) Math.ceil(maxX);
			int iMaxY=(int) Math.ceil(maxY);
			if (iMinX<0) iMinX=0;
			if (iMinY<0) iMinY=0;
			if (iMaxX>=sWidth)  iMaxX=sWidth-1;
			if (iMaxY>=sHeight) iMaxY=sHeight-1;
			double [] originXY=new double [2];
			double [] endXY=new double [2];
			for (int idY=iMinY; idY<=iMaxY;idY++){
				double pY=idY; // in sensor pixels
				for (int idX=iMinX; idX<=iMaxX;idX++){
					double pX=idX; // in sensor pixels
					// scan allowed triangles, usually 2
					for (int nCycle=0;nCycle<cycles.length;nCycle++) if (cycleFits[nCycle]){
						int [] cycle=cycles[nCycle];
						// is this point inside?
						boolean inside=true;
						for (int nEdge=0;nEdge<cycle.length;nEdge++){
							int nextNEdge=(nEdge==(cycle.length-1))?0:(nEdge+1);

							originXY[0]=patternGrid[v+uvInc[cycle[nEdge]][1]][u+uvInc[cycle[nEdge]][0]][0][0];
							originXY[1]=patternGrid[v+uvInc[cycle[nEdge]][1]][u+uvInc[cycle[nEdge]][0]][0][1];
							endXY[0]=   patternGrid[v+uvInc[cycle[nextNEdge]][1]][u+uvInc[cycle[nextNEdge]][0]][0][0];
							endXY[1]=   patternGrid[v+uvInc[cycle[nextNEdge]][1]][u+uvInc[cycle[nextNEdge]][0]][0][1];
							if (((pX-originXY[0])*(endXY[1]-originXY[1]) - (pY-originXY[1])*(endXY[0]-originXY[0]))<0.0){
								inside=false;
								break;
Andrey Filippov's avatar
Andrey Filippov committed
5413
							}
5414 5415 5416
						}
						if (!inside) continue; // point is outside of the interpolation area, try next triangle (if any)
						/* interpolate:
Andrey Filippov's avatar
Andrey Filippov committed
5417 5418 5419
							1. taking cycles[0] as origin and two (non co-linear) edge vectors - V1:from 0 to 1 and V2 from 1 to 2
							    find a1 and a2  so that vector V  (from 0  to pXY) = a1*V1+ a2*V2
							2. if F0 is the value of the interpolated function at cycles[0], F1 and F2 - at cycles[1] and cycles2
5420
							   then F=F0+(F1-F0)*a1 +(F2-F1)*a2
5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456
						 */

						double [] XY0={patternGrid[v+uvInc[cycle[0]][1]][u+uvInc[cycle[0]][0]][0][0],patternGrid[v+uvInc[cycle[0]][1]][u+uvInc[cycle[0]][0]][0][1]};
						double [] XY1={patternGrid[v+uvInc[cycle[1]][1]][u+uvInc[cycle[1]][0]][0][0],patternGrid[v+uvInc[cycle[1]][1]][u+uvInc[cycle[1]][0]][0][1]};
						double [] XY2={patternGrid[v+uvInc[cycle[2]][1]][u+uvInc[cycle[2]][0]][0][0],patternGrid[v+uvInc[cycle[2]][1]][u+uvInc[cycle[2]][0]][0][1]};

						double [] V= {pX-XY0[0],pY-XY0[1]};
						double [][] M={
								{XY1[0]-XY0[0],XY2[0]-XY1[0]},
								{XY1[1]-XY0[1],XY2[1]-XY1[1]}};
						double det=M[0][0]*M[1][1]-M[1][0]*M[0][1];
						double [][] MInverse={
								{ M[1][1]/det,-M[0][1]/det},
								{-M[1][0]/det, M[0][0]/det}};
						double [] a12={
								MInverse[0][0]*V[0]+MInverse[0][1]*V[1],
								MInverse[1][0]*V[0]+MInverse[1][1]*V[1]};
						int pCorrIndex=idY*sWidth+idX;
						// some points may be accumulated multiple times - thisPCorr[3] will take care of this
						if (this.debugLevel>3) {
							System.out.println("XY0="+IJ.d2s(XY0[0],3)+":"+IJ.d2s(XY0[1],3));
							System.out.println("XY1="+IJ.d2s(XY1[0],3)+":"+IJ.d2s(XY1[1],3));
							System.out.println("XY2="+IJ.d2s(XY2[0],3)+":"+IJ.d2s(XY2[1],3));
							System.out.println("M00="+IJ.d2s(M[0][0],3)+" M01="+IJ.d2s(M[0][1],3));
							System.out.println("M10="+IJ.d2s(M[1][0],3)+" M11="+IJ.d2s(M[1][1],3));
							System.out.println("MInverse00="+IJ.d2s(MInverse[0][0],5)+" MInverse01="+IJ.d2s(MInverse[0][1],5));
							System.out.println("MInverse10="+IJ.d2s(MInverse[1][0],5)+" MInverse11="+IJ.d2s(MInverse[1][1],5));
							System.out.println("a12="+IJ.d2s(a12[0],3)+":"+IJ.d2s(a12[1],3));
							System.out.println("gridIntensity[v+uvInc[cycle[0]][1]][u+uvInc[cycle[0]][0]]="+
									IJ.d2s(gridIntensity[v+uvInc[cycle[0]][1]][u+uvInc[cycle[0]][0]],3));
							System.out.println("gridIntensity[v+uvInc[cycle[1]][1]][u+uvInc[cycle[1]][0]]="+
									IJ.d2s(gridIntensity[v+uvInc[cycle[1]][1]][u+uvInc[cycle[1]][0]],3));
							System.out.println("gridIntensity[v+uvInc[cycle[2]][1]][u+uvInc[cycle[2]][0]]="+
									IJ.d2s(gridIntensity[v+uvInc[cycle[2]][1]][u+uvInc[cycle[2]][0]],3));
						}
						double val=
Andrey Filippov's avatar
Andrey Filippov committed
5457 5458 5459
								gridIntensity[v+uvInc[cycle[0]][1]][u+uvInc[cycle[0]][0]]+
								(gridIntensity[v+uvInc[cycle[1]][1]][u+uvInc[cycle[1]][0]]-gridIntensity[v+uvInc[cycle[0]][1]][u+uvInc[cycle[0]][0]])*a12[0]+
								(gridIntensity[v+uvInc[cycle[2]][1]][u+uvInc[cycle[2]][0]]-gridIntensity[v+uvInc[cycle[1]][1]][u+uvInc[cycle[1]][0]])*a12[1];
5460 5461
						if (this.debugLevel>3) {
							System.out.println("val="+IJ.d2s(val,3));
Andrey Filippov's avatar
Andrey Filippov committed
5462
						}
5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557
						fffg[pCorrIndex]+=val;// error in /data/focus/grid3d/center/1317924548_967543-00.tiff OOB: 5019002
						fffgNum[pCorrIndex]+=1;
					}
				} // idX
				// use same order in calculations, make sure no gaps
			} // idY
		} // finished image
		for (int i=0;i<fffg.length;i++) if (fffgNum[i]>0){
			fffg[i]/=fffgNum[i];
		}
		return fffg;
	}
	/* ======================================================================== */

	/**
	 *  Extrapolates flat-field correction
	 * @param data [nPixels] data to extrapolate
	 * @param sWidth data width
	 * @param preShrink shrink the non-zero data by this number of pixels before extrapolating
	 * @param expand expand the (pre-shrank) data by up to this number of pixels
	 * @param sigma when fitting plane through new point use Gaussian weight function for the neighbors
	 *  (normalized to non-decimated points)
	 * @param ksigma Process pixels in a square with the side 2*sigma*ksigma
	 */
	// TODO: Use threads
	public boolean extrapolatePatternFlatFieldCorrection(
			final double [] data, //fieldXY,
			final int sWidth,
			final int preShrink,
			final int expand,
			final double sigma,
			final double ksigma,
			int    threadsMax,     //   100; // testing multi-threading, limit maximal number of threads
			boolean updateStatus){
		int dbgThreshold=1;
		final int length=data.length;
		final int sHeight=length/sWidth;
		// create mask
		final boolean [] fMask=new boolean[data.length];
		for (int i=0;i<length;i++) fMask[i]= data[i]>0.0;

		final int len= (int) Math.ceil(sigma*ksigma);

		final double [] gaussian=new double[len+1];
		double k=0.5/sigma/sigma;
		for (int i=0;i<=len;i++) gaussian[i]=Math.exp(-i*i*k);
		int [][] dirs={{-1,0},{1,0},{0,-1},{0,1}}; // order matters
		final List <Integer> extList=new ArrayList<Integer>(1000);
		Integer Index, Index2;
		extList.clear();
		// create initial wave
		if (this.debugLevel>2) System.out.println("extrapolatePatternFlatFieldCorrection() sWidth="+sWidth+" sHeight="+sHeight);

		for (int iy=0;iy<sHeight;iy++) for (int ix=0;ix<sWidth;ix++) {
			Index=iy*sWidth+ix;
			if (fMask[Index]) {
				int numNew=0;
				for (int dir=0;dir<dirs.length;dir++){
					int ix1=ix+dirs[dir][0];
					int iy1=iy+dirs[dir][1];
					if ((ix1>=0) && (iy1>=0) && (ix1<sWidth) && (iy1<sHeight)) {
						if (!fMask[iy1*sWidth+ix1]) numNew++;
					}
					if (numNew>0) extList.add(Index); // neighbor will have non-singular matrix
				}
			}
		}
		// now shrink
		// unmask current wave
		for (int i=extList.size()-1; i>=0;i--) fMask[extList.get(i)]=false;
		if (extList.size()==0) return false; // no points
		for (int nShrink=0;nShrink<preShrink;nShrink++){
			int size=extList.size();
			if (size==0) return false; // no points
			// wave step, unmasking
			for (int i=0; i<size;i++) {
				Index=extList.get(0);
				extList.remove(0);
				int iy=Index/sWidth;
				int ix=Index%sWidth;
				for (int dir=0;dir<dirs.length;dir++){
					int ix1=ix+dirs[dir][0];
					int iy1=iy+dirs[dir][1];
					if ((ix1>=0) && (iy1>=0) && (ix1<sWidth) && (iy1<sHeight)){
						Index=iy1*sWidth+ix1;
						if (fMask[Index]){
							extList.add(Index);
							fMask[Index]=false; // restore later?
						}
					}
				}
			}
		}
		// restore mask on the front
		for (int i=extList.size()-1; i>=0;i--) fMask[extList.get(i)]=true;
Andrey Filippov's avatar
Andrey Filippov committed
5558

5559 5560
		if (this.debugLevel>dbgThreshold+1){
			for (int i=0;i<length;i++) if (!fMask[i]) data[i]=0.0;
5561

5562 5563
			this.SDFA_INSTANCE.showArrays(data, sWidth,sHeight, "shrank");
		}
Andrey Filippov's avatar
Andrey Filippov committed
5564

5565

5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613
		// repeat with the wave until there is place to move, but not more than "expand" steps

		final Thread[] threads = newThreadArray(threadsMax);
		final AtomicInteger pixInWaveNum = new AtomicInteger();


		int [] dirs2=new int [2];
		for (int n=0; (n<expand) && (extList.size()>0); n++ ){
			if (updateStatus) IJ.showStatus("Expanding, step="+(n+1)+" (of "+expand+"), extList.size()="+extList.size());
			if (this.debugLevel>2) System.out.println("Expanding, step="+n+", extList.size()="+extList.size());
			// move wave front 1 pixel hor/vert
			for (int i=extList.size();i>0;i--){ // repeat current size times
				Index=extList.get(0);
				extList.remove(0);
				int iy=Index/sWidth;
				int ix=Index%sWidth;
				for (int dir=0;dir<dirs.length;dir++){
					int ix1=ix+dirs[dir][0];
					int iy1=iy+dirs[dir][1];
					if ((ix1>=0) && (iy1>=0) && (ix1<sWidth) && (iy1<sHeight)){
						Index=iy1*sWidth+ix1;
						if (!fMask[Index]){
							// verify it has neighbors in the perpendicular direction to dir
							dirs2[0]=(dir+2) & 3;
							dirs2[1]=dirs2[0] ^ 1;
							for (int dir2=0;dir2<dirs2.length;dir2++){
								int ix2=ix+dirs[dirs2[dir2]][0]; // from the old, not the new point!
								int iy2=iy+dirs[dirs2[dir2]][1];
								if ((ix2>=0) && (iy2>=0) && (ix2<sWidth) && (iy2<sHeight)){
									Index2=iy2*sWidth+ix2;
									if (fMask[Index2]){ // has orthogonal neighbor, OK to add
										extList.add(Index);
										fMask[Index]=true; // remove later
										break;
									}
								}
							}
						}
					}
				}
			}
			// now un-mask the pixels in new list new
			for (int i =0;i<extList.size();i++){
				Index=extList.get(i);
				fMask[Index]=false; // now mask is only set for known pixels
			}
			// Calculate values (extrapolate) for the pixels in the list
			/*
Andrey Filippov's avatar
Andrey Filippov committed
5614 5615 5616
	Err = sum (W(x,y)*(f(x,y)-F0-Ax*(x-X0)-Ay*(y-Y0))^2)=
	sum (Wxy*(Fxy^2+F0^2+Ax^2*(x-X0)^2+Ay^2*(y-Y0)^2
	-2*Fxy*F0 -2*Fxy*Ax*(x-X0) - 2*Fxy*Ay*(y-Y0)
5617
	+2*F0*Ax*(x-X0) + 2*F0*Ay*(y-Y0)
Andrey Filippov's avatar
Andrey Filippov committed
5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650
	+2*Ax*(x-X0)*Ay*(y-Y0))
	(1)0=dErr/dF0= 2*sum (Wxy*(F0-Fxy+Ax*(x-X0)+Ay(y-Y0)))
	(2)0=dErr/dAx= 2*sum (Wxy*(Ax*(x-X0)^2-Fxy*(x-X0) +F0*(x-X0)+Ay*(x-x0)*(y-Y0)))
	(3)0=dErr/dAy= 2*sum (Wxy*(Ay*(y-y0)^2-Fxy*(y-Y0) +F0*(y-Y0)+Ax*(x-x0)*(y-Y0)))

	S0 = sum(Wxy)
	SF=  sum(Wxy*Fxy)
	SX=  sum(Wxy*(x-X0)
	SY=  sum(Wxy*(y-Y0)
	SFX= sum(Wxy*Fxy*(x-X0)
	SFY= sum(Wxy*Fxy*(y-Y0)
	SX2= sum(Wxy*(x-X0)^2
	SY2= sum(Wxy*(y-Y0)^2
	SXY= sum(Wxy*(x-X0)*(y-Y0)

	(1) F0*S0 - SF + Ax*SX +Ay*Sy = 0
	(2) Ax*SX2-SFX+F0*SX+Ay*SXY = 0
	(3) Ay*Sy2 -SFY + F0*SY +Ax*SXY = 0

	(1) F0*S0  + Ax*SX +Ay*SY = SF
	(2) Ax*SX2+F0*SX+Ay*SXY = SFX
	(3) Ay*Sy2  + F0*SY +Ax*SXY = SFY


	   | F0 |
	V= | Ax |
	   | Ay |

	     | SF  |
	B =  | SFX |
	     | SFY |

	     | S0  SX   SY  |
5651
	M =  | SX  SX2  SXY |
Andrey Filippov's avatar
Andrey Filippov committed
5652 5653 5654
	     | SY  SXY  SY2 |

	M * V = B
5655 5656 5657 5658 5659
			 */
			pixInWaveNum.set(0);
			for (int ithread = 0; ithread < threads.length; ithread++) {
				threads[ithread] = new Thread() {
					@Override
5660
					public void run() {
Andrey Filippov's avatar
Andrey Filippov committed
5661

5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695
						//			   for (int i =0;i<extList.size();i++){

						for (int i=pixInWaveNum.getAndIncrement(); i<extList.size();i=pixInWaveNum.getAndIncrement()){

							Integer Indx=extList.get(i);
							int iy=Indx/sWidth;
							int ix=Indx%sWidth;
							double  S0= 0.0;
							double  SF= 0.0;
							double  SX= 0.0;
							double  SY= 0.0;
							double  SFX=0.0;
							double  SFY=0.0;
							double  SX2=0.0;
							double  SY2=0.0;
							double  SXY=0.0;
							int iYmin=iy-len; if (iYmin<0) iYmin=0;
							int iYmax=iy+len; if (iYmax>=sHeight) iYmax=sHeight-1;
							int iXmin=ix-len; if (iXmin<0) iXmin=0;
							int iXmax=ix+len; if (iXmax>=sWidth) iXmax=sWidth-1;
							for (int iy1=iYmin;iy1<=iYmax;iy1++) for (int ix1=iXmin;ix1<=iXmax;ix1++) {
								int ind=ix1+iy1*sWidth;
								if (fMask[ind]){
									double w=gaussian[(iy1>=iy)?(iy1-iy):(iy-iy1)]*gaussian[(ix1>=ix)?(ix1-ix):(ix-ix1)];
									S0+= w;
									SF+= w*data[ind];
									SX+= w*(ix1-ix);
									SY+= w*(iy1-iy);
									SFX+=w*data[ind]*(ix1-ix);
									SFY+=w*data[ind]*(iy1-iy);
									SX2+=w*(ix1-ix)*(ix1-ix);
									SY2+=w*(iy1-iy)*(iy1-iy);
									SXY+=w*(ix1-ix)*(iy1-iy);
								}
Andrey Filippov's avatar
Andrey Filippov committed
5696

5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711
							}
							double [][] aB={{SF},{SFX},{SFY}};
							double [][] aM={
									{S0,SX, SY},
									{SX,SX2,SXY},
									{SY,SXY,SY2}
							};
							Matrix B=new Matrix(aB);
							Matrix M=new Matrix(aM);
							if (!(new LUDecomposition(M)).isNonsingular() && (S0!=0.0)){
								data[Indx]=SF/S0;
							} else {
								Matrix V=M.solve(B);  // sometimes singular
								data[Indx]=V.get(0,0);
							}
5712

5713 5714 5715 5716 5717
						}
					}
				};
			}
			startAndJoin(threads);
Andrey Filippov's avatar
Andrey Filippov committed
5718 5719


5720 5721 5722 5723 5724 5725 5726 5727
			// set mask again for the new calculated layer of pixels
			for (int i =0;i<extList.size();i++){
				Index=extList.get(i);
				fMask[Index]=true;
			}
			IJ.showProgress(n+1,expand);
		}
		IJ.showProgress(1.0);
Andrey Filippov's avatar
Andrey Filippov committed
5728

5729 5730
		return true;
	}
Andrey Filippov's avatar
Andrey Filippov committed
5731

5732 5733 5734



5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828
	/* ======================================================================== */
	private double [][] calcNeibLocsWeights (
			DistortionParameters distortionParameters,
			boolean useNeib){
		double [][] locsNeib={{0.0,0.0,1.0}};
		if (!useNeib) return locsNeib;
		locsNeib= new double [9][3];
		double [][]dirs={{ 0.0, 0.0},
				{ 1.0, 0.0},
				{ 0.0, 1.0},
				{-1.0, 0.0},
				{ 0.0,-1.0},
				{ 1.0, 1.0},
				{ 1.0,-1.0},
				{-1.0, 1.0},
				{-1.0,-1.0}};
		int i;
		locsNeib[0][2]=1.0-distortionParameters.averageOrthoWeight-distortionParameters.averageOrthoWeight;
		for (i=0;i<4;i++) {
			locsNeib[i+1][2]=0.25*distortionParameters.averageOrthoWeight;
			locsNeib[i+5][2]=0.25*distortionParameters.averageDiagWeight;
		}
		double k=1.0;
		for (i=0;i<9;i++) {
			if (i>0) k=distortionParameters.averageOrthoDist;
			if (i>4) k=distortionParameters.averageDiagDist;
			locsNeib[i][0]=k*dirs[i][0];
			locsNeib[i][1]=k*dirs[i][1];
		}
		return locsNeib;
	}
	/* ======================================================================== */
	public void zeroNaNContrast(){
		for (double [][][]  row:this.PATTERN_GRID){
			for (double [][] node:row){
				if ((node!=null) && (node.length>0) && (node[0]!=null) && (node[0].length>2)){
					if (Double.isNaN(node[0][2])) node[0][2]=0.0;
				}
			}
		}
	}


	public double[][][][] recalculateWaveVectors (
			//			   double[][][][] patternGrid,
			final boolean updateStatus,
			final int debug_level){// debug level used inside loops
		//		   double[][][][] patternGrid=this.PATTERN_GRID;
		int i;
		int [] iuv=new int [2];
		final int [][] directionsUV8= {{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,1},{-1,-1},{1,-1}}; // first 8 should be the same as in directionsUV
		final int [] directionsBits8= {1,4,1,4,2,8,2,8}; // should match directionsUV8
		int neibBits;
		int dir;
		int [] iUV=  new int [2];
		double [][][] neibors=new double [8][][]; // uv and xy vectors to 8 neibors (some may be null
		double [][] thisCell;
		double [][] otherCell;
		int was_debug_level=debugLevel;
		debugLevel=debug_level;
		if (debugLevel>1) System.out.println("Recalculating wave vectors from coordinates...");

		for (iuv[1]=0;iuv[1]<this.PATTERN_GRID.length;iuv[1]++) for (iuv[0]=0;iuv[0]<this.PATTERN_GRID[0].length;iuv[0]++) if (isCellValid(this.PATTERN_GRID,iuv)){
			if (debugLevel>2) System.out.println("<---= iuv= "+iuv[0]+",  "+iuv[1]);
			thisCell=this.PATTERN_GRID[iuv[1]][iuv[0]];
			neibBits=0;
			for (dir=0;dir<directionsUV8.length;dir++) {
				neibors[dir]=null;
				iUV[0]=iuv[0]+directionsUV8[dir][0];
				iUV[1]=iuv[1]+directionsUV8[dir][1];
				if ((iUV[0]<0) || (iUV[1]<0) ||
						(iUV[0]>=this.PATTERN_GRID[0].length) || (iUV[1]>=this.PATTERN_GRID.length)) continue; // don't fit into UV grid
				if (isCellValid(this.PATTERN_GRID,iUV)) {
					neibors[dir]= new double [2][2];
					otherCell=this.PATTERN_GRID[iUV[1]][iUV[0]];
					neibors[dir][0][0]=0.5*directionsUV8[dir][0];  // u
					neibors[dir][0][1]=0.5*directionsUV8[dir][1];  // v
					neibors[dir][1][0]=otherCell[0][0]-thisCell[0][0];  // x
					neibors[dir][1][1]=otherCell[0][1]-thisCell[0][1];  // y
					neibBits |= directionsBits8[dir];
				}
			}
			i=Integer.bitCount(neibBits);
			if (debugLevel>2) System.out.println("neibBits="+neibBits+", number of bits= "+i);
			if (i>1) {
				double[][] wv= waveVectorsFromNeib(neibors);
				setPatternGridCell(
						this.PATTERN_GRID,
						iuv,
						null, // XY already set
						wv[0],
						wv[1]);

				if (debugLevel>2) System.out.println("==+> number of bits:"+i+
Andrey Filippov's avatar
Andrey Filippov committed
5829 5830
						" vw00="+IJ.d2s(wv[0][0],5)+" vw01="+IJ.d2s(wv[0][1],5)+
						" vw10="+IJ.d2s(wv[1][0],5)+" vw11="+IJ.d2s(wv[1][1],5));
5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843
				//		    	wv= WaveVectorsFromNeib(neibors);
				//
				//	   vectors: [num_vector][0][0] - U
				//          [num_vector][0][1] - V
				//          [num_vector][1][0] - X
				//          [num_vector][1][1] - Y
				//          [num_vector] == null - skip
				//
			}
		}
		debugLevel=was_debug_level;
		return this.PATTERN_GRID;
	}
5844

5845 5846 5847 5848 5849 5850 5851 5852 5853 5854
	/* ======================================================================== */
	private double [][] waveVectorsFromNeib(double [][][] vectors){
		/*
		 * 		   vectors: [num_vector][0][0] - U
		 *                  [num_vector][0][1] - V
		 *                  [num_vector][1][0] - X
		 *                  [num_vector][1][1] - Y
		 *                  [num_vector] == null - skip
		 *    minimizing sum of squared errors.
		 *
Andrey Filippov's avatar
Andrey Filippov committed
5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878
      Ui=Xi*Wv00+Yi*Wv01
      Vi=Xi*Wv10+Yi*Wv11

           sum(Xi^2) *Wv00 +sum(Xi*Yi)*Wv01- sum(Xi*Ui) =0
           sum(Xi*Yi)*Wv00 +sum(Yi^2) *Wv01- sum(Yi*Ui) =0


           sum(Xi^2) *Wv10 +sum(Xi*Yi)*Wv11- sum(Xi*Vi) =0
           sum(Xi*Yi)*Wv10 +sum(Yi^2) *Wv11- sum(Yi*Vi) =0

     S= |  sum(Xi^2)   sum(Xi*Yi) |
        |  sum(Xi*Yi)  sum(Yi^2)  |

     SU= | sum(Xi*Ui) |
         | sum(Yi*Ui) |

     SV= | sum(Xi*Vi) |
         | sum(Yi*Vi) |

     Wv0=| Wv00 |
         | Wv01 |

     Wv1=| Wv10 |
         | Wv11 |
5879

Andrey Filippov's avatar
Andrey Filippov committed
5880 5881 5882
     S * Wv0 = SU
     S * Wv1 = SV

5883
     Wv0 = inv(S) * SU
Andrey Filippov's avatar
Andrey Filippov committed
5884
     Wv1 = inv(S) * SV
5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938
		 *
		 */
		int i;
		double [][] S={{0.0,0.0},{0.0,0.0}};
		double []   SU={0.0,0.0};
		double []   SV={0.0,0.0};
		double [][] WV=new double [2][];
		for (i=0;i<vectors.length;i++) if (vectors[i]!=null) {
			//			   if (debugLevel>1) System.out.println("waveVectorsFromNeib: i="+i +": "+
			//					   vectors[i][0][0]+" "+vectors[i][0][1]+" "+vectors[i][1][0]+" "+vectors[i][1][1]+" ");
			S[0][0]+= vectors[i][1][0]*vectors[i][1][0]; // sum(Xi^2)
			S[0][1]+= vectors[i][1][0]*vectors[i][1][1]; // sum(Xi*Yi)
			S[1][1]+= vectors[i][1][1]*vectors[i][1][1]; // sum(Yi^2)
			SU[0]+=   vectors[i][1][0]*vectors[i][0][0]; // sum(Xi*Ui)
			SU[1]+=   vectors[i][1][1]*vectors[i][0][0]; // sum(Yi*Ui)
			SV[0]+=   vectors[i][1][0]*vectors[i][0][1]; // sum(Xi*Vi)
			SV[1]+=   vectors[i][1][1]*vectors[i][0][1]; // sum(Yi*Vi)
		}
		S[1][0]=S[0][1];
		//		   if (debugLevel>1) System.out.println("waveVectorsFromNeib: S00="+S[0][0]+" S01="+S[0][1]);
		//		   if (debugLevel>1) System.out.println("waveVectorsFromNeib: S10="+S[1][0]+" S11="+S[1][1]);
		//		   if (debugLevel>1) System.out.println("waveVectorsFromNeib: SU0="+SU[0]+  " SU1="+SU[1]);
		//		   if (debugLevel>1) System.out.println("waveVectorsFromNeib: SV0="+SV[0]+  " SV1="+SV[1]);

		S=matrix2x2_invert(S);
		//		   if (debugLevel>1) System.out.println("waveVectorsFromNeib: S00="+S[0][0]+" S01="+S[0][1]);
		//		   if (debugLevel>1) System.out.println("waveVectorsFromNeib: S10="+S[1][0]+" S11="+S[1][1]);

		WV[0]=matrix2x2_mul(S,SU);
		WV[1]=matrix2x2_mul(S,SV);
		return WV;
	}
	private void putInWaveList (
			List <Integer> list,
			int [] uv,
			int dir) {
		int l=(Integer.SIZE-2)/2;
		int mask =(1<<l)-1;

		list.add(new Integer((dir & 3) | ((uv[0] & mask) << 2) | ((uv[1] & mask) << (2+l))));
	}
	private int [] getWaveList (
			List <Integer> list,
			int index) {
		int l=(Integer.SIZE-2)/2;
		int mask =(1<<l)-1;

		int d=list.get(index);
		int [] result=new int[3];
		result[2]=(d & 3);
		result[0]=(d >>2 ) & mask;
		result[1]=(d >> (2+l)) & mask;
		return result;
	}
5939 5940


5941
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
5942
	// set XY coordinates and (optionally) wave vectors of the pattern grid cell
5943
	/*
Andrey Filippov's avatar
Andrey Filippov committed
5944 5945 5946
		   cell==null - new cell, not yet defined
		   cell.length==1 - invalid cell
		   cell.length>1 - initialized:
5947
		   cell[0]==null - undefined
Andrey Filippov's avatar
Andrey Filippov committed
5948
		   cell[0]!=null - defined
5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983
	 */
	private void setPatternGridCell(
			double [][][][] grid,
			int [] uv,
			double [] xy, // may be a 3-element, with contrast
			double [] wv1,
			double [] wv2){
		int i;
		initPatternGridCell(grid,uv);
		if (xy!=null) {
			//				  double [] grid_xy= new double[2];
			//				  for (i=0;i<2;i++) grid_xy[i]=xy[i];
			grid[uv[1]][uv[0]][0]= xy.clone(); // grid_xy;
		}
		if (wv1!=null) {
			double [] grid_wv1= new double[2];
			for (i=0;i<2;i++) grid_wv1[i]=wv1[i];
			grid[uv[1]][uv[0]][1]= grid_wv1;
		}
		if (wv2!=null) {
			double [] grid_wv2= new double[2];
			for (i=0;i<2;i++) grid_wv2[i]=wv2[i];
			grid[uv[1]][uv[0]][2]= grid_wv2;
		}
	}
	private void initPatternGridCell(
			double [][][][] grid,
			int [] uv){
		int i;
		if (grid[uv[1]][uv[0]]==null) {
			double [][] grid_cell= new double [3][];
			for (i=0;i<3;i++) grid_cell[i]=null;
			grid[uv[1]][uv[0]]=grid_cell;
		}
	}
Andrey Filippov's avatar
Andrey Filippov committed
5984

5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997
	// mark the grid cell as invalid
	private void invalidatePatternGridCell(
			double [][][][] grid,
			int [] uv){
		double [][] cell = new double [1][];
		cell[0]=null;
		grid[uv[1]][uv[0]]=cell;
	}
	private void clearPatternGridCell(
			double [][][][] grid,
			int [] uv){
		grid[uv[1]][uv[0]]=null;
	}
5998

5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112
	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]][0]=newXYC;
		}
		//			   grid[uv[1]][uv[0]]=null;

	}

	private boolean isCellDeleted(
			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) && (grid[uv[1]][uv[0]][0]!=null) && (grid[uv[1]][uv[0]][0].length>3));
	}
	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);
		// 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,
			int [] uv){
		if ((uv[1]>=0) && (uv[0]>=0) && (uv[1]<grid.length) && (uv[0]<grid[uv[1]].length)) {
			double [][] cell = grid[uv[1]][uv[0]];
			return ((cell!=null) && (cell.length>1));
		}
		return false;
	}
	private boolean isCellDefined(
			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) && (grid[uv[1]][uv[0]][0]!=null));
	}
	private boolean isCellDefined(
			double [][][][] grid,
			int u,
			int v){
		return ((v>=0) && (u>=0) && (v<grid.length) && (u<grid[v].length) &&
				(grid[v][u]!=null) && (grid[v][u][0]!=null));
	}
	private boolean isCellDefined(
			int u,
			int v){
		return isCellDefined(this.PATTERN_GRID,u,v);
	}
	private boolean isCellDefined(
			int [] uv){
		return isCellDefined(this.PATTERN_GRID,uv);
	}

	// with contrast
	private double getCellContrast(double [][][][] grid,
			int [] uv){
		if  ((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]][0]!=null) && (grid[uv[1]][uv[0]][0].length>2)) {
			return grid[uv[1]][uv[0]][0][2];
		} else {
			return Double.NaN;
		}
	}
	private double getCellContrast(double [][][][] grid,
			int u,
			int v){
		if  ((v>=0) && (u>=0) && (v<grid.length) && (u<grid[v].length) &&
				(grid[v][u]!=null) && (grid[v][u][0]!=null) && (grid[v][u][0].length>2)) {
			return grid[v][u][0][2];
		} else {
			return Double.NaN;
		}
	}
	public double getCellContrast(int [] uv){
		return getCellContrast(this.PATTERN_GRID,uv);
	}
	public double getCellContrast(int u, int v){
		return getCellContrast(this.PATTERN_GRID,u,v);
	}
	private boolean isCellDefinedC(
			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) && (grid[uv[1]][uv[0]][0]!=null) && (grid[uv[1]][uv[0]][0].length>2) && !Double.isNaN(grid[uv[1]][uv[0]][0][2]));
	}
	private boolean isCellDefinedC(
			double [][][][] grid,
			int u,
			int v){
		return ((v>=0) && (u>=0) && (v<grid.length) && (u<grid[v].length) &&
				(grid[v][u]!=null) && (grid[v][u][0]!=null) && (grid[v][u][0].length>2) && !Double.isNaN(grid[v][u][0][2]));
	}
	public boolean isCellDefinedC(
			int u,
			int v){
		return isCellDefinedC(this.PATTERN_GRID,u,v);
	}
	public boolean isCellDefinedC(
			int [] uv){
		return isCellDefinedC(this.PATTERN_GRID,uv);
	}



	/*
Andrey Filippov's avatar
Andrey Filippov committed
6113 6114 6115 6116 6117 6118 6119 6120
		   private double [] cellXY(int u, int v){
			   if (!isCellDefined(u,v)) return null;
			   return this.PATTERN_GRID[v][u][0];
		   }
		   private double [] cellXY(int [] uv){
			   if (!isCellDefined(uv)) return null;
			   return this.PATTERN_GRID[uv[1]][uv[0]][0];
		   }
6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141
	 */
	private double [] cellXYC(int u, int v){
		if (!isCellDefined(u,v)) return null;
		double [] xyc={
				this.PATTERN_GRID[v][u][0][0],
				this.PATTERN_GRID[v][u][0][1],
				(this.PATTERN_GRID[v][u][0].length>2)?this.PATTERN_GRID[v][u][0][2]:
					((this.gridContrastBrightness==null)?1.0:this.gridContrastBrightness[0][v][u])
		};
		return xyc; // this.PATTERN_GRID[uv[1]][uv[0]][0];
	}
	private double [] cellXYC(int [] uv){
		if (!isCellDefined(uv)) return null;
		double [] xyc={
				this.PATTERN_GRID[uv[1]][uv[0]][0][0],
				this.PATTERN_GRID[uv[1]][uv[0]][0][1],
				(this.PATTERN_GRID[uv[1]][uv[0]][0].length>2)?this.PATTERN_GRID[uv[1]][uv[0]][0][2]:
					((this.gridContrastBrightness==null)?1.0:this.gridContrastBrightness[0][uv[1]][uv[0]])
		};
		return xyc; // this.PATTERN_GRID[uv[1]][uv[0]][0];
	}
Andrey Filippov's avatar
Andrey Filippov committed
6142

6143 6144


6145 6146 6147
	public int numDefinedCells()  {
		return numDefinedCells(this.PATTERN_GRID);
	}
Andrey Filippov's avatar
Andrey Filippov committed
6148

6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168
	public int numDefinedCells(double [][][][] grid)  { // calulate/print number of defined nodes in a grid
		int [] iUV=new int [2];
		int numDefinedCells=0;
		for (iUV[1]=0;iUV[1]<grid.length;iUV[1]++) for (iUV[0]=0;iUV[0]<grid[0].length;iUV[0]++)
			if (this.isCellDefined(grid,iUV)) numDefinedCells++;
		return numDefinedCells;
	}
	public int gridUVWidth(){return ((this.PATTERN_GRID==null) || (this.PATTERN_GRID.length==0l) ||(this.PATTERN_GRID[0]==null))?0:this.PATTERN_GRID[0].length;}
	public int gridUVHeight(){return ((this.PATTERN_GRID==null) || (this.PATTERN_GRID.length==0l) ||(this.PATTERN_GRID[0]==null))?0:this.PATTERN_GRID.length;}
	/* ======================================================================== */
	/**
	 * returns number of laser pointers matched (or negative error)
	 * if (this.flatFieldForGrid!=null) it should already be applied !!
	 */
	public int calculateDistortions(
			LwirReaderParameters lwirReaderParameters, // null is OK
			MatchSimulatedPattern.DistortionParameters distortionParameters, //
			MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			SimulationPattern.SimulParameters  simulParameters,
			boolean equalizeGreens,
6169
			ImagePlus imp, // image to process // has WOI_TOP and possibly - WOI_COMPENSATED
6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245
			LaserPointer laserPointer, // LaserPointer object or null
			boolean removeOutOfGridPointers, //
			double [][][] hintGrid, // predicted grid array (or null)
			double        hintGridTolerance, // allowed mismatch (fraction of period) or 0 - orientation only
			int threadsMax,
			boolean updateStatus,
			int global_debug_level, // DEBUG_LEVEL
			int debug_level, // debug level used inside loops
			boolean noMessageBoxes ){
		if (imp==null){
			IJ.showMessage("Error","There are no images open\nProcess canceled");
			return 0;
		}
		final int debugThreshold = 1;
		boolean is_lwir = ((lwirReaderParameters != null) && lwirReaderParameters.is_LWIR(imp));
		double    min_half_period= (is_lwir ? patternDetectParameters.minGridPeriodLwir : patternDetectParameters.minGridPeriod)/2;
		double    max_half_period=(is_lwir ? patternDetectParameters.maxGridPeriodLwir : patternDetectParameters.maxGridPeriod)/2;
		int       minimal_pattern_cluster = is_lwir ? distortionParameters.minimalPatternClusterLwir : distortionParameters.minimalPatternCluster;

		int fft_size = is_lwir ? distortionParameters.FFTSize_lwir : distortionParameters.FFTSize;

		long 	  startTime=System.nanoTime();
		Roi roi= imp.getRoi();
		Rectangle selection;
		if (roi==null){
			setWOI(0, 0, imp.getWidth(), imp.getHeight());
			selection=new Rectangle(0, 0, imp.getWidth(), imp.getHeight());
		} else {
			setWOI(roi.getBounds());
			selection=roi.getBounds();
		}
		this.debugLevel=global_debug_level;
		int patternCells=0;
		// save initial distortionParameters.correlationMinInitialContrast
		double savedCorrelationMinInitialContrast=distortionParameters.correlationMinInitialContrast;
		int reTries= 10; // bail out after these attempts
		boolean foundGoodCluster=false;

		int tryHor=0,tryVert=0;
		//with distortionParameters.searchOverlap==0.5 (default) step will be FFTSize original pixels, so half of the (2xFFTSize) square processed simultaneously
		if (distortionParameters.searchOverlap<0.1) distortionParameters.searchOverlap=0.1;
		int effectiveWidth=(int) (selection.width*0.5/distortionParameters.searchOverlap);
		int effectiveHeight=(int) (selection.height*0.5/distortionParameters.searchOverlap);

		for (int i = fft_size; i < effectiveWidth; i *= 2) tryHor++;
		for (int i = fft_size; i < effectiveHeight; i *= 2) tryVert++;

		int numTries=1<<(tryHor+tryVert);
		boolean [] triedIndices=new boolean[numTries+1]; // last set - all used
		for (int i=0;i<triedIndices.length;i++) triedIndices[i]=(i<3); // mark first 3 as if they are already used


		while (reTries-->0) {
			this.PATTERN_GRID=null;
			invalidateCalibration();

			patternCells=distortions( // calculates matchSimulatedPattern.DIST_ARRAY // invalidates calibration, flatFieldForGrid, resets this.PATTERN_GRID
					lwirReaderParameters, // null is OK
					triedIndices,
					distortionParameters, //
					patternDetectParameters,
					min_half_period,
					max_half_period,
					simulParameters,
					equalizeGreens,
					imp,
					threadsMax,
					updateStatus,
					debug_level,
					global_debug_level); // debug level
					if (global_debug_level>0) System.out.println("Pattern correlation done at "+ IJ.d2s(0.000000001*(System.nanoTime()-startTime),3)+
							" found "+patternCells+" cells, reTries left: "+reTries);
					if (patternCells>0) {
						foundGoodCluster=true;
						break; // new distortions() code - returns non-zero only if passed other tests
					}
Andrey Filippov's avatar
Andrey Filippov committed
6246

6247 6248 6249 6250 6251 6252
					boolean someLeft=false;
					int startScanIndex=0;
					for (startScanIndex=3;startScanIndex<triedIndices.length;startScanIndex++) if (!triedIndices[startScanIndex]){
						someLeft=true;
						break;
					}
6253

6254 6255 6256 6257 6258 6259 6260 6261 6262 6263
					if (someLeft) {
						if (global_debug_level>0){
							System.out.println("Initial pattern cluster is too small ("+patternCells+
									"), continuing scanning from index "+startScanIndex);
						}
					} else {
						if (global_debug_level>0) System.out.println("--- Tried all - nothing found --- at "+ IJ.d2s(0.000000001*(System.nanoTime()-startTime),3));
						break;
					}
		}
6264 6265 6266



6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340
		// restore initial distortionParameters.correlationMinInitialContrast
		distortionParameters.correlationMinInitialContrast=savedCorrelationMinInitialContrast;
		if (!foundGoodCluster){
			if (global_debug_level > (debugThreshold + 1)) System.out.println("calculateDistortions(): Pattern too small, initial cluster had "+patternCells+" cells");
			if (global_debug_level > (debugThreshold + 2)) IJ.showMessage("Error","Pattern too small: "+patternCells);
			return distortionParameters.errPatternNotFound;
		}
		if (!patternOK()) {
			if (global_debug_level > (debugThreshold + 1)) System.out.println("Pattern not found");
			if (global_debug_level > (debugThreshold+2))   IJ.showMessage("Error","Pattern not found");
			return distortionParameters.errPatternNotFound;
		} else {
			if (global_debug_level > (debugThreshold+1)) System.out.println("Initial pattern cluster has "+patternCells+" cells");
		}
		if (global_debug_level > (debugThreshold+1)) System.out.println("Wave vectors recalculated at "+ IJ.d2s(0.000000001*(System.nanoTime()-startTime),3));
		recalculateWaveVectors (
				updateStatus,
				debug_level);// debug level used inside loops
		ImagePlus imp_eq;
		if (distortionParameters.flatFieldCorrection && (this.flatFieldForGrid==null)) // if it is not null it is already supposed to be applied!
			imp_eq=equalizeGridIntensity(
					imp,
					this.PATTERN_GRID,
					distortionParameters, //
					equalizeGreens,
					global_debug_level,
					updateStatus,
					threadsMax);
		else imp_eq=imp;

		if (distortionParameters.refineCorrelations) {
			refineDistortionCorrelation (
					lwirReaderParameters, // LwirReaderParameters lwirReaderParameters, // null is OK
					distortionParameters, //
					patternDetectParameters,
					simulParameters,
					equalizeGreens,
					imp_eq,
					0.0, //final double maxCorr, // maximal allowed correction, in pixels (0.0) - any
					threadsMax,
					updateStatus,
					debug_level); // debug level

			recalculateWaveVectors (
					updateStatus,
					debug_level);// debug level used inside loops
			if (global_debug_level > (debugThreshold+1)) System.out.println("Second pass over at "+ IJ.d2s(0.000000001*(System.nanoTime()-startTime),3));
		}
		//hack gridSize
		if ((distortionParameters.gridSize & 1)!=0) {
			refineDistortionCorrelation (
					lwirReaderParameters, // LwirReaderParameters lwirReaderParameters, // null is OK
					distortionParameters, //
					patternDetectParameters,
					simulParameters,
					equalizeGreens,
					imp_eq,
					0.0, //final double maxCorr, // maximal allowed correction, in pixels (0.0) - any
					threadsMax,
					updateStatus,
					debug_level); // debug level

			recalculateWaveVectors (
					updateStatus,
					debug_level);// debug level used inside loops
			if (global_debug_level>0) System.out.println("Third pass over at "+ IJ.d2s(0.000000001*(System.nanoTime()-startTime),3));
			//hack gridSize
		}
		patternCells=numDefinedCells();
		if ((roi!=null) && (patternCells<minimal_pattern_cluster)){
			if (global_debug_level > (debugThreshold+0)) System.out.println("Detected pattern is too small: "+patternCells+
					", minimum is set to "+minimal_pattern_cluster);
			return distortionParameters.errTooFewCells; // -10
		}
6341

6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372
		double [] xy0={simulParameters.offsetX,simulParameters.offsetY} ; //debug
		createUV_INDEX(
				imp, // or null - just to determine WOI (when getWOI matches image size)
				xy0, // add to patterGrid xy, null OK
				threadsMax,
				updateStatus,
				global_debug_level, // DEBUG_LEVEL
				debug_level); // debug level used inside loops
		finalizeDistortionsBorder (
				distortionParameters, //
				updateStatus,
				debug_level);// debug level used inside loops
		//Wave vectors are used when calculating PSF
		recalculateWaveVectors (
				updateStatus,
				debug_level);// debug level used inside loops

		int numDifferentFFT=0;
		int maxLn2=0;
		for (int i=0;i<getCorrelationSizesUsed().length;i++) if (getCorrelationSizesUsed()[i]) {
			numDifferentFFT++;
			maxLn2=i;
		}
		if (numDifferentFFT>1){
			String sizesUsed="";
			for (int i=0;i<getCorrelationSizesUsed().length;i++) if (getCorrelationSizesUsed()[i]) sizesUsed+=" "+(1<<i);
			String msg="Different correlation FFT sizes used:"+sizesUsed+". You may consider increasing \"Correlation size\" setting to "+(1<<maxLn2)+" to reduce artifacts";
			if (global_debug_level > (debugThreshold+0)){
				System.out.println(msg);
				if (global_debug_level > (debugThreshold + 1)) IJ.showMessage(msg);
			}
6373

6374 6375 6376 6377 6378 6379 6380
		} else if (numDifferentFFT>0){
			String msg="Single correlation FFT size used: "+(1<<maxLn2);
			if (global_debug_level > (debugThreshold+0)) System.out.println(msg);
		}
		zeroNaNContrast(); // replace grid NaN with 0

		int numPointers=(laserPointer!=null)?laserPointer.laserUVMap.length:0;
6381
		double [][] pointersXY=(numPointers>0)?getPointersXYUV(imp, laserPointer):null;
6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407
		if (global_debug_level > (debugThreshold+1)){
			if (pointersXY!=null){
				System.out.println("calculateDistortions() numPointers="+numPointers+" pointersXY.length="+pointersXY.length);
				for (int ii=0;ii<pointersXY.length;ii++) {
					if (pointersXY[ii]!=null){
						System.out.println("calculateDistortions()  pointersXY["+ii+"][0]="+pointersXY[ii][0]);
						System.out.println("                        pointersXY["+ii+"][1]="+pointersXY[ii][1]);
					} else{
						System.out.println("calculateDistortions()  pointersXY["+ii+"]=NULL");
					}
				}
				System.out.println("                                     hintGrid="+((hintGrid==null)?"NULL":"not NULL"));
				System.out.println("                            hintGridTolerance="+hintGridTolerance);
			} else {
				System.out.println("pointersXY == null");
			}
		}
		return combineGridCalibration(
				laserPointer, // LaserPointer object or null
				pointersXY,
				removeOutOfGridPointers, //
				hintGrid, // predicted grid array (or null)
				hintGridTolerance, // alllowed mismatch (fraction of period) or 0 - orientation only
				global_debug_level, // DEBUG_LEVEL
				noMessageBoxes );
	}
6408

6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422
	//====== end of calculateDistortions() ==============================================
	/**
	 * Approximate function z(x,y) as a second degree polynomial
	 * f(x,y)=A*x^2+B*y^2+C*x*y+D*x+E*y+F
	 * data array consists of lines of either 2 or 3 vectors:
	 *  2-element vector x,y
	 *  variable length vector z (should be the same for all samples)
	 *  optional 1- element vector w (weight of the sample)
	 *
	 * returns array of vectors or null
	 * each vector (one per each z component) is either 6-element-  (A,B,C,D,E,F) if quadratic is possible and enabled
	 * or 3-element - (D,E,F) if linear is possible and quadratic is not possible or disbled
	 * returns null if not enough data even for the linear approximation
	 */
Andrey Filippov's avatar
Andrey Filippov committed
6423

6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465
	public double [][] approximatePSFQuadratic(
			double []       psf,     // PSF function, square array, nominally positive
			double cutoffEnergy,     // fraction of energy in the pixels to be used
			double cutoffLevel,      // minimal level as a fraction of maximal
			int         minArea,      // minimal selected area in pixels
			double      blurSigma,    // optionally blur the selection
			double      maskCutOff,
			int           debugLevel, // debug level
			String        title) {    // prefix used for debug images
		double [] mask=findClusterOnPSF(
				psf,
				cutoffEnergy,
				cutoffLevel,
				minArea,
				blurSigma,
				debugLevel,
				title);
		int numPix=0;
		for (int i=0;i<mask.length;i++)
			if (mask[i]<maskCutOff) mask[i]=0.0;
		for (int i=0;i<mask.length;i++)
			if (mask[i]>0.0) numPix++;
		double [][][]data = new double[numPix][3][];
		numPix=0;
		int size = (int) Math.sqrt(psf.length);
		int hsize=size/2;
		for (int i=0;i<mask.length;i++)  if (mask[i]>0.0) {
			data[numPix][0]=new double[2];
			data[numPix][0][0]=(i % size) - hsize;
			data[numPix][0][1]=(i / size) - hsize;
			data[numPix][1]=new double[1];
			data[numPix][1][0]=psf[i];
			data[numPix][2]=new double[1];
			data[numPix][2][0]=mask[i];
			numPix++;
		}
		return  new PolynomialApproximation(debugLevel).quadraticApproximation(
				data,
				false,  // use linear approximation (instead of quadratic)
				1.0E-10,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
				1.0E-20);  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
	}
Andrey Filippov's avatar
Andrey Filippov committed
6466

6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557
	//====================================================
	public double [] tangetRadialSizes(
			double ca, // cosine of the center to sample vector
			double sa, // sine of the center to sample vector
			double []       psf,     // PSF function, square array, nominally positive
			double cutoffEnergy,     // fraction of energy in the pixels to be used
			double cutoffLevel,      // minimal level as a fraction of maximal
			int         minArea,      // minimal selected area in pixels
			double      blurSigma,    // optionally blur the selection
			double      maskCutOff,
			int           debugLevel, // debug level
			String        title) {    // prefix used for debug images
		double [] mask=findClusterOnPSF(
				psf,
				cutoffEnergy,
				cutoffLevel,
				minArea,
				blurSigma,
				debugLevel,
				title);
		for (int i=0;i<mask.length;i++)
			if (mask[i]<maskCutOff) mask[i]=0.0;
		int size = (int) Math.sqrt(psf.length);
		int hsize=size/2;
		//			   int nn=0;
		double S0=0.0, SR=0.0, ST=0.0,SR2=0.0,ST2=0.0; //,SRT=0.0;
		for (int i=0;i<mask.length;i++)  if (mask[i]>0.0) {
			double x=(i % size) - hsize;
			double y=(i / size) - hsize;
			double rc= x*ca+ y*sa;
			double tc=-x*sa+ y*ca;
			double d=psf[i]*mask[i];
			S0+=d;
			SR+=d*rc;
			ST+=d*tc;
			SR2+=d*rc*rc;
			ST2+=d*tc*tc;
			//				   nn++;
		}
		if (S0==0.0) return null; // make sure it is OK
		double  [] result={ Math.sqrt(ST2*S0 - ST*ST)/S0, Math.sqrt(SR2*S0 - SR*SR)/S0};
		//			   System.out.println(" mask.length="+mask.length+" nn="+nn+" S0="+S0+" SR="+SR+" ST="+ST+" SR2="+SR2+" ST2="+ST2+
		//					   " result={"+result[0]+","+result[1]+"}");
		return result;
	}

	//====================================================
	public double [] x2y2xySizes(
			double []       psf,     // PSF function, square array, nominally positive
			double cutoffEnergy,     // fraction of energy in the pixels to be used
			double cutoffLevel,      // minimal level as a fraction of maximal
			int         minArea,      // minimal selected area in pixels
			double      blurSigma,    // optionally blur the selection
			double      maskCutOff,
			int           debugLevel, // debug level
			String        title) {    // prefix used for debug images
		double [] mask=findClusterOnPSF(
				psf,
				cutoffEnergy,
				cutoffLevel,
				minArea,
				blurSigma,
				debugLevel,
				title);
		for (int i=0;i<mask.length;i++)
			if (mask[i]<maskCutOff) mask[i]=0.0;
		int size = (int) Math.sqrt(psf.length);
		int hsize=size/2;
		//			   int nn=0;
		double S0=0.0, SX=0.0, SY=0.0,SX2=0.0,SY2=0.0,SXY=0.0;
		for (int i=0;i<mask.length;i++)  if (mask[i]>0.0) {
			double x=(i % size) - hsize;
			double y=(i / size) - hsize;
			double d=psf[i]*mask[i];
			S0+=d;
			SX+=d*x;
			SY+=d*y;
			SX2+=d*x*x;
			SY2+=d*y*y;
			SXY+=d*x*y;
			//				   nn++;
		}
		if (S0==0.0) return null; // make sure it is OK
		double  [] result={
				(SX2*S0 - SX*SX)/S0/S0,
				(SY2*S0 - SY*SY)/S0/S0,
				(SXY*S0 - SX*SY)/S0/S0}; // this may be negative
		//			   System.out.println(" mask.length="+mask.length+" nn="+nn+" S0="+S0+" SX="+SX+" SY="+SY+" SX2="+SXR2+" SY2="+SY2+" SXY="+SXY+
		//					   " result={"+result[0]+","+result[1]+","+result[2]+"}");
		return result;
	}
6558 6559


6560
	//====================================================
Andrey Filippov's avatar
Andrey Filippov committed
6561

6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602
	public double [] findClusterOnPSF(
			double []        psf,     // PSF function, square array, nominally positive
			double cutoffEnergy,     // fraction of energy in the pixels to be used
			double cutoffLevel,      // minimal level as a fraction of maximal
			int         minArea,      // minimal selected area in pixels
			double      blurSigma,    // optionally blur the selection
			int           debugLevel, // debug level
			String        title) {    // prefix used for debhug images
		//				int i,j;
		int ix,iy,ix1,iy1;
		List <Integer> pixelList=new ArrayList<Integer>(100);
		Integer Index=0, Index1,IndexMax;
		int size=(int) Math.sqrt(psf.length);
		//				int [][]clusterMap=new int[size][size];
		int [][] dirs={{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1}};
		int len=size*size;
		double [] clusterMap=new double[len];
		double full_energy=0.0;
		double maxValue=0;
		for (int i=0;i<len;i++) {
			clusterMap[i]=0.0;
			if (psf[i]>0.0) full_energy+=psf[i];
			if (maxValue<psf[i]) {
				maxValue=psf[i];
				Index=i;
			}
		}
		if (maxValue<=0.0){
			String msg="psf array does not contain any positive values";
			//	    		  IJ.showMessage("Error",msg);
			System.out.println("Error "+msg);
			throw new IllegalArgumentException (msg);
		}
		ix=Index % size;
		iy=Index / size;
		double theresholdLevel=maxValue*cutoffLevel;
		double theresholdEnergy=full_energy*cutoffEnergy;
		double cluster_energy=0.0;
		int clusterSize=0;

		boolean noNew=true;
Andrey Filippov's avatar
Andrey Filippov committed
6603 6604 6605
		if (debugLevel>1)		System.out.println("findClusterOnPSF(): full_energy="+full_energy+" theresholdEnergy="+theresholdEnergy+
				" maxValue="+maxValue+ " theresholdLevel="+theresholdLevel);
		if (debugLevel>1)		System.out.println("findClusterOnPSF(): ix="+ix+" iy="+iy);
6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622
		IndexMax=0;
		int listIndex;
		pixelList.clear();
		pixelList.add (Index);
		clusterSize++;
		clusterMap[Index]=1.0;
		cluster_energy+=psf[Index];
		noNew=true;
		while ((pixelList.size()>0) &&
				((clusterSize<minArea) || (cluster_energy<theresholdEnergy))) { // will break from the loop if  (psf[Index] <theresholdLevel)
			/* Find maximal new neighbor */
			maxValue=0.0;
			listIndex=0;
			while (listIndex<pixelList.size()) {
				Index=pixelList.get(listIndex);
				iy=Index/size;
				ix=Index%size;
Andrey Filippov's avatar
Andrey Filippov committed
6623
				noNew=true;
6624 6625 6626 6627 6628 6629 6630 6631 6632
				for (int j=0;j<8;j++) if (((iy > 0 ) || (dirs[j][1]>=0)) && ((iy < (size-1) ) || (dirs[j][1]<=0))){
					ix1=(ix+dirs[j][0]+size) % size;
					iy1= iy+dirs[j][1];
					Index1=iy1*size+ix1;
					if (clusterMap[Index1]==0.0) {
						noNew=false;
						if (psf[Index1]>maxValue) {
							maxValue= psf[Index1];
							IndexMax=Index1;
Andrey Filippov's avatar
Andrey Filippov committed
6633 6634 6635
						}
					}
				}
6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712
				if (noNew) pixelList.remove(listIndex);  //  remove current list element
				else       listIndex++;     // increase list index
			}
			if (maxValue==0.0) 	break; // no positive points left
			if ((clusterSize>=minArea) && (psf[IndexMax]<theresholdLevel)) break; // level is below thershold, minimal size condition met
			/* Add this new point to the list */
			pixelList.add (IndexMax);
			clusterSize++;
			clusterMap[IndexMax]=1.0;
			cluster_energy+=psf[IndexMax];
		} // end of while ((pixelList.size()>0) &&  ...)
		if (debugLevel>3)   System.out.println("findClusterOnPSF: cluster size is "+clusterSize);
		if (debugLevel>3) {
			SDFA_INSTANCE.showArrays(psf, size, size, title+"-psf");
		}
		if (debugLevel>2) {
			SDFA_INSTANCE.showArrays(clusterMap, size, size, title+"-clusterMap");
		}
		if (blurSigma>0.0){
			DoubleGaussianBlur gb=new DoubleGaussianBlur();
			gb.blurDouble(
					clusterMap,
					size,
					size,
					blurSigma,
					blurSigma,
					0.01);
			if (debugLevel>2) {
				SDFA_INSTANCE.showArrays(clusterMap, size, size, title+"-clusterMap-blured");
			}
		}
		return clusterMap;
	}


	/* ======================================================================== */
	/**
	 * Mask (assignes zero) to the flat-field array outside of the sample squares,]
	 * Clears grid nodes that do not have neighbors inside the sample squares.
	 * If (this.flatFieldForGrid==null) - creates mask of 1.0/0.0
	 * @param focusMeasurementParameters - parameters specifying probe points
	 */
	public void maskFocus(
			double x0,   // lens center on the sensor
			double y0,  // lens center on the sensor
			LensAdjustment.FocusMeasurementParameters focusMeasurementParameters){
		if (this.PATTERN_GRID==null) {
			String msg="PATTERN_GRID array does not exist, exiting";
			IJ.showMessage("Error",msg);
			throw new IllegalArgumentException (msg);
		}
		if (this.flatFieldForGrid==null) {
			String msg="Flat field for grid array does not exist, exiting";
			IJ.showMessage("Error",msg);
			throw new IllegalArgumentException (msg);
		}
		int [][] dirs= {{1,0},{0,1},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
		int width=getImageWidth();
		int height=getImageHeight();
		int halfSize=focusMeasurementParameters.sampleSize/2;
		// System.out.println("maskFocus(): width="+width+" height="+height+" _halfSize="+halfSize);

		double [][][] sampleCoord= focusMeasurementParameters.sampleCoordinates(
				x0,   // lens center on the sensor
				y0);  // lens center on the sensor
		this.focusMask =new boolean[this.flatFieldForGrid.length];
		for (int i=0;i<this.focusMask.length;i++) this.focusMask[i]=false;
		for (int i=0;i<focusMeasurementParameters.numSamples[1];i++){
			//System.out.println(i+": y0="+y0);
			for (int j=0;j<focusMeasurementParameters.numSamples[0];j++){
				int xs=(int) sampleCoord[i][j][0];
				int ys=(int) sampleCoord[i][j][1];
				//System.out.println(j+": xs="+xs);
				for (int y=ys-halfSize;y<(ys+halfSize);y++) if ((y>=0) && (y<height)) {
					for (int x=xs-halfSize;x<(xs+halfSize);x++) if ((x>=0) && (x<width)) {
						this.focusMask[y*width+x]=true;
					}
Andrey Filippov's avatar
Andrey Filippov committed
6713
				}
6714
			}
6715

6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740
		}
		// Do we really need to zero the this.flatFieldForGrid where this.focusMask is false? We may need
		// the pixels out of WOI to update grid around the needed nodes
		//			   for (int i=0;i<this.focusMask.length;i++) if (!this.focusMask[i]) this.flatFieldForGrid[i]=0.0;
		if (this.PATTERN_GRID.length==0) return;
		boolean [][] maskUV=new boolean[this.PATTERN_GRID.length][this.PATTERN_GRID[0].length];
		int [] iUV={0,0};
		for (iUV[1]=0;iUV[1]<maskUV.length;iUV[1]++) for (iUV[0]=0;iUV[0]<maskUV[0].length;iUV[0]++) {
			maskUV[iUV[1]][iUV[0]]=false;
			if (isCellDefined(this.PATTERN_GRID, iUV)){
				int x= (int) Math.round(this.PATTERN_GRID[iUV[1]][iUV[0]][0][0]);
				int y= (int) Math.round(this.PATTERN_GRID[iUV[1]][iUV[0]][0][1]);
				if ((x>=0) && (x<width) && (y>=0) && (y<height) && this.focusMask[y*width+x]) maskUV[iUV[1]][iUV[0]]=true;
			}
		}
		for (iUV[1]=0;iUV[1]<maskUV.length;iUV[1]++) for (iUV[0]=0;iUV[0]<maskUV[0].length;iUV[0]++) if (!maskUV[iUV[1]][iUV[0]]){
			boolean neibExists=false;
			for (int d=0;d<dirs.length;d++) {
				int [] iUV1={iUV[0]+dirs[d][0],iUV[1]+dirs[d][1]};
				if (isCellDefined(this.PATTERN_GRID, iUV1) && maskUV[iUV1[1]][iUV1[0]]){
					neibExists=true;
					break;
				}
			}
			if (!neibExists) clearPatternGridCell(this.PATTERN_GRID,iUV);
6741

6742 6743 6744
		}
	}
	/* ======================================================================== */
6745
	@Deprecated
6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766
	public double[][] getPointersXY(ImagePlus imp, int numPointers){
		// try absolute grid calibratrion
		// read image info to properties (if it was not done yet - should it?
		if ((imp.getProperty("timestamp")==null) || (((String) imp.getProperty("timestamp")).length()==0)) {
			JP46_Reader_camera jp4_instance= new JP46_Reader_camera(false);
			jp4_instance.decodeProperiesFromInfo(imp);
		}
		double [][] pointersXY=new double[numPointers][];
		int numPointerDetected=0;
		for (int i=0;i<pointersXY.length;i++) {
			pointersXY[i]=null;
			if ((imp.getProperty("POINTER_X_"+i)!=null) && (imp.getProperty("POINTER_Y_"+i)!=null)) {
				pointersXY[i]=new double[2];
				pointersXY[i][0]=Double.parseDouble((String) imp.getProperty("POINTER_X_"+i));
				pointersXY[i][1]=Double.parseDouble((String) imp.getProperty("POINTER_Y_"+i));
				numPointerDetected++;
			}
		}
		if (numPointerDetected>0) return pointersXY;
		else return null;
	}
6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832

	// laserPointer used as a backup if images do not contain data, and specify if they are needed at all (null - skip)
	public static double[][] getPointersXYUV(ImagePlus imp, LaserPointer laserPointer ){
		int numPointers=(laserPointer!=null)?laserPointer.laserUVMap.length:0;
		// try absolute grid calibratrion
		// read image info to properties (if it was not done yet - should it?
		if ((imp.getProperty("timestamp")==null) || (((String) imp.getProperty("timestamp")).length()==0)) {
			JP46_Reader_camera jp4_instance= new JP46_Reader_camera(false);
			jp4_instance.decodeProperiesFromInfo(imp);
		}
		double [][] pointersXY=new double[numPointers][];
		int numPointerDetected=0;
		// Normally all pointers should either have or not U,V - the smallest one will later be used
		for (int i=0;i<pointersXY.length;i++) {
			pointersXY[i]=null;
			if ((imp.getProperty("POINTER_X_"+i)!=null) && (imp.getProperty("POINTER_Y_"+i)!=null)) {
				if ((imp.getProperty("POINTER_U_"+i)!=null) && (imp.getProperty("POINTER_V_"+i)!=null)) {
					pointersXY[i]=new double[4];
					pointersXY[i][2]=Double.parseDouble((String) imp.getProperty("POINTER_U_"+i));
					pointersXY[i][3]=Double.parseDouble((String) imp.getProperty("POINTER_V_"+i));
				} else {
					pointersXY[i]=new double[2];
				}
				pointersXY[i][0]=Double.parseDouble((String) imp.getProperty("POINTER_X_"+i));
				pointersXY[i][1]=Double.parseDouble((String) imp.getProperty("POINTER_Y_"+i));
				numPointerDetected++;
			}
		}
		if (numPointerDetected>0) return pointersXY;
		else return null;
	}

	public static void setPointersXYUV(ImagePlus imp, double [][] pointersXYUV){

//		JP46_Reader_camera jp4_instance= new JP46_Reader_camera(false);
//		jp4_instance.decodeProperiesFromInfo(imp);
		String prefix = "POINTER_";
		Properties prop = imp.getProperties();
		// Delete all properties starting with "POINTER_"
		Enumeration<Object> enumKey = prop.keys();
		while(enumKey.hasMoreElements()) {
			Object key = enumKey.nextElement();
			if (((String) key).startsWith(prefix)) {
				prop.remove(key);
			}
		}
		// Delete all properties starting with "POINTER_"
		int num_used = 0;
		for (int i=0;i<pointersXYUV.length;i++) if (pointersXYUV[i] != null) {
			prop.setProperty(prefix+"X_"+i, ""+pointersXYUV[i][0]);
			prop.setProperty(prefix+"Y_"+i, ""+pointersXYUV[i][1]);
			if (pointersXYUV[i].length > 2) {
				prop.setProperty(prefix+"U_"+i, ""+pointersXYUV[i][2]);
				prop.setProperty(prefix+"V_"+i, ""+pointersXYUV[i][3]);
			}
			num_used++;
		}
		prop.setProperty("USED_POINTERS",""+num_used);
//		jp4_instance.encodeProperiesToInfo(imp);
//		(new FileSaver(imp)).saveAsTiff(path);

	}




6833
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
6834

6835 6836 6837 6838 6839 6840 6841 6842 6843
	public String getChannel(ImagePlus imp){
		// read image info to properties (if it was not done yet - should it?
		if ((imp.getProperty("timestamp")==null) || (((String) imp.getProperty("timestamp")).length()==0)) {
			JP46_Reader_camera jp4_instance= new JP46_Reader_camera(false);
			jp4_instance.decodeProperiesFromInfo(imp);
		}
		if (imp.getProperty("channel")==null) return null;
		return (String) imp.getProperty("channel");
	}
6844

6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948
	/* ======================================================================== */
	public void showFlatFieldForGrid(){
		if (this.flatFieldForGrid!=null) this.SDFA_INSTANCE.showArrays(this.flatFieldForGrid, getImageWidth(), getImageHeight(), "Flat_field_for_grid");
	}
	public void showFFCorrectedGrid(){
		if (this.gridFFCorr!=null) this.SDFA_INSTANCE.showArrays(this.gridFFCorr, getImageWidth(), getImageHeight(), "Flat_field_corrected_grid");
	}
	public void showFocusMask(){
		if (this.focusMask!=null){
			double [] dfm=new double [this.focusMask.length];
			for (int i=0;i<dfm.length;i++) dfm[i]=this.focusMask[i]?1.0:0.0;
			this.SDFA_INSTANCE.showArrays(dfm, getImageWidth(), getImageHeight(), "Focus_mask");
		}
	}
	public void showUVIndex(){
		if (this.UV_INDEX!=null){
			double [] uv=new double [this.UV_INDEX.length];
			for (int i=0;i<uv.length;i++) uv[i]=this.UV_INDEX[i];
			this.SDFA_INSTANCE.showArrays(uv, getImageWidth(), getImageHeight(), "UV_INDEX");
		}
	}
	public boolean patternOK(){
		return (this.PATTERN_GRID!=null);
	}
	public double [][][][] getDArray(){
		return this.PATTERN_GRID;
	}
	public double [][][] getDArray(int v){
		return this.PATTERN_GRID[v];
	}
	public double [][] getDArray(int v, int u){
		return this.PATTERN_GRID[v][u];
	}
	public double [] getDArray(int v, int u, int n){
		return this.PATTERN_GRID[v][u][n];
	}
	public double getDArray(int v, int u, int n, int k){
		return this.PATTERN_GRID[v][u][n][k];
	}
	public double [] getXY (int v, int u){
		return this.PATTERN_GRID[v][u][0];
	}
	public int getDArrayHeight(){
		if (this.PATTERN_GRID==null) return 0;
		return this.PATTERN_GRID.length;
	}
	public int getDArrayWidth(){
		if (this.PATTERN_GRID==null) return 0;
		return this.PATTERN_GRID[0].length;
	}
	//			matchSimulatedPattern.DIST_SELECTION.width, // image (mask) width
	public Rectangle getWOI(){
		return this.DIST_SELECTION;
	}
	public int getImageWidth(){
		return this.UV_INDEX_WIDTH;
	}
	public int getImageHeight(){
		if (this.UV_INDEX==null) return 0;
		return this.UV_INDEX.length/this.UV_INDEX_WIDTH;
	}
	public void setWOI(Rectangle woi){
		this.DIST_SELECTION=new Rectangle(woi) ;
	}
	public void setWOI(int x, int y, int w, int h){
		this.DIST_SELECTION=new Rectangle(x, y, w, h) ;
	}
	public int [] getUVIndex(){
		return this.UV_INDEX;
	}
	public int getUVIndex(int i){
		if ((i<0) || (i>=this.UV_INDEX.length)) return -1; // let it throw exception
		return this.UV_INDEX[i];
	}
	public int getUVIndex(int x, int y){
		if ((x<0) || (y<0) || (x>=this.PATTERN_GRID[0].length)  || (y>=this.PATTERN_GRID.length)) return -1;
		return this.UV_INDEX[x+UV_INDEX_WIDTH*y];
	}
	/**
	 * Returns a pair of U,V from xy, integer result (does not interpolate
	 * @param x - pixel coordinat X
	 * @param y - pixel coordinat Y
	 * @return {u,v} pair
	 */
	public int [] getUV(int x, int y)    {
		if ((x<0) || (y<0) || (x>=this.UV_INDEX_WIDTH) || (y>=(this.UV_INDEX.length/this.UV_INDEX_WIDTH))) return null; // out of UV_INDEX bounds
		if ((x+this.UV_INDEX_WIDTH*y)>this.UV_INDEX.length) {
			if (this.debugLevel>0){
				System.out.println("getUV("+x+","+y+"): this.UV_INDEX.length="+this.UV_INDEX.length+", this.UV_INDEX_WIDTH="+this.UV_INDEX_WIDTH);
			}
			return null;
		}
		int index=this.UV_INDEX[x+this.UV_INDEX_WIDTH*y];
		if (index<0) return null; // <0 - undefined
		int width=this.PATTERN_GRID[0].length;
		int [] uv={index % width,index / width};
		return uv;
	}
	/**
	 * Map estimated grid hintGrid  to measured
	 * @param hintGrid [v][u][ 0-x, 1-y, 2 - u, 3- v]
	 * @param searchAround  how far (in pixels) to look for the nearest defined one if the specified is undefined
	 * @return array [v][u] [0 - measured u, 1 - measured v, 2 - measured contrast] ([v][u]==null no measured grid there
	 */
Andrey Filippov's avatar
Andrey Filippov committed
6949

6950 6951 6952 6953 6954 6955 6956
	public double [][][] mapEstimatedToMeasured(double [][][] hintGrid, double searchAround){
		double [][][] measuredUV=new double[hintGrid.length][hintGrid[0].length][];
		for (int v=0;v<measuredUV.length;v++) for (int u=0;u<measuredUV[0].length;u++)
			if (hintGrid[v][u]!=null) measuredUV[v][u]=getUVLinear(hintGrid[v][u][0],hintGrid[v][u][1],searchAround);
			else measuredUV[v][u]=null;
		return measuredUV;
	}
Andrey Filippov's avatar
Andrey Filippov committed
6957

6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980
	/**
	 * Calculate linear matrix (2x3) parameters measured grid UV from estimated grid UV
	 * @param hintGrid  [v][u][ 0-x, 1-y, 2 - u, 3- v]
	 * @param searchAround how far (in pixels) to look for the nearest defined one if the specified is undefined
	 * @return {{Au, Bu, Cu}.{Av, Bc, Cv}}; where Umeas=Au*Uhint+Bu*Vhint+Cu, Vmeas=Av*Uhint+Bv*Vhint+Cv
	 */
	public double [][] calcGridMatchMatrix (double [][][] hintGrid, double searchAround){
		double [][][] measuredUV=mapEstimatedToMeasured(hintGrid, searchAround); // contrast - minimal of the ones around
		if (this.debugLevel>2){
			double [][] pixels=new double[9][measuredUV.length*measuredUV[0].length];
			int index=0;
			String [] titles={"grid-U","grid-V","contrast","hint-X","hint-Y","hint-U","hint-V","grid-hint-U","grid-hint-V"}; // last 2 only valid for rotation==0
			for (int v=0; v<measuredUV.length;v++) for (int u=0;u<measuredUV[v].length;u++){
				if (measuredUV[v][u]!=null){
					for (int i=0; i<3;i++)	pixels[i][index]=measuredUV[v][u][i];
					for (int i=0; i<4;i++)	pixels[i+3][index]=hintGrid[v][u][i];
					for (int i=0; i<2;i++)	pixels[i+7][index]=measuredUV[v][u][i]-hintGrid[v][u][i+2];
				} else {
					for (int i=0; i<9;i++)	pixels[i][index]=Double.NaN;
				}
				index++;
			}
			(new ShowDoubleFloatArrays()).showArrays(pixels, measuredUV[0].length, measuredUV.length,  true, "measuredUV", titles);
6981

6982
		}
6983

6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046
		int numDefined=0;
		for (int v=0;v<measuredUV.length;v++) for (int u=0;u<measuredUV[v].length;u++) if (measuredUV[v][u]!=null) numDefined++;
		double [][][] data =new double [numDefined][3][];//        	   double [][][] data =new double [numDefined][2][2];
		int index=0;
		for (int v=0;v<measuredUV.length;v++) for (int u=0;u<measuredUV[v].length;u++) if (measuredUV[v][u]!=null) {
			data[index][0]=new double[2];
			data[index][1]=new double[2];
			data[index][2]=new double[1];
			data [index][0][0]=hintGrid[v][u][2]; // hinted U
			data [index][0][1]=hintGrid[v][u][3]; // hinted V
			data [index][1][0]=measuredUV[v][u][0]; // measured U
			data [index][1][1]=measuredUV[v][u][1]; // measured V
			data [index][2][0]=measuredUV[v][u][2]; // contrast
			index++;
		}
		if (this.debugLevel>0) {
			System.out.println("calcGridMatchMatrix(), data.length="+data.length+" measuredUV.length="+measuredUV.length+
					((measuredUV.length>0)?(", measuredUV[0].length="+measuredUV[0].length):""));
			if (this.debugLevel>3) for (index=0;index<data.length;index++){
				System.out.println(data[index][0][0]+","+data[index][0][1]+","+data[index][1][0]+","+data[index][1][1]+","+data[index][2][0]);
			}
		}
		//			   int gridRotation=-1; //undefined
		if (data.length<3){
			///        		   if (this.debugLevel>0) System.out.println("calcGridMatchMatrix(), data.length="+data.length+" measuredUV.length="+measuredUV.length+
			///        				   ((measuredUV.length>0)?(", measuredUV[0].length="+measuredUV[0].length):""));
			return null;
		}
		double [][] coeff=new PolynomialApproximation(this.debugLevel).quadraticApproximation(data, true);
		if (coeff!=null) {
			//				   gridRotation=matrixToRot(coeff);
			int rot=matrixToRot(coeff);
			boolean [] flips=rotToFlips(rot);
			double [][] aI={{1,0},{0,1}};
			double [][] aSwap= {{ 0,1},{1,0}};
			double [][] aFlipU={{-1,0},{0,1}};
			double [][] aFlipV={{ 1,0},{0,-1}};
			Matrix M=new Matrix(aI);
			if (flips[0]) M=M.times((new Matrix(aSwap)));
			if (flips[1]) M=M.times((new Matrix(aFlipU)));
			if (flips[2]) M=M.times((new Matrix(aFlipV)));
			// now M reconstructs coeff
			double [][]aM=M.getArray();
			double SMU=0.0,SMV=0.0,SW=0.0;
			for (int i=0;i<data.length;i++){
				SMU+=data[i][2][0]*(data[i][1][0]-(aM[0][0]*data[i][0][0]+aM[0][1]*data[i][0][1]));
				SMV+=data[i][2][0]*(data[i][1][1]-(aM[1][0]*data[i][0][0]+aM[1][1]*data[i][0][1]));
				SW+=data[i][2][0];
			}
			SMU/=SW;
			SMV/=SW;
			if (this.debugLevel>0) {
				System.out.println("coeff[0][0]="+coeff[0][0]+" coeff[0][1]="+coeff[0][1]+" coeff[0][2]="+coeff[0][2]);
				System.out.println("coeff[1][0]="+coeff[1][0]+" coeff[1][1]="+coeff[1][1]+" coeff[1][2]="+coeff[1][2]);
				System.out.println("aM[0][0]="+aM[0][0]+" aM[0][1]="+aM[0][1]+" SMU="+SMU);
				System.out.println("aM[1][0]="+aM[1][0]+" aM[1][1]="+aM[1][1]+" SMV="+SMV);
			}
			coeff[0][0]=aM[0][0];
			coeff[0][1]=aM[0][1];
			coeff[1][0]=aM[1][0];
			coeff[1][1]=aM[1][1];
			coeff[0][2]=SMU;
			coeff[1][2]=SMV;
Andrey Filippov's avatar
Andrey Filippov committed
7047

7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063
		}
		return coeff;
	}
	public int matrixToRot(double [][] coeff){
		boolean [] flips = {false,false,false};
		double  [][] aR={{coeff[0][0],coeff[0][1]},{coeff[1][0],coeff[1][1]}};
		double  [][] aSwap={{0,1},{1,0}};
		Matrix R = new Matrix (aR);
		if ((aR[0][0]*aR[0][0]+aR[1][1]*aR[1][1]) < (aR[1][0]*aR[1][0]+aR[0][1]*aR[0][1])){
			flips[0]=true;
			R=(new Matrix(aSwap)).times(R);
		}
		flips[1]=R.getArray()[0][0]<0;
		flips[2]=R.getArray()[1][1]<0;
		return flipsToRot(flips[0],flips[1],flips[2]);
	}
Andrey Filippov's avatar
Andrey Filippov committed
7064

7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118
	public int [][] gridMatrixApproximate(double [][] coeff){
		int rot=matrixToRot(coeff);
		boolean [] flips=rotToFlips(rot);
		double [][] aI={{1,0},{0,1}};
		double [][] aSwap= {{ 0,1},{1,0}};
		double [][] aFlipU={{-1,0},{0,1}};
		double [][] aFlipV={{ 1,0},{0,-1}};
		Matrix M=new Matrix(aI);
		if (flips[0]) M=M.times((new Matrix(aSwap)));
		if (flips[1]) M=M.times((new Matrix(aFlipU)));
		if (flips[2]) M=M.times((new Matrix(aFlipV)));
		// now M reconstructs coeff
		double [][]aM=M.getArray();
		// Black/white cells have to be flipped if flipU XOR flipW, regardless of swapUV
		int flipForWhite=(flips[1]^flips[2])?1:0;
		int [][] shifts={
				{2*((int) Math.round(0.5*(coeff[0][2]+0))),   2*((int) Math.round(0.5*(coeff[1][2]+flipForWhite)))    -flipForWhite},
				{2*((int) Math.round(0.5*(coeff[0][2]-1)))+1, 2*((int) Math.round(0.5*(coeff[1][2]+flipForWhite-1)))+1-flipForWhite}
		};
		int shiftSelect=(
				((shifts[0][0]-coeff[0][2])*(shifts[0][0]-coeff[0][2]) + (shifts[0][1]-coeff[1][2])*(shifts[0][1]-coeff[1][2]))>
				((shifts[1][0]-coeff[0][2])*(shifts[1][0]-coeff[0][2]) + (shifts[1][1]-coeff[1][2])*(shifts[1][1]-coeff[1][2])))?1:0;
		if (this.debugLevel>1){
			double d1=Math.sqrt((shifts[0][0]-coeff[0][2])*(shifts[0][0]-coeff[0][2]) + (shifts[0][1]-coeff[1][2])*(shifts[0][1]-coeff[1][2]));
			double d2=Math.sqrt((shifts[1][0]-coeff[0][2])*(shifts[1][0]-coeff[0][2]) + (shifts[1][1]-coeff[1][2])*(shifts[1][1]-coeff[1][2]));
			System.out.println("gridMatrixApproximate(): shifts[0][0]="+shifts[0][0]+" shifts[0][1]="+shifts[0][1]+
					" shifts[1][0]="+shifts[1][0]+" shifts[1][1]="+shifts[1][1]+ " shiftSelect="+shiftSelect+ " d1="+d1+" d2="+d2);
		}
		int [][] iCoeff={
				//        			   {(int) Math.round(aM[0][0]), (int) Math.round(aM[0][1]),(int) Math.round(coeff[0][2])},
				//        			   {(int) Math.round(aM[1][0]), (int) Math.round(aM[1][1]),(int) Math.round(coeff[1][2])}};
				{(int) Math.round(aM[0][0]), (int) Math.round(aM[0][1]),shifts[shiftSelect][0]},
				{(int) Math.round(aM[1][0]), (int) Math.round(aM[1][1]),shifts[shiftSelect][1]}};
		return iCoeff;
	}
	// old version, no distinction between B and W
	public int [][] gridMatrixApproximateNoBW(double [][] coeff){
		int rot=matrixToRot(coeff);
		boolean [] flips=rotToFlips(rot);
		double [][] aI={{1,0},{0,1}};
		double [][] aSwap= {{ 0,1},{1,0}};
		double [][] aFlipU={{-1,0},{0,1}};
		double [][] aFlipV={{ 1,0},{0,-1}};
		Matrix M=new Matrix(aI);
		if (flips[0]) M=M.times((new Matrix(aSwap)));
		if (flips[1]) M=M.times((new Matrix(aFlipU)));
		if (flips[2]) M=M.times((new Matrix(aFlipV)));
		// now M reconstructs coeff
		double [][]aM=M.getArray();
		int [][] iCoeff={
				{(int) Math.round(aM[0][0]), (int) Math.round(aM[0][1]),(int) Math.round(coeff[0][2])},
				{(int) Math.round(aM[1][0]), (int) Math.round(aM[1][1]),(int) Math.round(coeff[1][2])}};
		return iCoeff;
	}
Andrey Filippov's avatar
Andrey Filippov committed
7119

7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188
	public double worstGridMatchRotSkew(double [][] coeff){
		int [][] iCoeff=gridMatrixApproximate(coeff);
		double worst=0;
		for (int i=0;i<2;i++) for (int j=0;j<2;j++){
			double d=Math.abs(coeff[i][j]-iCoeff[i][j]);
			if (d>worst) worst=d;
		}
		return worst;
	}
	public double worstGridMatchTranslate(double [][] coeff){ // in grids half-periods, not pixels!
		int [][] iCoeff=gridMatrixApproximate(coeff);
		double worst=0;
		for (int i=0;i<2;i++){
			double d=Math.abs(coeff[i][2]-iCoeff[i][2]);
			if (d>worst) worst=d;
		}
		return worst;
	}
	//searchAround

	/**
	 * Find double u,v from double x,y by linear interpolation from neighbor cells. Requires  this.UV_INDEX to be calculated and
	 * matching this.PATTERN_GRID
	 * @param x pixel coordinate
	 * @param y  pixel coordinate
	 * @param searchAround how far (in pixels) to look for the nearest defined one if the specified is undefined
	 * @return uv pair or null - modified - triplet, last - contrast
	 */
	public double [] getUVLinear(double x, double y, double searchAround){
		int ix0= (int) Math.round(x);
		int iy0= (int) Math.round(y);
		int    ix=ix0,iy=iy0;
		int [] uv0=getUV(ix, iy);
		double best2=searchAround*searchAround+1;
		// if the point is slightly out of grid - find the one near
		if (uv0==null) {
			for (int iy1=iy-((int) Math.round(searchAround));iy1>=iy+searchAround;iy1++)
				for (int ix1=ix-((int) Math.round(searchAround));ix1>=ix+searchAround;ix1++) {
					double d= (ix1-ix)*(ix1-ix)+(iy1-iy)*(iy1-iy);
					if (d<best2)  {
						uv0=getUV(ix1, iy1);
						if (uv0!=null){
							best2=d;
							ix=ix1;
							iy=iy1;
						}
					}
				}

		}
		if (uv0==null) return null; // no grid points near
		int [][] dirDiffs={{1,1},{-1,1},{1,-1},{-1,-1}};
		double [] xy0= cellXYC(uv0);
		double [] xy1=null;
		double [] xy2=null;
		int [] deltaUV=null;
		for (int dir=0;dir<dirDiffs.length;dir++) {
			xy1=cellXYC(uv0[0]+dirDiffs[dir][0],uv0[1]);
			xy2=cellXYC(uv0[0]                 ,uv0[1]+dirDiffs[dir][1]);
			if ((xy1!=null) && (xy2!=null)) {
				deltaUV=new int[2];
				deltaUV[0]=dirDiffs[dir][0];
				deltaUV[1]=dirDiffs[dir][1];
				break;
			}
		}
		double minContrast=Math.min(Math.min(xy1[2], xy2[2]),xy0[2]); // minimal contrast off all 3 points
		if (deltaUV==null) return null; // could not find 2 orthogonal neighbors to interpolate
		/*
Andrey Filippov's avatar
Andrey Filippov committed
7189 7190 7191
x=xy0[0] + dU*deltaUV[0]*(xy1[0]-xy0[0])+dV*deltaUV[1]*(xy2[0]-xy0[0])
y=xy0[1] + dU*deltaUV[0]*(xy1[1]-xy0[1])+dV*deltaUV[1]*(xy2[1]-xy0[1])

7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227
		 */
		double [][] aM={
				{deltaUV[0]*(xy1[0]-xy0[0]),deltaUV[1]*(xy2[0]-xy0[0])},
				{deltaUV[0]*(xy1[1]-xy0[1]),deltaUV[1]*(xy2[1]-xy0[1])}};
		Matrix M=new Matrix(aM);
		double [][] aB={{x-xy0[0]},{y-xy0[1]}};
		Matrix B=new Matrix(aB);
		if (!(new LUDecomposition(M)).isNonsingular()){
			System.out.println("getUVLinear("+x+","+y+"): Matix is singular:");
			M.print(10, 6);
			return null;
		}
		double [] dUV=M.solve(B).getRowPackedCopy();
		double [] result={uv0[0]+dUV[0],uv0[1]+dUV[1],minContrast};
		return result;
	}
	public void invalidateAll(){
		invalidateCalibration();
		invalidateFlatFieldForGrid();
		invalidateFocusMask();
	}
	private void invalidateCalibration(){
		this.reMap=null;    // invalidate if any
		this.targetUV=null; // invalidate if any
		this.pXYUV=null; // invalidate if any
		this.passNumber=1;
		resetCorrelationSizesUsed(); // reset which FFT sizes where used in correlation
	}
	public void invalidateFlatFieldForGrid(){
		this.flatFieldForGrid=null; // reset flat field for grid
		this.gridContrastBrightness=null;
	}
	public void invalidateFocusMask(){
		this.focusMask=null;
	}
	/* get height and width of the measured pattern array applies to PATTERN_GRID, targetUV and pixelsUV */
7228

7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248
	public int getHeight(){
		if (this.pXYUV==null) return 0;
		return this.pXYUV.length;
	}
	public int getWidth(){
		if (this.pXYUV==null) return 0;
		return this.pXYUV[0].length;
	}
	/* Get physical target UV pair from measured pattern. Requires absolute mapping (by laser spots)
	 *  Pair may be null if no pattern is detected for this node in the image
	 */
	public int [][][] getTargetUV(){
		return this.targetUV;
	}
	/* Get pixel X,Y pair for each node in the measured pattern. Calculated during absolute mapping (by laser spots)
	 *  Pair may be null if no pattern is detected for this node in the image
	 */
	public double [][][] getPXYUV(){
		return this.pXYUV;
	}
7249

7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264
	public int restorePatternGridFromGridList(
			double [][][] pixelsXYSet,
			int [][][] pixelsUVSet,
			double [] intensityRange){
		double maxX=0,maxY=0;
		for (int n=0;n<pixelsXYSet.length;n++){
			for (int i=0;i<pixelsXYSet[n].length;i++){
				if (pixelsXYSet[n][i][0]>maxX) maxX=pixelsXYSet[n][i][0];
				if (pixelsXYSet[n][i][1]>maxY) maxY=pixelsXYSet[n][i][1];
			}
		}
		int width=(int) Math.ceil(maxX)+1;
		int height=(int) Math.ceil(maxY)+1;
		return restorePatternGridFromGridList(pixelsXYSet, pixelsUVSet, width,height,intensityRange);
	}
7265

7266 7267 7268 7269 7270 7271 7272 7273
	/**
	 * Restore this.PATTERN_GRID array (no wave vectors) from the lists use in Distortions class
	 * @param pixelsXYSet list of the {x,y} pairs for each grid node (now - a pair of lists used pixels and those that did not fit into physical target)
	 * @param pixelsUVSet list of {u,v} pairs for each grid node (now - a pair of lists)
	 * @param width sensor (image) width
	 * @param height sensor (image) height
	 * @return number of cells in the grid
	 */
7274

7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327
	public int restorePatternGridFromGridList(
			double [][][] pixelsXYSet,
			int [][][] pixelsUVSet,
			int width,
			int height,
			double [] intensityRange){
		int numCells=0;
		int minU=0,minV=0,maxU=0,maxV=0;
		this.PATTERN_GRID=null;
		setWOI(0, 0, 0, 0);
		if ((pixelsXYSet!=null) && (pixelsUVSet!=null ) &&  (pixelsXYSet.length>0)){
			setWOI(0, 0, width, height);//  set WOI for the current image
			for (int n=0;n<pixelsXYSet.length;n++)
				for (int i=0;i<pixelsXYSet[n].length;i++) if ((pixelsXYSet[n][i]!=null)&&(pixelsUVSet[n][i]!=null)) {
					if (numCells==0){
						minV=pixelsUVSet[n][i][1];
						maxV=pixelsUVSet[n][i][1];
						minU=pixelsUVSet[n][i][0];
						maxU=pixelsUVSet[n][i][0];
					} else {
						if (minV>pixelsUVSet[n][i][1])  minV=pixelsUVSet[n][i][1];
						else if (maxV<pixelsUVSet[n][i][1])  maxV=pixelsUVSet[n][i][1];
						if (minU>pixelsUVSet[n][i][0])  minU=pixelsUVSet[n][i][0];
						else if (maxU<pixelsUVSet[n][i][0])  maxU=pixelsUVSet[n][i][0];
					}
					numCells++;
				}
			if (numCells>0) {
				// do not break black/white correspondence, always move by even number of cells
				if ((minU & 1)!=0 )minU--;
				if ((minV & 1)!=0 )minV--;
				this.minUV[0]=minU;
				this.minUV[1]=minV; // save shift to restore later
				this.PATTERN_GRID=setPatternGridArray(maxU-minU+1,maxV-minV+1);
				this.gridContrastBrightness=new double[4][this.PATTERN_GRID.length][this.PATTERN_GRID[0].length]; //{grid contrast, grid intensity red, grid intensity green, grid intensity blue}[v][u]
				for (int n=0;n<4;n++)
					for (int v=0;v<this.gridContrastBrightness[0].length;v++)
						for (int u=0;u<this.gridContrastBrightness[0][0].length;u++)
							this.gridContrastBrightness[n][v][u]=0.0;

				for (int n=0;n<pixelsXYSet.length;n++)
					for (int i=0;i<pixelsXYSet[n].length;i++) if ((pixelsXYSet[n][i]!=null)&&(pixelsUVSet[n][i]!=null)) {
						int [] shiftedUV={pixelsUVSet[n][i][0]-minU,pixelsUVSet[n][i][1]-minV};
						setPatternGridCell(
								this.PATTERN_GRID,
								shiftedUV, //pixelsUV[i],
								pixelsXYSet[n][i], // will have extra data
								null,
								null);
						this.gridContrastBrightness[0][shiftedUV[1]][shiftedUV[0]]=pixelsXYSet[n][i][2];
						this.gridContrastBrightness[1][shiftedUV[1]][shiftedUV[0]]=pixelsXYSet[n][i][3]*intensityRange[0];
						this.gridContrastBrightness[2][shiftedUV[1]][shiftedUV[0]]=pixelsXYSet[n][i][4]*intensityRange[1];
						this.gridContrastBrightness[3][shiftedUV[1]][shiftedUV[0]]=pixelsXYSet[n][i][5]*intensityRange[2];
7328

7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365
					}
			}
		}
		return numCells;
	}
	/**
	 * Create PATTERN_GRID for calculated grid (for sensor parameters, orientation) for debugging purposes
	 * @param hintGrid  grid array [v][u][0- x,  1 - y, 2 - u, 3 - v] (u,v - not used here)
	 * @param width     image width, in pixels
	 * @param height    image height, in pixels
	 * @return          number of non-empty cells
	 */
	public int restoreSimulatedPatternGridFromHint(double [][][] hintGrid, int width, int height){
		int numCells=0;
		this.PATTERN_GRID=null;
		if (hintGrid==null) return 0;
		setWOI(0, 0, width, height);//  set WOI for the current image
		//        	   this.PATTERN_GRID=setPatternGridArray(hintGrid[0].length+1,hintGrid.length+1);
		this.PATTERN_GRID=setPatternGridArray(hintGrid[0].length,hintGrid.length);
		for (int v=0;v<hintGrid.length;v++) for (int u=0;u<hintGrid[v].length;u++) if (hintGrid[v][u]!=null){
			double [] xy={hintGrid[v][u][0],hintGrid[v][u][1]};
			if ((xy[0]>=0) && (xy[1]>=0) && (xy[0]<width) && (xy[1]<height)) {
				int    [] uv={u,v};
				//            		   if ((xy[0]==0) && (xy[1]==0)) {
				//            			   System.out.println("x==0,y==0 for u="+u+", v="+v+", hintGrid[v][u][2]="+hintGrid[v][u][2]+", hintGrid[v][u][3]="+hintGrid[v][u][3]);
				//            		   }
				setPatternGridCell(
						this.PATTERN_GRID,
						uv,
						xy,
						null,
						null);
				numCells++;
			}
		}
		return numCells;
	}
7366 7367


7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475
	/**
	 * restore grid parameters - this.PATTERN_GRID (no wave vectors) only
	 * absolute calibration (if any) is lost, only orientation is preserved
	 * @param imp_grid - grid encoded as image
	 * @return array of laser pointers coordinates - no separate, return number of grid cells
	 */
	public int restorePatternGridFromImage(ImagePlus imp_grid){
		int numCells=0;
		this.PATTERN_GRID=null;
		setWOI(0, 0, 0, 0);
		if (imp_grid!=null){
			setWOI(0, 0, imp_grid.getWidth(), imp_grid.getHeight());//  set WOI for the current image
			ImageStack stack=imp_grid.getStack();
			float [][] pixels=new float[4][];
			if ((stack==null) || (stack.getSize()!=4)) {
				String msg="Expected a 4-slice stack in "+imp_grid.getTitle();
				IJ.showMessage("Error",msg);
				throw new IllegalArgumentException (msg);
			}
			for (int i=0;i<4;i++) pixels[i]= (float[]) stack.getPixels(i+1); // pixel X : negative - no grid here
			int minU=0,minV=0,maxU=0,maxV=0;
			for (int i=0;i<pixels[0].length;i++) if (pixels[0][i]>=0) {
				int u=Math.round(pixels[2][i]);
				int v=Math.round(pixels[3][i]);
				if (numCells==0){
					minV=v;
					maxV=v;
					minU=u;
					maxU=u;
				} else {
					if (minV>v)  minV=v;
					else if (maxV<v)  maxV=v;
					if (minU>u)  minU=u;
					else if (maxU<u)  maxU=u;
				}
				numCells++;
			}
			if (numCells>0) {
				setPatternGridArray(maxU-minU+1,maxV-minV+1);
				double [] xy=new double[2];
				int [] uv=new int[2];
				for (int i=0;i<pixels[0].length;i++) if (pixels[0][i]>=0) {
					uv[0]=(Math.round(pixels[2][i]))-minU;
					uv[1]=(Math.round(pixels[3][i]))-minV;
					xy[0]=pixels[0][i];
					xy[1]=pixels[1][i];
					setPatternGridCell(
							this.PATTERN_GRID,
							uv,
							xy,
							null,
							null);
				}
			}
		}
		return numCells;
	}
	/**
	 * Calculate this.UV_INDEX (and this.UV_INDEX_WIDTH) - map from image pixel to U,V (U*this.UV_INDEX_WIDTH*V). this.DIST_SELECTION should be set
	 * @param imp source image (just to find image size, null - use this.DIST_SELECTION.width, this.DIST_SELECTION.height)
	 * @param shiftXY pattern shift (from debug), null - use {0,0}
	 * @param threadsMax limit on threads to use
	 * @param updateStatus update ImageJ status bar
	 * @param global_debug_level global debug level
	 * @param debug_level loop debug level
	 * @return true - OK, false - failure
	 */
	public boolean createUV_INDEX(
			ImagePlus imp, // or null - just to determine WOI (when getWOI matches image size)
			double [] shiftXY, // add to patterGrid xy, null OK
			int threadsMax,
			boolean updateStatus,
			int global_debug_level, // DEBUG_LEVEL
			int debug_level // debug level used inside loops
			){
		SimulationPattern simulationPattern=new SimulationPattern(); // do not need bitmap array here
		float [] UV_float0= simulationPattern.simulateGrid (
				getDArray(),
				2, // gridFrac, // number of grid steps per pattern full period
				null, //simulParameters,
				getWOI(),
				1, // SIMUL.subdiv/2,
				shiftXY,    // add to patterGrid xy, null OK
				threadsMax,
				updateStatus,
				debug_level); // debug level
		if (UV_float0==null) {
			System.out.println ("BUG: createUV_INDEX(): simulationPattern.simulateGrid() returnerd null");
			System.out.println ("BUG: createUV_INDEX(): getDArray() returnerd "+((getDArray()==null)?"null":"noy null"));
			return false;
		}
		float [] UV_float= simulationPattern.combineWithCanvas(
				-1.0,
				((imp==null)?(getWOI().width):imp.getWidth()),
				((imp==null)?(getWOI().height):imp.getHeight()),
				getWOI(),
				UV_float0 );
		if (global_debug_level>3) SDFA_INSTANCE.showArrays(UV_float0,getWOI().width, getWOI().height, "UV_float0"); // all -1
		if (global_debug_level>2) SDFA_INSTANCE.showArrays(UV_float, ((imp==null)?(getWOI().width):imp.getWidth()), ((imp==null)?(getWOI().height):imp.getHeight()), "UV_float");

		this.UV_INDEX=new int [UV_float.length];
		this.UV_INDEX_WIDTH=((imp==null)?(getWOI().width):imp.getWidth());
		for (int i=0;i<this.UV_INDEX.length;i++) this.UV_INDEX[i]=(int) UV_float[i];
		return true;
	}
	/**
	 * Apply hint grid and laser pointer calibration to the grid. If (hintGrid!= null) && (hintGridTolerance>0) than any result >=0 means match
	 * Create this.pixelsUV, this.targetUV
7476
	 * @param laserPointer Laser pointer parameters DEPRECATE, move to pointersXYUV
7477 7478 7479 7480 7481 7482 7483 7484
	 * @param pointersXY   pairs of detected pointers x,y (or nulls)
	 * @param removeOutOfGridPointers if true - remove pointers if they are outside of the pattern grid
	 * @param hintGrid predicted grid array (or null)
	 * @param hintGridTolerance allowed mismatch (fraction of period) or 0 - orientation only
	 * @param global_debug_level debug level
	 * @param noMessageBoxes do not open (and wait for) dialog boxes
	 * @return >=0 - number of laser pointers used for calibration, <0 - different errors
	 */
7485

7486
	public int combineGridCalibration_deprecated(
7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553
			LaserPointer laserPointer, // LaserPointer object or null
			double [][] pointersXY,
			boolean removeOutOfGridPointers, //
			double [][][] hintGrid, // predicted grid array (or null)
			double        hintGridTolerance, // alllowed mismatch (fraction of period) or 0 - orientation only
			int global_debug_level, // DEBUG_LEVEL
			boolean noMessageBoxes
			){
		int acalibrated=0;
		double [][] gridMatchCoeff=null;
		double searchAround=20.0; // how far to look for the grid node
		int gridRotation=-1; //undefined
		int [] iGridTranslateUV=null; // translate UV grid by these integer numbers
		if (hintGrid!=null){
			gridMatchCoeff=calcGridMatchMatrix (hintGrid, searchAround);
			// now already rounds rotate and re-replaces gridMatchCoeff with approximated, refines gridMatchCoeff[0][2] and gridMatchCoeff[1][2]
			if (gridMatchCoeff!=null) {
				gridRotation=matrixToRot(gridMatchCoeff);
				this.debugLevel=global_debug_level;
				int [][] iGridMatchCoeff=gridMatrixApproximate(gridMatchCoeff);
				if (global_debug_level>1){
					System.out.println("gridMatchCoeff[0]={"+IJ.d2s(gridMatchCoeff[0][0],5)+", "+IJ.d2s(gridMatchCoeff[0][1],5)+", "+IJ.d2s(gridMatchCoeff[0][2],5)+"}");
					System.out.println("gridMatchCoeff[1]={"+IJ.d2s(gridMatchCoeff[1][0],5)+", "+IJ.d2s(gridMatchCoeff[1][1],5)+", "+IJ.d2s(gridMatchCoeff[1][2],5)+"}");
					System.out.println("gridRotation="+gridRotation);
					System.out.println("iGridMatchCoeff[0]={"+iGridMatchCoeff[0][0]+", "+iGridMatchCoeff[0][1]+", "+iGridMatchCoeff[0][2]+"}");
					System.out.println("iGridMatchCoeff[1]={"+iGridMatchCoeff[1][0]+", "+iGridMatchCoeff[1][1]+", "+iGridMatchCoeff[1][2]+"}");
					System.out.println("worstGridMatchRotSkew()="+IJ.d2s(worstGridMatchRotSkew(gridMatchCoeff),5));
					System.out.println("worstGridMatchTranslate()="+IJ.d2s(worstGridMatchTranslate(gridMatchCoeff),5));
				}
				// hintGridTolerance==0 - do not try to determine shift from the hint (not reliable yet)
				if (hintGridTolerance>0) {
					if (worstGridMatchTranslate(gridMatchCoeff)<=hintGridTolerance){ // convert to pixels from halfperiods (or just chnage definition of hintGridTolerance)
						if (global_debug_level>1) System.out.println("worstGridMatchTranslate(gridMatchCoeff)= "+worstGridMatchTranslate(gridMatchCoeff)+", hintGridTolerance="+hintGridTolerance);
						iGridTranslateUV=new int[2];
						iGridTranslateUV[0]=iGridMatchCoeff[0][2];
						iGridTranslateUV[1]=iGridMatchCoeff[1][2];
					} else {
						if (global_debug_level>1) System.out.println("*** Warning: combineGridCalibration() failed,  worstGridMatchTranslate(gridMatchCoeff)= "+worstGridMatchTranslate(gridMatchCoeff)+", hintGridTolerance="+hintGridTolerance);
						return -1;
					}
				}
				if (global_debug_level>0){
					System.out.println((((iGridMatchCoeff[0][2]+iGridMatchCoeff[1][2])&1)==0)?"EVEN shift":"ODD shift");
				}
			} else {
				if (global_debug_level>0) System.out.println("*** Warning: combineGridCalibration(): gridMatchCoeff() failed");
				return -1;
			}
		}
		if (((laserPointer!=null) && (laserPointer.laserUVMap.length>0)) ||
				((iGridTranslateUV!=null) && (gridRotation>=0))){ // no laser pointers, but hint grid with specified tolerance
			if ((global_debug_level>1) && (pointersXY==null)) System.out.println("This image does not contain any laser pointer data");
			acalibrated=calibrateGrid( // now should work without laser pointers too
					laserPointer,
					pointersXY,
					removeOutOfGridPointers,
					gridRotation,
					iGridTranslateUV,
					noMessageBoxes,
					global_debug_level);
			if (global_debug_level>1) {
				System.out.println("matchSimulatedPattern.laserCalibrateGrid() returned "+acalibrated+
						((acalibrated>0)?" laser points used.":(((iGridTranslateUV==null) || (acalibrated<0))?" - error code":"none")));
			}
		}
		if (global_debug_level>0) System.out.println("Pattern size is "+getDArrayWidth()+" x "+ getDArrayHeight());
		return acalibrated;
7554

7555
	}
7556

7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646
// Modified 06/19 to move laser pointers to files
	public int combineGridCalibration(
			LaserPointer lp, // Only for possible hint on rotations/ flips.LaserPointer object or null
			double [][] pointersXYUV,
			boolean removeOutOfGridPointers, //
			double [][][] hintGrid, // predicted grid array (or null)
			double        hintGridTolerance, // alllowed mismatch (fraction of period) or 0 - orientation only
			int global_debug_level, // DEBUG_LEVEL
			boolean noMessageBoxes
			){
		boolean has_lasers = false;
		if (pointersXYUV != null) {
			for (double[] e:pointersXYUV) if ((e != null) && (e.length>2)) {
				has_lasers = true;
				break;
			}
		}
		int acalibrated=0;
		double [][] gridMatchCoeff=null;
		double searchAround=20.0; // how far to look for the grid node
		int gridRotation=-1; //undefined
		int [] iGridTranslateUV=null; // translate UV grid by these integer numbers
		if (hintGrid!=null){
			gridMatchCoeff=calcGridMatchMatrix (hintGrid, searchAround);
			// now already rounds rotate and re-replaces gridMatchCoeff with approximated, refines gridMatchCoeff[0][2] and gridMatchCoeff[1][2]
			if (gridMatchCoeff!=null) {
				gridRotation=matrixToRot(gridMatchCoeff);
				this.debugLevel=global_debug_level;
				int [][] iGridMatchCoeff=gridMatrixApproximate(gridMatchCoeff);
				if (global_debug_level>1){
					System.out.println("gridMatchCoeff[0]={"+IJ.d2s(gridMatchCoeff[0][0],5)+", "+IJ.d2s(gridMatchCoeff[0][1],5)+", "+IJ.d2s(gridMatchCoeff[0][2],5)+"}");
					System.out.println("gridMatchCoeff[1]={"+IJ.d2s(gridMatchCoeff[1][0],5)+", "+IJ.d2s(gridMatchCoeff[1][1],5)+", "+IJ.d2s(gridMatchCoeff[1][2],5)+"}");
					System.out.println("gridRotation="+gridRotation);
					System.out.println("iGridMatchCoeff[0]={"+iGridMatchCoeff[0][0]+", "+iGridMatchCoeff[0][1]+", "+iGridMatchCoeff[0][2]+"}");
					System.out.println("iGridMatchCoeff[1]={"+iGridMatchCoeff[1][0]+", "+iGridMatchCoeff[1][1]+", "+iGridMatchCoeff[1][2]+"}");
					System.out.println("worstGridMatchRotSkew()="+IJ.d2s(worstGridMatchRotSkew(gridMatchCoeff),5));
					System.out.println("worstGridMatchTranslate()="+IJ.d2s(worstGridMatchTranslate(gridMatchCoeff),5));
				}
				// hintGridTolerance==0 - do not try to determine shift from the hint (not reliable yet)
				if (hintGridTolerance>0) {
					if (worstGridMatchTranslate(gridMatchCoeff)<=hintGridTolerance){ // convert to pixels from halfperiods (or just chnage definition of hintGridTolerance)
						if (global_debug_level>1) System.out.println("worstGridMatchTranslate(gridMatchCoeff)= "+worstGridMatchTranslate(gridMatchCoeff)+", hintGridTolerance="+hintGridTolerance);
						iGridTranslateUV=new int[2];
						iGridTranslateUV[0]=iGridMatchCoeff[0][2];
						iGridTranslateUV[1]=iGridMatchCoeff[1][2];
					} else {
						if (global_debug_level>1) System.out.println("*** Warning: combineGridCalibration() failed,  worstGridMatchTranslate(gridMatchCoeff)= "+worstGridMatchTranslate(gridMatchCoeff)+", hintGridTolerance="+hintGridTolerance);
						return -1;
					}
				}
				if (global_debug_level>0){
					System.out.println((((iGridMatchCoeff[0][2]+iGridMatchCoeff[1][2])&1)==0)?"EVEN shift":"ODD shift");
				}
			} else {
				if (global_debug_level>0) System.out.println("*** Warning: combineGridCalibration(): gridMatchCoeff() failed");
				return -1;
			}
		}
		if (has_lasers ||
				((iGridTranslateUV!=null) && (gridRotation>=0))){ // no laser pointers, but hint grid with specified tolerance
			if ((global_debug_level>1) && (pointersXYUV==null)) System.out.println("This image does not contain any laser pointer data");
			double maxOffsetFromCenter=0.6; // maximal offset of the laser spot from the center, relative to cell radius
			boolean white_only = true;
			if (lp != null) {
				maxOffsetFromCenter = lp.maxOffsetFromCenter;
				white_only = lp.whiteOnly;
			}
			acalibrated=calibrateGrid( // now should work without laser pointers too
					lp, // for flips hint only
					white_only,
					maxOffsetFromCenter,
					pointersXYUV,
					removeOutOfGridPointers,
					gridRotation,
					iGridTranslateUV,
					noMessageBoxes,
					global_debug_level);
			if (global_debug_level>1) {
				System.out.println("matchSimulatedPattern.laserCalibrateGrid() returned "+acalibrated+
						((acalibrated>0)?" laser points used.":(((iGridTranslateUV==null) || (acalibrated<0))?" - error code":"none")));
			}
		}
		if (global_debug_level>0) System.out.println("Pattern size is "+getDArrayWidth()+" x "+ getDArrayHeight());
		return acalibrated;

	}




7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702
	public int replaceGridXYWithProjected(double [][][] projectedGrid, String debugTitle){
		int minU=0,minV=0,maxU=0,maxV=0;
		boolean notYetSet=true;
		for (double [][]row:projectedGrid) for (double [] cell:row) if (cell!=null){
			int u = (int) cell[2];
			int v = (int) cell[3];
			if (notYetSet){
				minU=u;
				maxU=u;
				minV=v;
				maxV=v;
				notYetSet=false;
			} else {
				if (minU>u) minU=u;
				if (maxU<u) maxU=u;
				if (minV>v) minV=v;
				if (maxV<v) maxV=v;
			}
		}
		double [][][] grid=new double [maxV-minV+1][maxU-minU+1][];
		//        	   for (double [][]row:grid) for (double [] cell:row) cell=null; // See if this works with "enhanced for loop"
		for (double [][]row:grid) for (int u=0;u<row.length;u++) row[u]=null; // See if this works with "enhanced for loop"

		for (double [][] row:projectedGrid) for (double [] cell:row) if (cell!=null){
			int u = (int) cell[2];
			int v = (int) cell[3];
			double [] xy={cell[0],cell[1]};
			grid[v-minV][u-minU]=xy;
		}
		int numNewDefined=0;
		//        	   System.out.println("this.PATTERN_GRID.length="+this.PATTERN_GRID.length+"this.PATTERN_GRID[0.length="+this.PATTERN_GRID[0].length);
		//        	   System.out.println("this.targetUV.length="+this.targetUV.length+"this.targetUV[0.length="+this.targetUV[0].length);
		if ((debugTitle!=null) && (this.debugLevel>0)){
			double [][] debugReplace=null;
			String [] debugTiltes={"deltaX","deltaY","Contrast", "measX","measY", "targetU","targetV"};
			debugReplace=new double[7][this.PATTERN_GRID.length*this.PATTERN_GRID[0].length];
			for (int i=0;i<debugReplace.length;i++) Arrays.fill(debugReplace[i],Double.NaN);
			for (int v=0;v<this.PATTERN_GRID.length;v++) for (int u=0;u<this.PATTERN_GRID[v].length;u++) {
				double [][] cell=this.PATTERN_GRID[v][u];
				if ((cell !=null) && (cell.length>0) &&(cell[0] !=null) && (cell[0].length>1)){
					int tu=this.targetUV[v][u][0]-minU;
					int tv=this.targetUV[v][u][1]-minV;
					if ((tu>=0) && (tv>=0) && (tv<grid.length) && (tu<grid[tv].length) && (grid[tv][tu]!=null)) {
						int index=v*this.PATTERN_GRID[0].length+u;
						debugReplace[0][index]=grid[tv][tu][0]-this.PATTERN_GRID[v][u][0][0];
						debugReplace[1][index]=grid[tv][tu][1]-this.PATTERN_GRID[v][u][0][1];
						debugReplace[2][index]=this.PATTERN_GRID[v][u][0][2];
						debugReplace[3][index]=this.PATTERN_GRID[v][u][0][0];
						debugReplace[4][index]=this.PATTERN_GRID[v][u][0][1];
						debugReplace[5][index]=tu;
						debugReplace[6][index]=tu;
					}
				}
			}
			SDFA_INSTANCE.showArrays(debugReplace, this.PATTERN_GRID[0].length, this.PATTERN_GRID.length, true, "replaceGridXYWithProjected-"+debugTitle, debugTiltes);
		}
7703

7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725
		for (int v=0;v<this.PATTERN_GRID.length;v++) for (int u=0;u<this.PATTERN_GRID[v].length;u++) {
			double [][] cell=this.PATTERN_GRID[v][u];
			if ((cell !=null) && (cell.length>0) &&(cell[0] !=null) && (cell[0].length>1)){
				//                	   System.out.print("v="+v+" u="+u);
				int tu=this.targetUV[v][u][0]-minU;
				int tv=this.targetUV[v][u][1]-minV;
				//                	   System.out.println("  tv="+tv+" tu="+tu);
				if ((tu>=0) && (tv>=0) && (tv<grid.length) && (tu<grid[tv].length) && (grid[tv][tu]!=null)) {
					cell[0][0]=grid[tv][tu][0]; // -81 -.-1
					cell[0][1]=grid[tv][tu][1];
					if (Double.isNaN(cell[0][0]) || Double.isNaN(cell[0][1])){
						this.PATTERN_GRID[v][u]=null; // make it undefined
					} else {
						numNewDefined++;
					}
				} else {
					this.PATTERN_GRID[v][u]=null; // make it undefined
				}
			}
		}
		return numNewDefined;
	}
7726

7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760
	/* Get calibrated pattern as a 8-slice image (can be saved as TIFF)
	 * first   slice - pixel X or -1 for undefined
	 * second  slice - pixel Y or -1 for undefined
	 * third   slice - target U (may be negative)
	 * fourth  slice - target V (may be negative)
	 *  other slices - if present
	 * fifth   slice -  local grid contrast (looks for 2  white and 2 blacks around) - can be used to filter
	 * sixth   slice - red intensity of the grid (averaged around the grid node)
	 * seventh slice - green intensity of the grid (averaged around the grid node)
	 * eighth  slice - blue intensity of the grid (averaged around the grid node)
	 */
	public ImagePlus getCalibratedPatternAsImage(String title, int numUsedPointers){
		if ((this.targetUV==null) ||(this.pXYUV==null)) {
//			System.out.println("getCalibratedPatternAsImage(): this.targetUV="+((this.targetUV==null)?"null":"not null")+", this.pixelsUV="+((this.pXYUV==null)?"null":"not null"));
//			System.out.println("Using grid w/o absolute calibration.");
			unCalibrateGrid();
			//        		   return null;
		}
		int numSlices=(this.gridContrastBrightness==null)?4:8;
		float [][] pixels=new float [numSlices][getWidth()*getHeight()];
		ImageStack stack=new ImageStack(getWidth(),getHeight());
		int index=0;
		for (int v=0;v<getHeight();v++) for (int u=0;u<getWidth();u++) {
			if ((this.targetUV[v][u]==null) ||(this.pXYUV[v][u]==null)||
					(this.pXYUV[v][u][0]<0.0) || (this.pXYUV[v][u][1]<0.0)) { // disregard negative sensor pixels
				pixels [0][index]=-1.0f;
				pixels [1][index]=-1.0f;
				pixels [2][index]= 0.0f;
				pixels [3][index]= 0.0f;
				if (numSlices>4){
					pixels [4][index]= 0.0f; // contrast
					pixels [5][index]=-1.0f; // red, undefined
					pixels [6][index]=-1.0f; // green, undefined
					pixels [7][index]=-1.0f; // blue, undefined
7761

7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785
				}
			} else {
				pixels [0][index]=(float) this.pXYUV[v][u][0];
				pixels [1][index]=(float) this.pXYUV[v][u][1];
				pixels [2][index]=this.targetUV[v][u][0];
				pixels [3][index]=this.targetUV[v][u][1];
				if (numSlices>4){
					pixels [4][index]=(float) this.gridContrastBrightness[0][v][u]; // grid contrast
					pixels [5][index]=(float) this.gridContrastBrightness[1][v][u]; // red
					pixels [6][index]=(float) this.gridContrastBrightness[2][v][u]; // green
					pixels [7][index]=(float) this.gridContrastBrightness[3][v][u]; // blue
				}
			}
			index++;
		}
		stack.addSlice("pixel-X",  pixels[0]);
		stack.addSlice("pixel-Y",  pixels[1]);
		stack.addSlice("target-U", pixels[2]);
		stack.addSlice("target-V", pixels[3]);
		if (numSlices>4){
			stack.addSlice("contrast",  pixels[4]);
			stack.addSlice("red",       pixels[5]);
			stack.addSlice("green",     pixels[6]);
			stack.addSlice("blue",      pixels[7]);
7786

7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858
		}
		ImagePlus imp = new ImagePlus(title, stack);
		imp.setProperty("USED_POINTERS",((numUsedPointers>=0)?numUsedPointers:0)+"");
		//				System.out.println("getCalibratedPatternAsImage(): numUsedPointers="+numUsedPointers+" getProperty(\"USED_POINTERS\")="+imp.getProperty("USED_POINTERS"));
		return imp;
	}
	// searching for single-pixel errors (program bug)
	public ImagePlus getCalibratedPatternCurvatureAsImage(String title){
		if ((this.targetUV==null) ||(this.pXYUV==null)) {
			String msg="this.targetUV="+((this.targetUV==null)?"null":"not null")+", this.pixelsUV="+((this.pXYUV==null)?"null":"not null");
			IJ.showMessage("Error",msg);
			throw new IllegalArgumentException (msg);
		}
		double [][][] curves=     new double [this.targetUV.length][this.targetUV[0].length][2];
		double [][][] diff_curves=new double [this.targetUV.length][this.targetUV[0].length][2];
		boolean [][]   mask_curves=new boolean [this.targetUV.length][this.targetUV[0].length];
		boolean [][]   mask_diff_curves=new boolean [this.targetUV.length][this.targetUV[0].length];
		for (int v=0;v<curves.length;v++) for (int u=0;u<curves[0].length;u++){
			curves[v][u][0]=0.0;
			curves[v][u][1]=0.0;
			diff_curves[v][u][0]=0.0;
			diff_curves[v][u][1]=0.0;
			mask_curves[v][u]=false;
			mask_diff_curves[v][u]=false;
		}
		ImageStack stack=new ImageStack(getWidth(),getHeight());
		int [][] dirs=   {{0,0},{-1,0},{0,-1},{1,0},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
		double [] weights={1.0, -0.15,- 0.15, -.15, -.15,   -0.1,  -0.1,  -0.1, -0.1};
		// first pass - calculate "curvature" - difference between pixel dx, dy values and those average (weighted) of 8 neigbors
		for (int v=1;v<getHeight()-1;v++) for (int u=1;u<getWidth()-1;u++) {
			double [] avrg={0.0,0.0};
			boolean valid=true;
			for (int d=0;d<dirs.length;d++){
				int u1=u+dirs[d][0];
				int v1=v+dirs[d][1];
				if ((this.targetUV[v1][u1]==null) ||(this.pXYUV[v1][u1]==null)||
						(this.pXYUV[v1][u1][0]<0.0) || (this.pXYUV[v1][u1][0]<0.0)) { // disregard negative sensor pixels
					valid=false;
					break;
				} else {
					avrg[0]+=weights[d]*this.pXYUV[v1][u1][0];
					avrg[1]+=weights[d]*this.pXYUV[v1][u1][1];
				}
			}
			if (valid) {
				curves[v][u][0]=avrg[0];
				curves[v][u][1]=avrg[1];
				mask_curves[v][u]=true;
			}
		}
		// second pass - calculate difference between curvatives of each node and and those average (weighted) of 8 neigbors
		// This will mostly eliminate the distortion shift, and can be used as a measure of the "noise"
		for (int v=2;v<getHeight()-2;v++) for (int u=2;u<getWidth()-2;u++) {
			double [] avrg={0.0,0.0};
			boolean valid=true;
			for (int d=0;d<dirs.length;d++){
				int u1=u+dirs[d][0];
				int v1=v+dirs[d][1];
				if (!mask_curves[v1][u1]) {
					valid=false;
					break;
				} else {
					avrg[0]+=weights[d]*curves[v1][u1][0];
					avrg[1]+=weights[d]*curves[v1][u1][1];
				}
			}
			if (valid) {
				diff_curves[v][u][0]=avrg[0];
				diff_curves[v][u][1]=avrg[1];
				mask_diff_curves[v][u]=true;
			}
		}
7859

7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888
		float [][] pixels=new float [7][getWidth()*getHeight()];
		int numPix=0;
		double sum=0.0;
		int index=0;
		double curvature, diff_curvature;
		for (int v=0;v<getHeight();v++) for (int u=0;u<getWidth();u++) {
			if (mask_curves[v][u]){
				curvature=Math.sqrt(curves[v][u][0]*curves[v][u][0]+curves[v][u][1]*curves[v][u][1]);
				pixels[0][index]=(float) curvature;
				pixels[1][index]=(float) curves[v][u][0];
				pixels[2][index]=(float) curves[v][u][1];
				if (mask_diff_curves[v][u]){
					diff_curvature=Math.sqrt(diff_curves[v][u][0]*diff_curves[v][u][0]+diff_curves[v][u][1]*diff_curves[v][u][1]);
					pixels[3][index]=(float) diff_curvature;
					pixels[4][index]=(float) diff_curves[v][u][0];
					pixels[5][index]=(float) diff_curves[v][u][1];
					pixels[6][index]=1.0f;
					sum+=diff_curvature*diff_curvature;
					numPix++;
				} else {
					pixels[3][index]=0.0f;
					pixels[4][index]=0.0f;
					pixels[5][index]=0.0f;
					pixels[6][index]=0.0f;
				}
			} else {
				pixels[0][index]=0.0f;
				pixels[1][index]=0.0f;
				pixels[2][index]=0.0f;
7889

7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022
			}
			index++;
		}
		stack.addSlice("curvature",  pixels[0]);
		stack.addSlice("X-diff",     pixels[1]);
		stack.addSlice("Y-diff",     pixels[2]);
		stack.addSlice("error",      pixels[3]);
		stack.addSlice("X-error",    pixels[4]);
		stack.addSlice("Y-error",    pixels[5]);
		stack.addSlice("mask",       pixels[6]);
		if (numPix>0){
			double rms=Math.sqrt(sum/numPix);
			String msg="Deviation calculated for "+numPix+" grid nodes. RMS="+rms;
			System.out.println(msg);
			IJ.showMessage(msg);
		} else {
			String msg="Zero points to calculate deviation";
			System.out.println(msg);
			IJ.showMessage(msg);

		}
		ImagePlus imp = new ImagePlus(title, stack);
		return imp;
	}
	public ImagePlus getCalibratedPatternAsImage(
			ImagePlus imp_src,
			String prefix, int numUsedPointers){
		//        	   ImagePlus imp_result=getCalibratedPatternAsImage("grid-"+imp_src.getTitle(), numUsedPointers);
		ImagePlus imp_result=getCalibratedPatternAsImage(prefix+imp_src.getTitle(), numUsedPointers);
		if (imp_result == null) {
			System.out.println("getCalibratedPatternAsImage(): Grid is empty !");
			return null;
		}

		// copy all the properties to the new image
		JP46_Reader_camera jp4_instance= new JP46_Reader_camera(false);
		jp4_instance.copyProperties (imp_src,imp_result);
		jp4_instance.encodeProperiesToInfo(imp_result);
		return imp_result;
	}

	public boolean [] rotToFlips(int rot){
		boolean[][] rot2flips={      // swapUV,flipU,flipV for different rotations above
				{false,false,false},
				{true, true, false},
				{false,true, true },
				{true, false,true },
				{false,false,true },
				{true, false,false},
				{false,true, false},
				{true, true, true }};
		return rot2flips[rot];
	}
	public int flipsToRot(boolean swapUV, boolean flipU, boolean flipV) {
		for (int i=0;i<8;i++) if (
				(rotToFlips(i)[0]==swapUV) &&
				(rotToFlips(i)[1]==flipU) &&
				(rotToFlips(i)[2]==flipV)) return i;
		return -1; // never
	}
	//
	// move elsewhere?
	/**
	 * Create this.targetUV and this.pixelsUV for the grid that does not have any laser pointer references
	 */
	public void unCalibrateGrid(){
		// calculate targetUV that maps PATTERN_GRID cells to the target (absolute) UV
		this.targetUV=new int    [this.PATTERN_GRID.length][this.PATTERN_GRID[0].length][];
		this.pXYUV=new double [this.PATTERN_GRID.length][this.PATTERN_GRID[0].length][];
		// Or set it back to original 9do not touch, rotate/shift in the end?
		Arrays.fill(this.UVShiftRot, 0);
		for (int v=0;v<this.PATTERN_GRID.length;v++) for (int u=0;u<this.PATTERN_GRID[v].length;u++){
			if ((this.PATTERN_GRID[v][u]==null) || (this.PATTERN_GRID[v][u][0]==null)) {
				this.targetUV[v][u]=null;
				this.pXYUV[v][u]=null;
			} else {
				this.targetUV[v][u]=new int [2];
				this.targetUV[v][u][0]=u;
				this.targetUV[v][u][1]=v;
				this.pXYUV[v][u]=new double [2];
				this.pXYUV[v][u][0]=PATTERN_GRID[v][u][0][0];
				this.pXYUV[v][u][1]=PATTERN_GRID[v][u][0][1];
			}
		}

	}
	// returns -1 - failure, otherwise - number of points used for calibration array (move default orientation to laserPointer paramerters
	public int calibrateGrid(
			LaserPointer laserPointer,
			double [][] xy, // null and zero length OK
			boolean removeOutOfGridPointers,
			int hintRotation, // rotation (0..7) found from hintGrid, -1 - undefined
			int [] hintTranslateUV, // found from hintGrid: translate UV by this vector or null if undefined
			//				   double [][][] hintGrid, // predicted grid array (or null) - use just direction
			//				   double        hintGridTolerance, // alllowed mismatch (fraction of period) or 0 - orientation only
			boolean noMessageBoxes,
			int debugLevel
			)
	{
		if (xy==null) xy=new double[0][];
		invalidateCalibration();
		double [][]uv=uvFromXY(xy,removeOutOfGridPointers?2.0:-1);
		//        	   if (uv==null) return -1;
		int numPointesLeft=0;
		for (int i=0;i<xy.length;i++) if ((xy[i]!=null) && (uv[i]!=null)) numPointesLeft++;
		if (debugLevel>1){
			int numRemoved=0;
			for (int i=0;i<xy.length;i++) if ((xy[i]!=null) && (uv[i]==null)) numRemoved++;
			System.out.println("Removed "+numRemoved+" out-of-grid pointers, "+numPointesLeft+" pointers remain.");
		}
		// Now remove pointers that are not on white cells

		if ((laserPointer!=null) && laserPointer.whiteOnly){
			int numBad=0;
			for (int i=0;i<uv.length;i++) if (uv[i]!=null) {
				// Verify that laser spots are on the white cells (sum of uv is even)
				if ((((int)(Math.floor(uv[i][0])+Math.floor(uv[i][1]))) & 1)!=0){
					String msg="Laser point "+i+" is not on the white pattern cell, and this check is enforced in the configuration";
					System.out.println("Warning:"+msg);
					if (!noMessageBoxes) IJ.showMessage("Warning",msg);
					uv[i]=null;
					numBad++;
					continue;
				}
			}
			if (numBad>0){
				String msg="Removed "+numBad+" pointers on black cells";
				System.out.println("Warning:"+msg);
			}
		}
		// Later some pointers may be removed even if they are used to determine orientation/shift. But that should not lead
		// to white/black confusion
		/*
Andrey Filippov's avatar
Andrey Filippov committed
8023 8024 8025 8026 8027
        	   int [][][] rotations={
        			   {{ 1, 0},{ 0, 1}}, // not mirrored
        			   {{ 0, 1},{-1, 0}},
        			   {{-1, 0},{ 0,-1}},
        			   {{ 0,-1},{ 1, 0}},
8028

Andrey Filippov's avatar
Andrey Filippov committed
8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042
        			   {{ 1, 0},{ 0,-1}}, // mirrored
        			   {{ 0, 1},{ 1, 0}},
        			   {{-1, 0},{ 0, 1}},
        			   {{ 0,-1},{-1, 0}}};
        	   // shifts when rotating around unknown center (make it white)
        	   int [][] dfltShifts={
        			   {0,0},
        			   {0,1},
        			   {0,0},
        			   {1,0},
        			   {0,1},
        			   {0,0},
        			   {1,0},
        			   {0,0}};
8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097
		 */
		boolean [] possibleRotations={true,true,true,true,true,true,true,true};
		// If orientation is hinted, remove all other ones from the list of possible ones
		if (hintRotation>=0){ // defind from the hintGrid
			for (int i=0;i<possibleRotations.length;i++) possibleRotations[i]=(i==hintRotation);
		}
		//       	   boolean [] partialPossibleRotations=new boolean [possibleRotations.length];
		boolean pairMatch,allMatch;
		int [] diffUVTable=new int [2]; // difference between points specified in the table
		int [] diffUVMeas= new int [2]; // measured difference (PATTERN_GRID U,V
		int [] rotUVTable=  new int [2]; // rotated 'laser' coordinates difference (should match measured)
		int [] belongsToGoodPair=new int[uv.length];
		int [] belongsToBadPair=new int[uv.length];
		for (int i=0;i<uv.length;i++){
			belongsToGoodPair[i]=0;
			belongsToBadPair[i]=0;
		}
		// pass 0 - process good/bad pairs, do not disable directions if does not match
		// if at least 1 good pair exists - remove all that do not match
		// if no good pairs - remove all bad
		// second pass: if more than 1 good pair - should match all (or error)


		//TODO: When hinted position, remove far pointers before matching pairs

		for (int pass=0;pass<2;pass++) {
			for (int i=0;i<uv.length;i++) if (uv[i]!=null) for (int j=i+1;j<uv.length;j++) if (uv[j]!=null) {
				pairMatch=false;
				allMatch=false;
				diffUVTable[0]=(int) Math.round(laserPointer.laserUVMap[j][0]-laserPointer.laserUVMap[i][0]); // should not get here if uv is {}
				diffUVTable[1]=(int) Math.round(laserPointer.laserUVMap[j][1]-laserPointer.laserUVMap[i][1]);
				diffUVMeas[0]= (int) Math.round(uv[j][0]-uv[i][0]);
				diffUVMeas[1]= (int) Math.round(uv[j][1]-uv[i][1]);
				// see which rotations are possible for this pair of points
				if (debugLevel>2){
					System.out.println("pass="+pass+" i="+i+" j="+j);
					System.out.println("diffUVTable=["+diffUVTable[0]+","+diffUVTable[1]+"]");
					System.out.println("diffUVMeas= ["+diffUVMeas[0]+ ","+diffUVMeas[1]+"]");
				}
				for (int dir=0; dir<rotations.length;dir++) {
					rotUVTable[0]=rotations[dir][0][0]*diffUVTable[0]+rotations[dir][0][1]*diffUVTable[1];
					rotUVTable[1]=rotations[dir][1][0]*diffUVTable[0]+rotations[dir][1][1]*diffUVTable[1];
					if (debugLevel>2){
						System.out.println(dir+": rotUVTable= ["+rotUVTable[0]+ ","+rotUVTable[1]+"]");
					}
					if ((rotUVTable[0]==diffUVMeas[0]) && (rotUVTable[1]==diffUVMeas[1])) {
						pairMatch=true;
						if (possibleRotations[dir]) allMatch=true; // this and hinted direction
					} else {
						if (pass>0){ // do not disable rotation on the first pass
							possibleRotations[dir]=false;
						}
					}
				}
				// TODO: Find maximal number of matching pointers?
8098

8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235
				if (pass==0) {
					//        				   if (allMatch) {
					if (pairMatch) {
						belongsToGoodPair[i]++;
						belongsToGoodPair[j]++;
					} else {
						belongsToBadPair[i]++;
						belongsToBadPair[j]++;
					}
				} else { // second pass
					if (!pairMatch) {
						String msg="Laser points\n"+i+" ["+IJ.d2s(laserPointer.laserUVMap[i][0],2)+":"+IJ.d2s(laserPointer.laserUVMap[i][1],2)+
								"] -> ["+IJ.d2s(uv[i][0],2)+":"+IJ.d2s(uv[i][1],2)+"] and \n"+
								j +" ["+IJ.d2s(laserPointer.laserUVMap[j][0],2)+":"+IJ.d2s(laserPointer.laserUVMap[j][1],2)+
								"] -> ["+IJ.d2s(uv[j][0],2)+":"+IJ.d2s(uv[j][1],2)+"] do not match";
						System.out.println(msg);
						if (!noMessageBoxes) IJ.showMessage("Error",msg);
						unCalibrateGrid();
						return -2;
					} else if (!allMatch){
						String msg="Following laser pointers can not be mapped simultaneously:\n";
						for (int k=0;k<=j;k++) if (uv[k]!=null) {
							msg+=k+" ["+IJ.d2s(laserPointer.laserUVMap[k][0],2)+":"+IJ.d2s(laserPointer.laserUVMap[k][1],2)+
									"] -> ["+IJ.d2s(uv[k][0],2)+":"+IJ.d2s(uv[k][1],2)+"]\n";
						}
						System.out.println(msg);
						if (!noMessageBoxes) IJ.showMessage("Error",msg);
						unCalibrateGrid();
						return -1;
					}
				}
			}
			if (pass==0){
				int numInGood=0;
				int numInBad=0;
				for (int i=0;i<uv.length;i++){
					if (belongsToGoodPair[i]>0) numInGood++;
					if (belongsToBadPair[i]>0) numInBad++;
				}
				if (numInBad>0){
					if (numInGood==0){
						String msg="No matching laser points pairs exist, and "+numInBad+" points do not match";
						System.out.println(msg);
						if (!noMessageBoxes) IJ.showMessage("Error",msg);
						/// will report error on the second pass
					} else if (numInBad>0){
						String msg="Matching laser points pair(s) exist(s), but other:";
						for (int i=0;i<uv.length;i++) if ((belongsToBadPair[i]>0) && (belongsToGoodPair[i]==0)){
							msg+=" #"+i+" ("+(i+1)+" of "+uv.length+")";
							uv[i]=null; // remove it from consideration
						}
						msg+=" do not match and will be removed.";
						System.out.println(msg);
						if (!noMessageBoxes) IJ.showMessage("Error",msg);
					}
				}
			}
		}
		//TODO: here at least some rotations match all points. If there ere more than two - try to use closest to the default/previous
		int rotation=(laserPointer!=null)?(flipsToRot(laserPointer.swapUV,laserPointer.flipU,laserPointer.flipV)):0;
		if (!possibleRotations[rotation]) { // current rotation value defined by laserPointer.{swapUV,flipU,flipV} does not match
			// find a new one (first - without mirroring)
			for (int i=0; i<8;i++) if (possibleRotations[(((rotation ^ i) & 4)) | ((rotation+i) & 3)]) {
				rotation=(((rotation ^ i) & 4)) | ((rotation+i) & 3); // first tried in the same half, then - the next one
				break;
			}
			if (!possibleRotations[rotation]) { // Program bug - should not happen
				String msg="Program error - could not find laser point mapping while it should exist\n";
				System.out.println(msg);
				if (!noMessageBoxes) IJ.showMessage("Error",msg);
				unCalibrateGrid();
				return -3;
			}
		}
		// now rotation is the correct one, update laserPointer.{swapUV,flipU,flipV};
		if (laserPointer!=null){
			laserPointer.swapUV=rotToFlips(rotation)[0];
			laserPointer.flipU =rotToFlips(rotation)[1];
			laserPointer.flipV =rotToFlips(rotation)[2];
		}
		//calculate shift
		int [] uvShift=dfltShifts[rotation].clone(); //{0,0};
		for (int i=0;i<uv.length;i++) if (uv[i]!=null) { // laserPointer -> uv=={}
			uvShift[0]=(int) Math.round(uv[i][0]-
					(rotations[rotation][0][0]*laserPointer.laserUVMap[i][0]+
							rotations[rotation][0][1]*laserPointer.laserUVMap[i][1]));
			uvShift[1]=(int) Math.round(uv[i][1]-
					(rotations[rotation][1][0]*laserPointer.laserUVMap[i][0]+
							rotations[rotation][1][1]*laserPointer.laserUVMap[i][1]));
			break;
		}

		// Hinted shift will only be used if no laser pointers are available, otherwise - only verify/warn
		if (hintTranslateUV!=null) {
			//        		   if ((uv.length==0) || (numPointesLeft==0)){
			if (numPointesLeft==0){
				uvShift[0]=hintTranslateUV[0];
				uvShift[1]=hintTranslateUV[1];
				if (debugLevel>1){
					System.out.println("No laser pointers available, using hinted translation");
				}
			} else {
				if ((uvShift[0]==hintTranslateUV[0]) && (uvShift[0]==hintTranslateUV[0])){
					if (debugLevel>1){
						System.out.println("Translation from the laser pointers matches the hinted one");
					}
				} else {
					if (debugLevel>1){
						System.out.println("Translation from the laser pointers does not match the hinted one:");
						System.out.println("Hinted: delta U="+hintTranslateUV[0]+", V="+hintTranslateUV[1]);
						System.out.println("Lasers: delta U="+uvShift[0]+", V="+uvShift[1]);
						System.out.println("Trusting lasers");
					}
				}
			}
		}
		// calculate remap array (rotation+translation) from the target UV to the measured grid UV.
		this.reMap=new int[2][3]; // seems it is never used?
		this.reMap[0][0]= rotations[rotation][0][0];
		this.reMap[0][1]= rotations[rotation][0][1];
		this.reMap[0][2]= uvShift[0];
		this.reMap[1][0]= rotations[rotation][1][0];
		this.reMap[1][1]= rotations[rotation][1][1];
		this.reMap[1][2]= uvShift[1];
		// calculate reverse remap array (rotation+translation) from the the measured grid UV to the target UV
		int reRot=(rotation>=4)?rotation:((4-rotation) & 3); // number of reverse mirror-rotation
		//        	   int [] UVRot={uvShift[0],uvShift[1],reRot};
		int [] UVRot={
				-(rotations[reRot][0][0]*uvShift[0] +rotations[reRot][0][1]*uvShift[1]),
				-(rotations[reRot][1][0]*uvShift[0] +rotations[reRot][1][1]*uvShift[1]),
				reRot};
		return  applyUVShiftRot(
				UVRot, // int [] UVShiftRot,
				uv,   // double [][]uv,
				laserPointer,
				noMessageBoxes);
		/*
Andrey Filippov's avatar
Andrey Filippov committed
8236 8237 8238 8239 8240
        	   int [][] reReMap={
        			   {rotations[reRot][0][0],rotations[reRot][0][1],
        		       -(rotations[reRot][0][0]*uvShift[0] +rotations[reRot][0][1]*uvShift[1])},
        		       {rotations[reRot][1][0],rotations[reRot][1][1],
            		       -(rotations[reRot][1][0]*uvShift[0] +rotations[reRot][1][1]*uvShift[1])}};
8241

Andrey Filippov's avatar
Andrey Filippov committed
8242 8243
        	   if (debugLevel>1){
        		   System.out.println("rotation="+rotation+", reMap= [["+this.reMap[0][0]+","+this.reMap[0][1]+","+this.reMap[0][2]+"]["+
8244
        				   +this.reMap[1][0]+","+this.reMap[1][1]+","+this.reMap[1][2]+"]]");
Andrey Filippov's avatar
Andrey Filippov committed
8245
        		   System.out.println("reRot="+   reRot+",  reReMap= [["+reReMap[0][0]+","+reReMap[0][1]+","+reReMap[0][2]+"]["+
8246
        				   +reReMap[1][0]+","+reReMap[1][1]+","+reReMap[1][2]+"]]");
Andrey Filippov's avatar
Andrey Filippov committed
8247 8248 8249
        	   }
// calculate targetUV that maps PATTERN_GRID cells to the target (absolute) UV
        	   this.targetUV=new int    [this.PATTERN_GRID.length][this.PATTERN_GRID[0].length][];
8250
        	   this.pXYUV=new double [this.PATTERN_GRID.length][this.PATTERN_GRID[0].length][];
Andrey Filippov's avatar
Andrey Filippov committed
8251 8252 8253
        	   for (int v=0;v<this.PATTERN_GRID.length;v++) for (int u=0;u<this.PATTERN_GRID[v].length;u++){
        		   if ((this.PATTERN_GRID[v][u]==null) || (this.PATTERN_GRID[v][u][0]==null)) {
        			   this.targetUV[v][u]=null;
8254
        			   this.pXYUV[v][u]=null;
Andrey Filippov's avatar
Andrey Filippov committed
8255 8256 8257 8258 8259 8260 8261
        		   } else {
        			   this.targetUV[v][u]=new int [2];
        			   this.targetUV[v][u][0]=reReMap[0][0]*u+reReMap[0][1]*v+reReMap[0][2];
        			   this.targetUV[v][u][1]=reReMap[1][0]*u+reReMap[1][1]*v+reReMap[1][2];
//        			   System.out.println("v="+v+", u="+u+", PATTERN_GRID.length="+PATTERN_GRID.length+", PATTERN_GRID[v].length="+PATTERN_GRID[v].length);
//        			   System.out.println("this.pixelsUV.length="+this.pixelsUV.length);
//        			   System.out.println("this.pixelsUV["+v+"].length="+this.pixelsUV[v].length);
8262 8263 8264
        			   this.pXYUV[v][u]=new double [2];
        			   this.pXYUV[v][u][0]=PATTERN_GRID[v][u][0][0];
        			   this.pXYUV[v][u][1]=PATTERN_GRID[v][u][0][1];
Andrey Filippov's avatar
Andrey Filippov committed
8265 8266 8267 8268 8269 8270
        		   }
        	   }
        	   int numGood=0;
        	   int numBad=0;
        	   double [] distUV=new double[2];
        	   double dist;
8271
        	   for (int i=0;i<uv.length;i++) if (uv[i]!=null) { //laserPointer == null > uv={}
8272
        		// Verify that laser spots are inside specified distance from the cell centers
Andrey Filippov's avatar
Andrey Filippov committed
8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293
        		   distUV[0]=reReMap[0][0]*uv[i][0]+reReMap[0][1]*uv[i][1]+reReMap[0][2]-laserPointer.laserUVMap[i][0];
        		   distUV[1]=reReMap[1][0]*uv[i][0]+reReMap[1][1]*uv[i][1]+reReMap[1][2]-laserPointer.laserUVMap[i][1];
        		   dist=Math.sqrt(distUV[0]*distUV[0]+distUV[1]*distUV[1]);
            	   if (debugLevel>1){
            		   System.out.println("Laser spot #"+i+", distance from predicted ="+ IJ.d2s(dist,3)+" ("+IJ.d2s(200*dist,3)+
            				   "% of cell radius), du="+IJ.d2s(distUV[0],3)+", dv="+IJ.d2s(distUV[1],3));
            	   }
            	   if ((2*dist)> laserPointer.maxOffsetFromCenter){
        			   String msg="Laser point "+(i+1)+"(of "+uv.length+") is too far from the specified location, and this check is enforced in the configuration\n"+
        			              "measured distance is "+ IJ.d2s(200*dist,1)+"% of the cell radius, specified is "+ IJ.d2s(100*laserPointer.maxOffsetFromCenter,1)+"%";
        			   System.out.println("Warning:"+msg);
        			   if (!noMessageBoxes) IJ.showMessage("Warning",msg);
        			   numBad++;
        			   uv[i]=null;
        			   continue;
            	   }
        		   numGood++;
        	   }
        	   if ((debugLevel>0) && (numBad>0)){
        		   System.out.println("Removed "+numBad+" pointers that are too far from the predicted locations");
        	   }
8294
        	   return numGood;
8295 8296
		 */
	}
8297 8298


8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324
	public int calibrateGrid(
			LaserPointer lp, // only as hint for rotations/flips (may update), null is OK
			boolean white_only, // laser pointer only on white
			double maxOffsetFromCenter,
			double [][] xyuv, // null and zero length OK now combines x,y and laserPointer.laserUVMap u,v
			// each non- null xyuv[] should be either 2 or 4 long
			boolean removeOutOfGridPointers,
			int hintRotation, // rotation (0..7) found from hintGrid, -1 - undefined
			int [] hintTranslateUV, // found from hintGrid: translate UV by this vector or null if undefined
			//				   double [][][] hintGrid, // predicted grid array (or null) - use just direction
			//				   double        hintGridTolerance, // alllowed mismatch (fraction of period) or 0 - orientation only
			boolean noMessageBoxes,
			int debugLevel
			)
	{
		if (xyuv==null) xyuv=new double[0][];
		invalidateCalibration();
		boolean has_lasers = false;
		for (double[] e:xyuv) if ((e != null) && (e.length>2)) {
			has_lasers = true;
			break;
		}
		double [][]uv=uvFromXY(xyuv,removeOutOfGridPointers?2.0:-1);
		//        	   if (uv==null) return -1;
		int numPointesLeft=0;
		for (int i=0;i<xyuv.length;i++) if ((xyuv[i]!=null) && (uv[i]!=null)) numPointesLeft++;
8325
		if (debugLevel>1){
8326 8327 8328
			int numRemoved=0;
			for (int i=0;i<xyuv.length;i++) if ((xyuv[i]!=null) && (uv[i]==null)) numRemoved++;
			System.out.println("Removed "+numRemoved+" out-of-grid pointers, "+numPointesLeft+" pointers remain.");
8329
		}
8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592
		// Now remove pointers that are not on white cells

		if (has_lasers && white_only){
			int numBad=0;
			for (int i=0;i<uv.length;i++) if (uv[i]!=null) {
				// Verify that laser spots are on the white cells (sum of uv is even)
				if ((((int)(Math.floor(uv[i][0])+Math.floor(uv[i][1]))) & 1)!=0){
					String msg="Laser point "+i+" is not on the white pattern cell, and this check is enforced in the configuration";
					System.out.println("Warning:"+msg);
					if (!noMessageBoxes) IJ.showMessage("Warning",msg);
					uv[i]=null;
					numBad++;
					continue;
				}
			}
			if (numBad>0){
				String msg="Removed "+numBad+" pointers on black cells";
				System.out.println("Warning:"+msg);
			}
		}
		// Later some pointers may be removed even if they are used to determine orientation/shift. But that should not lead
		// to white/black confusion
		/*
        	   int [][][] rotations={
        			   {{ 1, 0},{ 0, 1}}, // not mirrored
        			   {{ 0, 1},{-1, 0}},
        			   {{-1, 0},{ 0,-1}},
        			   {{ 0,-1},{ 1, 0}},

        			   {{ 1, 0},{ 0,-1}}, // mirrored
        			   {{ 0, 1},{ 1, 0}},
        			   {{-1, 0},{ 0, 1}},
        			   {{ 0,-1},{-1, 0}}};
        	   // shifts when rotating around unknown center (make it white)
        	   int [][] dfltShifts={
        			   {0,0},
        			   {0,1},
        			   {0,0},
        			   {1,0},
        			   {0,1},
        			   {0,0},
        			   {1,0},
        			   {0,0}};
		 */
		boolean [] possibleRotations={true,true,true,true,true,true,true,true};
		// If orientation is hinted, remove all other ones from the list of possible ones
		if (hintRotation>=0){ // defind from the hintGrid
			for (int i=0;i<possibleRotations.length;i++) possibleRotations[i]=(i==hintRotation);
		}
		//       	   boolean [] partialPossibleRotations=new boolean [possibleRotations.length];
		boolean pairMatch,allMatch;
		int [] diffUVTable=new int [2]; // difference between points specified in the table
		int [] diffUVMeas= new int [2]; // measured difference (PATTERN_GRID U,V
		int [] rotUVTable=  new int [2]; // rotated 'laser' coordinates difference (should match measured)
		int [] belongsToGoodPair=new int[uv.length];
		int [] belongsToBadPair=new int[uv.length];
		for (int i=0;i<uv.length;i++){
			belongsToGoodPair[i]=0;
			belongsToBadPair[i]=0;
		}
		// pass 0 - process good/bad pairs, do not disable directions if does not match
		// if at least 1 good pair exists - remove all that do not match
		// if no good pairs - remove all bad
		// second pass: if more than 1 good pair - should match all (or error)


		//TODO: When hinted position, remove far pointers before matching pairs

		for (int pass=0;pass<2;pass++) {
			for (int i=0;i<uv.length;i++) if (uv[i]!=null) for (int j=i+1;j<uv.length;j++) if (uv[j]!=null) { // xyuv[i] != null, xyuv[j] != null
				pairMatch=false;
				allMatch=false;
				diffUVTable[0]=(int) Math.round(xyuv[j][2]-xyuv[i][2]); // should not get here if uv is {}
				diffUVTable[1]=(int) Math.round(xyuv[j][3]-xyuv[i][3]);
				diffUVMeas[0]= (int) Math.round(uv[j][0]-uv[i][0]);
				diffUVMeas[1]= (int) Math.round(uv[j][1]-uv[i][1]);
				// see which rotations are possible for this pair of points
				if (debugLevel>2){
					System.out.println("pass="+pass+" i="+i+" j="+j);
					System.out.println("diffUVTable=["+diffUVTable[0]+","+diffUVTable[1]+"]");
					System.out.println("diffUVMeas= ["+diffUVMeas[0]+ ","+diffUVMeas[1]+"]");
				}
				for (int dir=0; dir<rotations.length;dir++) {
					rotUVTable[0]=rotations[dir][0][0]*diffUVTable[0]+rotations[dir][0][1]*diffUVTable[1];
					rotUVTable[1]=rotations[dir][1][0]*diffUVTable[0]+rotations[dir][1][1]*diffUVTable[1];
					if (debugLevel>2){
						System.out.println(dir+": rotUVTable= ["+rotUVTable[0]+ ","+rotUVTable[1]+"]");
					}
					if ((rotUVTable[0]==diffUVMeas[0]) && (rotUVTable[1]==diffUVMeas[1])) {
						pairMatch=true;
						if (possibleRotations[dir]) allMatch=true; // this and hinted direction
					} else {
						if (pass>0){ // do not disable rotation on the first pass
							possibleRotations[dir]=false;
						}
					}
				}
				// TODO: Find maximal number of matching pointers?

				if (pass==0) {
					//        				   if (allMatch) {
					if (pairMatch) {
						belongsToGoodPair[i]++;
						belongsToGoodPair[j]++;
					} else {
						belongsToBadPair[i]++;
						belongsToBadPair[j]++;
					}
				} else { // second pass
					if (!pairMatch) {
						String msg="Laser points\n"+i+" ["+IJ.d2s(xyuv[i][2],2)+":"+IJ.d2s(xyuv[i][3],2)+
								"] -> ["+IJ.d2s(uv[i][0],2)+":"+IJ.d2s(uv[i][1],2)+"] and \n"+
								j +" ["+IJ.d2s(xyuv[j][2],2)+":"+IJ.d2s(xyuv[j][3],2)+
								"] -> ["+IJ.d2s(uv[j][0],2)+":"+IJ.d2s(uv[j][1],2)+"] do not match";
						System.out.println(msg);
						if (!noMessageBoxes) IJ.showMessage("Error",msg);
						unCalibrateGrid();
						return -2;
					} else if (!allMatch){
						String msg="Following laser pointers can not be mapped simultaneously:\n";
						for (int k=0;k<=j;k++) if (uv[k]!=null) {
							msg+=k+" ["+IJ.d2s(xyuv[k][2],2)+":"+IJ.d2s(xyuv[k][3],2)+
									"] -> ["+IJ.d2s(uv[k][0],2)+":"+IJ.d2s(uv[k][1],2)+"]\n";
						}
						System.out.println(msg);
						if (!noMessageBoxes) IJ.showMessage("Error",msg);
						unCalibrateGrid();
						return -1;
					}
				}
			}
			if (pass==0){
				int numInGood=0;
				int numInBad=0;
				for (int i=0;i<uv.length;i++){
					if (belongsToGoodPair[i]>0) numInGood++;
					if (belongsToBadPair[i]>0) numInBad++;
				}
				if (numInBad>0){
					if (numInGood==0){
						String msg="No matching laser points pairs exist, and "+numInBad+" points do not match";
						System.out.println(msg);
						if (!noMessageBoxes) IJ.showMessage("Error",msg);
						/// will report error on the second pass
					} else if (numInBad>0){
						String msg="Matching laser points pair(s) exist(s), but other:";
						for (int i=0;i<uv.length;i++) if ((belongsToBadPair[i]>0) && (belongsToGoodPair[i]==0)){
							msg+=" #"+i+" ("+(i+1)+" of "+uv.length+")";
							uv[i]=null; // remove it from consideration
						}
						msg+=" do not match and will be removed.";
						System.out.println(msg);
						if (!noMessageBoxes) IJ.showMessage("Error",msg);
					}
				}
			}
		}
		//TODO: here at least some rotations match all points. If there ere more than two - try to use closest to the default/previous
		int rotation=(lp != null)?(flipsToRot(lp.swapUV, lp.flipU, lp.flipV)):0;
		if (!possibleRotations[rotation]) { // current rotation value defined by laserPointer.{swapUV,flipU,flipV} does not match
			// find a new one (first - without mirroring)
			for (int i=0; i<8;i++) if (possibleRotations[(((rotation ^ i) & 4)) | ((rotation+i) & 3)]) {
				rotation=(((rotation ^ i) & 4)) | ((rotation+i) & 3); // first tried in the same half, then - the next one
				break;
			}
			if (!possibleRotations[rotation]) { // Program bug - should not happen
				String msg="Program error - could not find laser point mapping while it should exist\n";
				System.out.println(msg);
				if (!noMessageBoxes) IJ.showMessage("Error",msg);
				unCalibrateGrid();
				return -3;
			}
		}
		// now rotation is the correct one, update laserPointer.{swapUV,flipU,flipV};
		if (lp != null){
			lp.swapUV=rotToFlips(rotation)[0];
			lp.flipU =rotToFlips(rotation)[1];
			lp.flipV =rotToFlips(rotation)[2];
		}
		//calculate shift
		int [] uvShift=dfltShifts[rotation].clone(); //{0,0};
		for (int i=0;i<uv.length;i++) if (uv[i]!=null) { // laserPointer -> uv=={}
			uvShift[0]=(int) Math.round(uv[i][0]-
					(rotations[rotation][0][0]*xyuv[i][2]+
							rotations[rotation][0][1]*xyuv[i][3]));
			uvShift[1]=(int) Math.round(uv[i][1]-
					(rotations[rotation][1][0]*xyuv[i][2]+
							rotations[rotation][1][1]*xyuv[i][3]));
			break;
		}

		// Hinted shift will only be used if no laser pointers are available, otherwise - only verify/warn
		if (hintTranslateUV!=null) {
			//        		   if ((uv.length==0) || (numPointesLeft==0)){
			if (numPointesLeft==0){
				uvShift[0]=hintTranslateUV[0];
				uvShift[1]=hintTranslateUV[1];
				if (debugLevel>1){
					System.out.println("No laser pointers available, using hinted translation");
				}
			} else {
				if ((uvShift[0]==hintTranslateUV[0]) && (uvShift[0]==hintTranslateUV[0])){
					if (debugLevel>1){
						System.out.println("Translation from the laser pointers matches the hinted one");
					}
				} else {
					if (debugLevel>1){
						System.out.println("Translation from the laser pointers does not match the hinted one:");
						System.out.println("Hinted: delta U="+hintTranslateUV[0]+", V="+hintTranslateUV[1]);
						System.out.println("Lasers: delta U="+uvShift[0]+", V="+uvShift[1]);
						System.out.println("Trusting lasers");
					}
				}
			}
		}
		// calculate remap array (rotation+translation) from the target UV to the measured grid UV.
		this.reMap=new int[2][3]; // seems it is never used?
		this.reMap[0][0]= rotations[rotation][0][0];
		this.reMap[0][1]= rotations[rotation][0][1];
		this.reMap[0][2]= uvShift[0];
		this.reMap[1][0]= rotations[rotation][1][0];
		this.reMap[1][1]= rotations[rotation][1][1];
		this.reMap[1][2]= uvShift[1];
		// calculate reverse remap array (rotation+translation) from the the measured grid UV to the target UV
		int reRot=(rotation>=4)?rotation:((4-rotation) & 3); // number of reverse mirror-rotation
		//        	   int [] UVRot={uvShift[0],uvShift[1],reRot};
		int [] UVRot={
				-(rotations[reRot][0][0]*uvShift[0] +rotations[reRot][0][1]*uvShift[1]),
				-(rotations[reRot][1][0]*uvShift[0] +rotations[reRot][1][1]*uvShift[1]),
				reRot};
//		return  applyUVShiftRot(
//				UVRot, // int [] UVShiftRot,
//				uv,   // double [][]uv,
//				laserPointer,
//				noMessageBoxes);
		return  applyUVShiftRot(
				UVRot, // int [] UVShiftRot,
				uv, // double [][] uv,
				xyuv, // double [][] xyuv, // [][2], [][3] contain laser pointers u,v
				maxOffsetFromCenter, // double maxOffsetFromCenter,
//				LaserPointer laserPointer,
				noMessageBoxes);
	}

	public int applyUVShiftRot(
			int [] UVShiftRot,
			double [][]uv,
			LaserPointer laserPointer,
			boolean noMessageBoxes
			){
		if (UVShiftRot!=null) this.UVShiftRot=UVShiftRot.clone();
		int [][] reReMap=getRemapMatrix(UVShiftRot);
		if (debugLevel>1){
			//        		   System.out.println("rotation="+rotation+", reMap= [["+this.reMap[0][0]+","+this.reMap[0][1]+","+this.reMap[0][2]+"]["+
			//        				   +this.reMap[1][0]+","+this.reMap[1][1]+","+this.reMap[1][2]+"]]");
			System.out.println("reRot="+   UVShiftRot[2]+",  reReMap= [["+reReMap[0][0]+","+reReMap[0][1]+","+reReMap[0][2]+"]["+
					+reReMap[1][0]+","+reReMap[1][1]+","+reReMap[1][2]+"]]");
		}
		// calculate targetUV that maps PATTERN_GRID cells to the target (absolute) UV
		this.targetUV=new int    [this.PATTERN_GRID.length][this.PATTERN_GRID[0].length][];
		this.pXYUV=new double [this.PATTERN_GRID.length][this.PATTERN_GRID[0].length][];
		for (int v=0;v<this.PATTERN_GRID.length;v++) for (int u=0;u<this.PATTERN_GRID[v].length;u++){
			if ((this.PATTERN_GRID[v][u]==null) || (this.PATTERN_GRID[v][u][0]==null)) {
8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639
				this.targetUV[v][u]=null;
				this.pXYUV[v][u]=null;
			} else {
				this.targetUV[v][u]=new int [2];
				this.targetUV[v][u][0]=reReMap[0][0]*u+reReMap[0][1]*v+reReMap[0][2];
				this.targetUV[v][u][1]=reReMap[1][0]*u+reReMap[1][1]*v+reReMap[1][2];
				//        			   System.out.println("v="+v+", u="+u+", PATTERN_GRID.length="+PATTERN_GRID.length+", PATTERN_GRID[v].length="+PATTERN_GRID[v].length);
				//        			   System.out.println("this.pixelsUV.length="+this.pixelsUV.length);
				//        			   System.out.println("this.pixelsUV["+v+"].length="+this.pixelsUV[v].length);
				this.pXYUV[v][u]=new double [2];
				this.pXYUV[v][u][0]=PATTERN_GRID[v][u][0][0];
				this.pXYUV[v][u][1]=PATTERN_GRID[v][u][0][1];
			}
		}
		int numGood=0;
		int numBad=0;
		double [] distUV=new double[2];
		double dist;
		if (laserPointer!=null) {
			for (int i=0;i<uv.length;i++) if (uv[i]!=null) { //laserPointer == null > uv={}
				// Verify that laser spots are inside specified distance from the cell centers
				distUV[0]=reReMap[0][0]*uv[i][0]+reReMap[0][1]*uv[i][1]+reReMap[0][2]-laserPointer.laserUVMap[i][0];
				distUV[1]=reReMap[1][0]*uv[i][0]+reReMap[1][1]*uv[i][1]+reReMap[1][2]-laserPointer.laserUVMap[i][1];
				dist=Math.sqrt(distUV[0]*distUV[0]+distUV[1]*distUV[1]);
				if (debugLevel>1){
					System.out.println("Laser spot #"+i+", distance from predicted ="+ IJ.d2s(dist,3)+" ("+IJ.d2s(200*dist,3)+
							"% of cell radius), du="+IJ.d2s(distUV[0],3)+", dv="+IJ.d2s(distUV[1],3));
				}
				if ((2*dist)> laserPointer.maxOffsetFromCenter){
					String msg="Laser point "+(i+1)+"(of "+uv.length+") is too far from the specified location, and this check is enforced in the configuration\n"+
							"measured distance is "+ IJ.d2s(200*dist,1)+"% of the cell radius, specified is "+ IJ.d2s(100*laserPointer.maxOffsetFromCenter,1)+"%";
					System.out.println("Warning:"+msg);
					if (!noMessageBoxes) IJ.showMessage("Warning",msg);
					numBad++;
					uv[i]=null;
					continue;
				}
				numGood++;
			}
		}
		if ((debugLevel>0) && (numBad>0)){
			System.out.println("Removed "+numBad+" pointers that are too far from the predicted locations");
		}
		return numGood;
	}


8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716




	public int applyUVShiftRot(
			int [] UVShiftRot,
			double [][] uv,
			double [][] xyuv, // [][2], [][3] contain laser pointers u,v
			double maxOffsetFromCenter,
//			LaserPointer laserPointer,
			boolean noMessageBoxes
			){
		if (UVShiftRot!=null) this.UVShiftRot=UVShiftRot.clone();
		boolean has_lasers = false;
		for (double[] e:xyuv) if ((e != null) && (e.length>2)) {
			has_lasers = true;
			break;
		}
		int [][] reReMap=getRemapMatrix(UVShiftRot);
		if (debugLevel>1){
			//        		   System.out.println("rotation="+rotation+", reMap= [["+this.reMap[0][0]+","+this.reMap[0][1]+","+this.reMap[0][2]+"]["+
			//        				   +this.reMap[1][0]+","+this.reMap[1][1]+","+this.reMap[1][2]+"]]");
			System.out.println("reRot="+   UVShiftRot[2]+",  reReMap= [["+reReMap[0][0]+","+reReMap[0][1]+","+reReMap[0][2]+"]["+
					+reReMap[1][0]+","+reReMap[1][1]+","+reReMap[1][2]+"]]");
		}
		// calculate targetUV that maps PATTERN_GRID cells to the target (absolute) UV
		this.targetUV=new int    [this.PATTERN_GRID.length][this.PATTERN_GRID[0].length][];
		this.pXYUV=new double [this.PATTERN_GRID.length][this.PATTERN_GRID[0].length][];
		for (int v=0;v<this.PATTERN_GRID.length;v++) for (int u=0;u<this.PATTERN_GRID[v].length;u++){
			if ((this.PATTERN_GRID[v][u]==null) || (this.PATTERN_GRID[v][u][0]==null)) {
				this.targetUV[v][u]=null;
				this.pXYUV[v][u]=null;
			} else {
				this.targetUV[v][u]=new int [2];
				this.targetUV[v][u][0]=reReMap[0][0]*u+reReMap[0][1]*v+reReMap[0][2];
				this.targetUV[v][u][1]=reReMap[1][0]*u+reReMap[1][1]*v+reReMap[1][2];
				//        			   System.out.println("v="+v+", u="+u+", PATTERN_GRID.length="+PATTERN_GRID.length+", PATTERN_GRID[v].length="+PATTERN_GRID[v].length);
				//        			   System.out.println("this.pixelsUV.length="+this.pixelsUV.length);
				//        			   System.out.println("this.pixelsUV["+v+"].length="+this.pixelsUV[v].length);
				this.pXYUV[v][u]=new double [2];
				this.pXYUV[v][u][0]=PATTERN_GRID[v][u][0][0];
				this.pXYUV[v][u][1]=PATTERN_GRID[v][u][0][1];
			}
		}
		int numGood=0;
		int numBad=0;
		double [] distUV=new double[2];
		double dist;
		if (has_lasers) {
			for (int i=0;i<uv.length;i++) if (uv[i]!=null) { //laserPointer == null > uv={}
				// Verify that laser spots are inside specified distance from the cell centers
				distUV[0]=reReMap[0][0]*uv[i][0]+reReMap[0][1]*uv[i][1]+reReMap[0][2]-xyuv[i][2];
				distUV[1]=reReMap[1][0]*uv[i][0]+reReMap[1][1]*uv[i][1]+reReMap[1][2]-xyuv[i][3];
				dist=Math.sqrt(distUV[0]*distUV[0]+distUV[1]*distUV[1]);
				if (debugLevel>1){
					System.out.println("Laser spot #"+i+", distance from predicted ="+ IJ.d2s(dist,3)+" ("+IJ.d2s(200*dist,3)+
							"% of cell radius), du="+IJ.d2s(distUV[0],3)+", dv="+IJ.d2s(distUV[1],3));
				}
				if ((2*dist)> maxOffsetFromCenter){
					String msg="Laser point "+(i+1)+"(of "+uv.length+") is too far from the specified location, and this check is enforced in the configuration\n"+
							"measured distance is "+ IJ.d2s(200*dist,1)+"% of the cell radius, specified is "+ IJ.d2s(100*maxOffsetFromCenter,1)+"%";
					System.out.println("Warning:"+msg);
					if (!noMessageBoxes) IJ.showMessage("Warning",msg);
					numBad++;
					uv[i]=null;
					continue;
				}
				numGood++;
			}
		}
		if ((debugLevel>0) && (numBad>0)){
			System.out.println("Removed "+numBad+" pointers that are too far from the predicted locations");
		}
		return numGood;
	}


8717 8718 8719 8720 8721 8722 8723 8724
	/**
	 * Rotate/flip PATTERN_GRID to match expected
	 * @param hintGrid [v][u][0 - pixel X, 1 - pixel Y, 2 - targetU, 3 - targetV
	 * @return true if possible, false - if not
	 */
	/*
           public boolean applyHintToGrid(double [][][] hintGrid){

Andrey Filippov's avatar
Andrey Filippov committed
8725
           }
8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264
	 */
	/**
	 * Calculate grid fractional UV from x,y and grid array
	 * using bi-linear interpolation from the nearest points.
	 * Iterates through all grid points, so it is not optimal
	 * for processing each pixel.
	 */
	public double [][] uvFromXY(
			double [][] xy,
			double maxDist
			){
		if (xy==null) {
			double[][] uv=new double[0][];
			return uv;
		}
		double[][] uv=new double[xy.length][];
		for (int i=0;i<xy.length;i++) {
			uv[i]= uvFromXY(xy[i],maxDist);
			//				   if (uv[i]==null) return null;
		}
		return uv;
	}
	public double [] uvFromXY(
			double [] xy,
			double maxDist
			){
		//			   double [][][][] grid= this.PATTERN_GRID;
		//			   double [] gXY=new double [2];
		//			   int [] iUV=new int [2];
		if (xy==null) return null;
		int [][] iUV=new int[3][2];
		int width=this.PATTERN_GRID[0].length;
		// find closest point to xy
		double dist2,dx,dy,minDist2=-1.0;
		double []dist2Array=new double [this.PATTERN_GRID.length*width];
		for (int v=0;v< this.PATTERN_GRID.length; v++) for (int u=0;u< this.PATTERN_GRID[v].length; u++)
			if ((this.PATTERN_GRID[v][u]!=null) && (this.PATTERN_GRID[v][u][0]!=null)){
				dx=this.PATTERN_GRID[v][u][0][0]-xy[0];
				dy=this.PATTERN_GRID[v][u][0][1]-xy[1];
				dist2=dx*dx+dy*dy;
				dist2Array[v*width+u]=dist2;
				if ((minDist2<0.0) || (minDist2>dist2)) {
					minDist2=dist2;
					iUV[0][0]=u;
					iUV[0][1]=v;
				}
			} else {
				dist2Array[v*width+u]=-1.0;
			}
		// now find two other closest points (not on the same line
		dist2Array[iUV[0][1]*width+iUV[0][0]]=-1.0; // mark used point
		int indx=0;
		minDist2=-1.0;
		for (int i=0; i<dist2Array.length;i++) if ((dist2Array[i]>=0.0) && ((minDist2<0.0) || (minDist2>dist2Array[i]))){
			minDist2=dist2Array[i];
			indx=i;
		}
		iUV[1][0]=indx%width;
		iUV[1][1]=indx/width;
		// mark all points on the same line as iUV[0] and iUV[1]
		// find closest of the remaining points
		indx=0;
		minDist2=-1.0;
		int dU1=iUV[1][0]-iUV[0][0];
		int dV1=iUV[1][1]-iUV[0][1];
		int dU2,dV2;
		for (int i=0; i<dist2Array.length;i++)
			if ((dist2Array[i]>=0.0) && ((minDist2<0.0) || (minDist2>dist2Array[i]))) {
				dU2=i%width-iUV[0][0];
				dV2=i/width-iUV[0][1];
				if (dU1*dV2!=dV1*dU2) {
					minDist2=dist2Array[i];
					indx=i;
				}
			}
		iUV[2][0]=indx%width;
		iUV[2][1]=indx/width;
		// now there are 3 (not co-linear) points to interpolate u,v
		double [][] aMuv={
				{iUV[1][0]-iUV[0][0],iUV[2][0]-iUV[0][0]},
				{iUV[1][1]-iUV[0][1],iUV[2][1]-iUV[0][1]}
		};
		Matrix Muv=new Matrix(aMuv);
		if (
				(this.PATTERN_GRID==null)||
				(this.PATTERN_GRID[iUV[0][1]][iUV[0][0]]==null)||
				(this.PATTERN_GRID[iUV[1][1]][iUV[1][0]]==null)||
				(this.PATTERN_GRID[iUV[2][1]][iUV[2][0]]==null)||
				(this.PATTERN_GRID[iUV[0][1]][iUV[0][0]][0]==null)||
				(this.PATTERN_GRID[iUV[1][1]][iUV[1][0]][0]==null)||
				(this.PATTERN_GRID[iUV[2][1]][iUV[2][0]][0]==null)) return null;
		double [][] aMxy={
				{   this.PATTERN_GRID[iUV[1][1]][iUV[1][0]][0][0]-this.PATTERN_GRID[iUV[0][1]][iUV[0][0]][0][0],
					this.PATTERN_GRID[iUV[2][1]][iUV[2][0]][0][0]-this.PATTERN_GRID[iUV[0][1]][iUV[0][0]][0][0]},
				{   this.PATTERN_GRID[iUV[1][1]][iUV[1][0]][0][1]-this.PATTERN_GRID[iUV[0][1]][iUV[0][0]][0][1],
						this.PATTERN_GRID[iUV[2][1]][iUV[2][0]][0][1]-this.PATTERN_GRID[iUV[0][1]][iUV[0][0]][0][1]}
		};
		Matrix Mxy=new Matrix(aMxy);
		double [][] aVxy={
				{xy[0]-this.PATTERN_GRID[iUV[0][1]][iUV[0][0]][0][0]},
				{xy[1]-this.PATTERN_GRID[iUV[0][1]][iUV[0][0]][0][1]}
		};
		Matrix Vxy=new Matrix(aVxy);
		double [][] aVuv0={
				{iUV[0][0]},
				{iUV[0][1]}
		};
		Matrix Vuv0=new Matrix(aVuv0);
		Matrix Vuv=Vuv0.plus(Muv.times(Mxy.inverse()).times(Vxy));
		double [] result=Vuv.getRowPackedCopy();
		if (this.debugLevel>1) System.out.println("X="+IJ.d2s(xy[0],3)+" Y="+IJ.d2s(xy[1],3));
		if (this.debugLevel>2) System.out.println(" "+
				"Grid["+iUV[0][1]+"]["+iUV[0][0]+"]X="+IJ.d2s(this.PATTERN_GRID[iUV[0][1]][iUV[0][0]][0][0],3)+" "+
				"Grid["+iUV[0][1]+"]["+iUV[0][0]+"]Y="+IJ.d2s(this.PATTERN_GRID[iUV[0][1]][iUV[0][0]][0][1],3)+"\n "+
				"Grid["+iUV[1][1]+"]["+iUV[1][0]+"]X="+IJ.d2s(this.PATTERN_GRID[iUV[1][1]][iUV[1][0]][0][0],3)+" "+
				"Grid["+iUV[1][1]+"]["+iUV[1][0]+"]Y="+IJ.d2s(this.PATTERN_GRID[iUV[1][1]][iUV[1][0]][0][1],3)+"\n "+
				"Grid["+iUV[2][1]+"]["+iUV[2][0]+"]X="+IJ.d2s(this.PATTERN_GRID[iUV[2][1]][iUV[2][0]][0][0],3)+" "+
				"Grid["+iUV[2][1]+"]["+iUV[2][0]+"]Y="+IJ.d2s(this.PATTERN_GRID[iUV[2][1]][iUV[2][0]][0][1],3));
		if (this.debugLevel>1) System.out.println("U="+IJ.d2s(result[0],3)+" V="+IJ.d2s(result[1],3)+"\n");
		minDist2=(result[0]-iUV[0][0])*(result[0]-iUV[0][0])+(result[1]-iUV[0][1])*(result[1]-iUV[0][1]);
		if ((maxDist>0.0) && (minDist2>maxDist*maxDist)) {
			if (this.debugLevel>0) System.out.println("minDist2="+minDist2+" (maxDist="+maxDist+") - pointer too far (x="+xy[0]+" y="+xy[1]+")");
			return null; // pointer too far from the grid (outside of the grid)
		}
		// change test (make sure that all 4 grid points around the result are defined
		int uFloor=(int) Math.floor(result[0]);
		int vFloor=(int) Math.floor(result[1]);
		int extra=(int) Math.round(maxDist)-1;
		if (extra<0) extra=0;
		for (int v=vFloor-extra; v<= vFloor+extra+1;v++)  for (int u=uFloor-extra; u<= uFloor+extra+1;u++)
			if ((v<0) || (u<0) || (v>=this.PATTERN_GRID.length) || (u>=this.PATTERN_GRID[v].length) ||
					(this.PATTERN_GRID[v][u]==null) || (this.PATTERN_GRID[v][u][0]==null)) {
				if (this.debugLevel>1) System.out.println("pointer="+result[0]+":"+result[1]+
						", no grid at "+u+":"+v+" - pointer does not have grid around (x="+xy[0]+" y="+xy[1]+"), extra="+extra+" vFloor="+vFloor+" uFloor="+uFloor);
				for (int iiv=-2;iiv<3;iiv++) {
					for (int iiu=-2;iiu<3;iiu++) {
						boolean iinValid=
								((iiv+vFloor)<0)||
								((iiv+vFloor)>=this.PATTERN_GRID.length) ||
								((iiu+uFloor)<0)||
								((iiu+uFloor)>=this.PATTERN_GRID[0].length) ||
								(this.PATTERN_GRID[iiv+vFloor][iiu+uFloor]==null) ||
								(this.PATTERN_GRID[iiv+vFloor][iiu+uFloor][0]==null);
						if (this.debugLevel>1)  System.out.println((iiu+uFloor)+":"+
								(iiv+vFloor)+ "  "+(iinValid?"---":(IJ.d2s(this.PATTERN_GRID[iiv+vFloor][iiu+uFloor][0][0],1)+":"+IJ.d2s(this.PATTERN_GRID[iiv+vFloor][iiu+uFloor][0][1],1))));
					}
				}
				return null; // pointer too far from the grid (outside of the grid)
			}
		return result;
	}

	/* ======================================================================== */
	/*
    public static MatchSimulatedPattern.LaserPointer LASER_POINTERS= new MatchSimulatedPattern.LaserPointer (

	 */
	//
	/* ======================================================================== */
	private  double [] correctedPatternCrossLocation(
			LwirReaderParameters lwirReaderParameters, // null is OK
			double [] beforeXY, // initial coordinates of the pattern cross point
			double wv0x,
			double wv0y,
			double wv1x,
			double wv1y,
			double [][] correction,
			ImagePlus imp,      // image data (Bayer mosaic)
			DistortionParameters distortionParameters, //
			MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			MatchSimulatedPattern matchSimulatedPattern, // correlationSize
			SimulationPattern.SimulParameters  thisSimulParameters,
			boolean equalizeGreens,
			double [] window,   // window function
			double [] window2,   // window function - twice FFT size (or null)
			double [] window4,   // window function - 4x FFT size (or null)
			SimulationPattern simulationPattern,
			boolean negative, // invert cross phase
			DoubleFHT fht_instance,
			boolean fast, // use fast measuring of the maximum on the correlation
			double [][] locsNeib, // locations and weights of neighbors to average
			int debug_level,
			String dbgStr){
		if (distortionParameters.legacyMode)
			return correctedPatternCrossLocationOld(
					beforeXY, // initial coordinates of the pattern cross point
					wv0x,
					wv0y,
					wv1x,
					wv1y,
					correction,
					imp,      // image data (Bayer mosaic)
					distortionParameters, //
					patternDetectParameters,
					matchSimulatedPattern, // correlationSize
					thisSimulParameters,
					equalizeGreens,
					window,   // window function
					window2,   // window function - twice FFT size (or null)
					window4,   // window function - 4x FFT size (or null)
					simulationPattern,
					negative, // invert cross phase
					fht_instance,
					fast, // use fast measuring of the maximum on the correlation
					locsNeib, // locations and weights of neighbors to average
					debug_level);
		else
			return correctedPatternCrossLocationAverage4(
					lwirReaderParameters, //  LwirReaderParameters lwirReaderParameters, // null is OK
					beforeXY, // initial coordinates of the pattern cross point
					wv0x,
					wv0y,
					wv1x,
					wv1y,
					correction,
					imp,      // image data (Bayer mosaic)
					distortionParameters, //
					patternDetectParameters,
					matchSimulatedPattern, // correlationSize
					thisSimulParameters,
					equalizeGreens,
					window,   // window function
					window2,   // window function - twice FFT size (or null)
					window4,   // window function - 4x FFT size (or null)
					simulationPattern,
					negative, // invert cross phase
					fht_instance,
					fast, // use fast measuring of the maximum on the correlation
					locsNeib, // locations and weights of neighbors to average
					debug_level,
					dbgStr);
	}
	private  double [] correctedPatternCrossLocationOld(
			double [] beforeXY, // initial coordinates of the pattern cross point
			double wv0x,
			double wv0y,
			double wv1x,
			double wv1y,
			double [][] correction,
			ImagePlus imp,      // image data (Bayer mosaic)
			DistortionParameters distortionParameters, //
			MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			MatchSimulatedPattern matchSimulatedPattern, // correlationSize
			SimulationPattern.SimulParameters  thisSimulParameters,
			boolean equalizeGreens,
			double [] window,   // window function
			double [] window2,   // window function - twice FFT size (or null)
			double [] window4,   // window function - 4x FFT size (or null)
			SimulationPattern simulationPattern,
			boolean negative, // invert cross phase
			DoubleFHT fht_instance,
			boolean fast, // use fast measuring of the maximum on the correlation
			double [][] locsNeib, // locations and weights of neighbors to average
			int debug_level){

		// Just for testing

		beforeXY[0]+=distortionParameters.correlationDx;  // offset, X (in pixels)
		beforeXY[1]+=distortionParameters.correlationDy; // offset y (in pixels)


		double [][] convMatrix= {{1.0,-1.0},{1.0,1.0}}; // from greens2 to pixel WV
		double [][] invConvMatrix= matrix2x2_scale(matrix2x2_invert(convMatrix),2.0);

		double [] result=new double [3];
		result[0]=beforeXY[0];
		result[1]=beforeXY[1];
		result[2]=0.0; // contrast


		if (fht_instance==null) fht_instance=new DoubleFHT(); // move upstream to reduce number of initializations


		//create diagonal green selection around ixc,iyc

		double [][]wv={{wv0x, wv0y},
				{wv1x, wv1y}};
		double [][] WVgreens=matrix2x2_mul(wv,invConvMatrix);
		if (debug_level>2) System.out.println("WVgreens[0][0]="+IJ.d2s(WVgreens[0][0],3)+
				" WVgreens[0][1]="+IJ.d2s(WVgreens[0][1],3)+
				" WVgreens[1][0]="+IJ.d2s(WVgreens[1][0],3)+
				" WVgreens[1][1]="+IJ.d2s(WVgreens[1][1],3));
		double [] dUV;
		double[][] sim_pix;
		double [] simGreensCentered;
		double [] modelCorr;
		double [] xyCorr={0.0,0.0};
		double [] centerXY;
		double 	contrast;
		int numNeib;
		double []corr=null;
		double [] neibCenter=new double[2];
		if (correction!=null) { // overwrite wave vectors
			wv[0][0]=correction[0][0];
			wv[0][1]=correction[0][1];
			wv[1][0]=correction[1][0];
			wv[1][1]=correction[1][1];
			if (correction[0].length>3) { // enough data for quadratic approximation
				corr=new double[10];
				corr[0]=correction[0][3]/4;
				corr[1]=correction[0][4]/4;
				corr[2]=correction[0][5]/4;
				corr[3]=correction[1][3]/4;
				corr[4]=correction[1][4]/4;
				corr[5]=correction[1][5]/4;
				corr[6]=0.0;
				corr[7]=0.0;
				corr[9]=0.0;
				corr[9]=0.0;
			}
		}
		double u_span=Math.sqrt(wv0x*wv0x+wv0y*wv0y)*distortionParameters.correlationSize;
		double v_span=Math.sqrt(wv1x*wv1x+wv1y*wv1y)*distortionParameters.correlationSize;
		double min_span=Math.min(u_span, v_span);
		int thisCorrelationSize=distortionParameters.correlationSize;
		double [] thisWindow=window;
		double uv_threshold=distortionParameters.minUVSpan*0.25*Math.sqrt(2.0);

		if (
				(min_span<uv_threshold) &&
				(window2!=null) &&
				(thisCorrelationSize<distortionParameters.maximalCorrelationSize)) { // trying to increase only twice
			thisCorrelationSize*=2;
			min_span*=2;
			thisWindow=window2;
			if (
					(min_span<uv_threshold) &&
					(window4!=null) &&
					(thisCorrelationSize<distortionParameters.maximalCorrelationSize)) {
				thisCorrelationSize*=2;
				min_span*=2;
				thisWindow=window4;
			}
		}
		setCorrelationSizesUsed(thisCorrelationSize);
		//		     if (thisCorrelationSize>distortionParameters.correlationSize) System.out.println("**** u/v span too small, increasing FFT size to "+thisCorrelationSize);
		if ((debug_level>0)&&(thisCorrelationSize>distortionParameters.correlationSize)) System.out.println("**** u/v span too small, increasing FFT size to "+thisCorrelationSize);
		Rectangle centerCross=correlationSelection(
				beforeXY, // initial coordinates of the pattern cross point
				//						distortionParameters.correlationSize);
				thisCorrelationSize);

		int ixc=centerCross.x+centerCross.width/2;
		int iyc=centerCross.y+centerCross.height/2;
		double [] diffBeforeXY={beforeXY[0]-ixc, beforeXY[1]-iyc};
		double[][] input_bayer=splitBayer (imp,centerCross,equalizeGreens);

		if (debug_level>3) SDFA_INSTANCE.showArrays(input_bayer,  true, "centered");
		if (debug_level>2) SDFA_INSTANCE.showArrays(input_bayer[4], "greens");
		if (debug_level>2) System.out.println("ixc="+ixc+" iyc="+iyc);
		double [] greens=normalizeAndWindow (input_bayer[4], thisWindow);

		if (debug_level>2) {
			System.out.println(" wv0x="+IJ.d2s(wv0x,5)+" wv0y="+IJ.d2s(wv0y,5));
			System.out.println(" wv1x="+IJ.d2s(wv1x,5)+" wv1y="+IJ.d2s(wv1y,5));
			System.out.println(" u-span="+IJ.d2s(u_span,3)+"  v-span="+IJ.d2s(v_span,3)+" threshold="+IJ.d2s(uv_threshold,3)+" ("+IJ.d2s(distortionParameters.minUVSpan,3)+")");
			if (corr!=null) {
				System.out.println(" Ax="+IJ.d2s(corr[0],8)+" Bx="+IJ.d2s(corr[1],8)+" Cx="+IJ.d2s(corr[2],8)+" Dx="+IJ.d2s(corr[6],8)+" Ex="+IJ.d2s(corr[7],8));
				System.out.println(" Ay="+IJ.d2s(corr[3],8)+" By="+IJ.d2s(corr[4],8)+" Cy="+IJ.d2s(corr[5],8)+" Dy="+IJ.d2s(corr[8],8)+" Ey="+IJ.d2s(corr[9],8));
			}
		}
		for (numNeib=0;numNeib<locsNeib.length;numNeib++) if (locsNeib[numNeib][2]!=0.0) {
			neibCenter[0]=diffBeforeXY[0]+locsNeib[numNeib][0];
			neibCenter[1]=diffBeforeXY[1]+locsNeib[numNeib][1];
			//			 dUV=matrix2x2_scale(matrix2x2_mul(wv,diffBeforeXY),-2*Math.PI);

			dUV=matrix2x2_scale(matrix2x2_mul(wv,neibCenter),-2*Math.PI);
			simulationPattern.simulatePatternFullPattern( // not thread safe
					wv0x,
					wv0y,
					dUV[0]+(negative?(-Math.PI/2):Math.PI/2), // negative?(-Math.PI/2):Math.PI/2,
					wv1x,
					wv1y,
					dUV[1]+Math.PI/2, //Math.PI/2,
					corr, //null, // no mesh distortion here
					thisSimulParameters.subdiv,// SIMUL.subdiv, - do not need high quality here
					thisCorrelationSize,
					true,// center for greens
					false);//boolean mono
			sim_pix= simulationPattern.extractSimulPatterns (
					thisSimulParameters,
					1,       // subdivide output pixels
					thisCorrelationSize,    // number of Bayer cells in width of the square selection (half number of pixels)
					0,
					0);
			if ((debug_level>2) && (numNeib==0)){
				//				 if (debug_level>2){
				System.out.println("==========Showing simul"+ixc+":"+iyc);
				SDFA_INSTANCE.showArrays(sim_pix[4].clone(),  "simul"+ixc+":"+iyc);
			}

			simGreensCentered= normalizeAndWindow (sim_pix[4], thisWindow);
			//				 if ((debug_level>2) && (numNeib==0)){
			if (debug_level>2){
				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
			}
			modelCorr=fht_instance.correlate (greens.clone(),  // measured pixel array
					//						 modelCorr=fht_instance.correlate (greens,  // measured pixel array
					simGreensCentered,  // simulated (model) pixel array)
					//	                     distortionParameters.correlationHighPassSigma);
					distortionParameters.correlationHighPassSigma,
					distortionParameters.correlationLowPassSigma,
					distortionParameters.phaseCorrelationFraction);

			//				 if ((debug_level>2) && (numNeib==0)){
			if (debug_level>2){
				System.out.println("==========Showing modelCorr"+ixc+":"+iyc);
				SDFA_INSTANCE.showArrays(modelCorr, "modelCorr"+ixc+":"+iyc);
			}
			//				 xyCorr=new double[2]; //????????????????????
			// Use fast, but less precise method here ?
			//				 if (numNeib==0) System.out.println ("correctedPatternCrossLocation(): debugLevel="+debugLevel+" fast="+fast);
			if (fast) centerXY= correlationMaximum(
					modelCorr,
					distortionParameters.correlationMaxOffset,
					(debug_level>2) && (numNeib==0));
			else      centerXY= correlationMaximum(modelCorr,
					distortionParameters.correlationRadius,
					distortionParameters.correlationThreshold,	//double threshold, // fraction of maximum (slightly less than 1.0) to limit the top part of the maximum for centroid

					distortionParameters.correlationSubdiv,
					distortionParameters.correlationFFTSubdiv,
					fht_instance,
					distortionParameters.correlationMaxOffset,
					0.0, // lowpass filtering already done
					(debug_level>2) && ((numNeib==0) || (passNumber>1)));

			if (centerXY==null) {
				if (debug_level>0) System.out.println("Too far from the center0 ("+beforeXY[0]+"/"+beforeXY[1]+")");
				return null;
			}
			// Verify contrast (if specified) - only for the center sample (numNeib==0)
			if (numNeib==0) {
				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.contrastSelectSigmaCenter, // Gaussian sigma to select correlation centers (pixels, 2.0)
						distortionParameters.contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
						//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 (Double.isNaN(contrasts[0]) || ((distortionParameters.correlationMinContrast>0) && (contrasts[0]<distortionParameters.correlationMinContrast))) {
					if (debug_level>1) System.out.println("Center contrast too low - "+contrasts[0]+"<"+distortionParameters.correlationMinContrast);
					if (debug_level>1) System.out.println("Center 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;
				} else {
					if (debug_level>1) System.out.println("Contrast "+IJ.d2s(contrasts[0],3)+" ("+distortionParameters.correlationMinContrast+")"+
							" is good ("+IJ.d2s(beforeXY[0],3)+"/"+IJ.d2s(beforeXY[1],3)+")->"+
							IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
				}


				if (Double.isNaN(contrasts[1]) || ((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));
			// convert from diagonal greens coordinates to sensor pixel coordinates
			xyCorr[0]+=(-centerXY[0]-centerXY[1])*locsNeib[numNeib][2];
			xyCorr[1]+=( centerXY[0]-centerXY[1])*locsNeib[numNeib][2];/*
				 if (debug_array!=null) {
					 debug_array[9][0]+=(-centerXY[0]-centerXY[1])*locsNeib[numNeib][2];
					 debug_array[9][1]+=( centerXY[0]-centerXY[1])*locsNeib[numNeib][2];
				 }
			 */
			if (debug_level>1) System.out.println("correctedPatternCrossLocation: dist="+IJ.d2s(Math.sqrt(xyCorr[0]*xyCorr[0]+xyCorr[1]*xyCorr[1]),4)+" xyCorr[0]="+IJ.d2s(xyCorr[0],4)+" xyCorr[1]="+ 	IJ.d2s(xyCorr[1],4));
		}
		// average 	xyCorr[]

		//			 result[0]=ixc-xyCorr[0];
		//			 result[1]=iyc-xyCorr[1];

		// disabling correction !!!!!!!!!!!!!!!!!!!!!!!
		result[0]=ixc-xyCorr[0]+diffBeforeXY[0];
		result[1]=iyc-xyCorr[1]+diffBeforeXY[1];
		//			 result[0]=ixc+diffBeforeXY[0];
		//			 result[1]=iyc+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));
		return result;
	}



	private  double [] correctedPatternCrossLocationAverage4(
			LwirReaderParameters lwirReaderParameters, // null is OK
			double [] beforeXY, // initial coordinates of the pattern cross point
			double wv0x,
			double wv0y,
			double wv1x,
			double wv1y,
			double [][] correction,
			ImagePlus imp,      // image data (Bayer mosaic)
			DistortionParameters distortionParameters, //distortionParameters.refineCorrelations
			MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			MatchSimulatedPattern matchSimulatedPattern, // correlationSize
			SimulationPattern.SimulParameters  thisSimulParameters,
			boolean equalizeGreens,
			double [] window,   // window function
			double [] window2,   // window function - twice FFT size (or null)
			double [] window4,   // window function - 4x FFT size (or null)
			SimulationPattern simulationPattern,
			boolean negative, // invert cross phase
			DoubleFHT fht_instance,
			boolean fast, // use fast measuring of the maximum on the correlation
			double [][] locsNeib, // locations and weights of neighbors to average
			int debug_level,
			String dbgStr
			){
		if (imp == null) {
			return null;
		}
		boolean is_lwir = ((lwirReaderParameters != null) && lwirReaderParameters.is_LWIR(imp));
		int     correlation_size =     is_lwir ? distortionParameters.correlationSizeLwir :        distortionParameters.correlationSize;
		int     max_correlation_size = is_lwir ? distortionParameters.maximalCorrelationSizeLwir : distortionParameters.maximalCorrelationSize;
9265 9266 9267 9268 9269 9270
		boolean is_mono = false;
		try {
			is_mono = Boolean.parseBoolean((String) imp.getProperty("MONOCHROME"));
		} catch (Exception e) {
		}
		is_mono |= is_lwir;
9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304

		int debug_threshold = 3;
		// next print - same for good and bad, correction==null
		if (dbgStr!=null) System.out.println(dbgStr+ ": wv0x="+wv0x+" wv0y="+wv0y+ " wv1x="+wv1x+" wv1y="+wv1y+
				" beforeXY[0]="+beforeXY[0]+", beforeXY[1]="+beforeXY[1]+" correction is "+((correction==null)?"null":"not null"));


		boolean dbgThis=
				(Math.abs(beforeXY[0]-patternDetectParameters.debugX)<patternDetectParameters.debugRadius) &&
				(Math.abs(beforeXY[1]-patternDetectParameters.debugY)<patternDetectParameters.debugRadius);
		// dbgThis=true;
//		dbgThis=true;
		if (dbgThis) {
			System.out.println("correctedPatternCrossLocationAverage4(), beforeXY[0]="+beforeXY[0]+", beforeXY[1]="+beforeXY[1]);
			debug_level+=3;
		}
		// Just for testing
		beforeXY[0]+=distortionParameters.correlationDx;  // offset, X (in pixels)
		beforeXY[1]+=distortionParameters.correlationDy; // offset y (in pixels)

		double [][] invConvMatrix= {{1,0},{0,1}}; // identity
		if (!is_lwir) {
			double [][] convMatrix= {{1.0,-1.0},{1.0,1.0}}; // from greens2 to pixel WV
			invConvMatrix= matrix2x2_scale(matrix2x2_invert(convMatrix),2.0);
		}

		double [] result=new double [3];
		result[0]=beforeXY[0];
		result[1]=beforeXY[1];
		result[2]=0.0; // contrast


		if (fht_instance==null) fht_instance=new DoubleFHT(); // move upstream to reduce number of initializations

9305

9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497
		//create diagonal green selection around ixc,iyc

		double [][]wv={{wv0x, wv0y},
				{wv1x, wv1y}};
		double [][] WVgreensMono=matrix2x2_mul(wv,invConvMatrix); // rotated for greens, same dir for mono (lwir)
		if (debug_level > debug_threshold) System.out.println("WVgreensMono[0][0]="+IJ.d2s(WVgreensMono[0][0],3)+
				" WVgreensMono[0][1]="+IJ.d2s(WVgreensMono[0][1],3)+
				" WVgreensMono[1][0]="+IJ.d2s(WVgreensMono[1][0],3)+
				" WVgreensMono[1][1]="+IJ.d2s(WVgreensMono[1][1],3));
		double [] dUV;
//		double [] simGreensCentered;
		double [] simCentered;
		//				 double [] modelCorr;

		double [] centerXY;
		double 	contrast;
		int numNeib;
		double []corr=null;
		double [] neibCenter=new double[2];
		if (correction!=null) { // overwrite wave vectors
			wv[0][0]=correction[0][0];
			wv[0][1]=correction[0][1];
			wv[1][0]=correction[1][0];
			wv[1][1]=correction[1][1];
			if (correction[0].length>3) { // enough data for quadratic approximation
				corr=new double[10];
				corr[0]=correction[0][3]/4;
				corr[1]=correction[0][4]/4;
				corr[2]=correction[0][5]/4;
				corr[3]=correction[1][3]/4;
				corr[4]=correction[1][4]/4;
				corr[5]=correction[1][5]/4;
				corr[6]=0.0;
				corr[7]=0.0;
				corr[9]=0.0;
				corr[9]=0.0;
			}
		}
		double u_span=Math.sqrt(wv0x*wv0x+wv0y*wv0y)*correlation_size;
		double v_span=Math.sqrt(wv1x*wv1x+wv1y*wv1y)*correlation_size;
		double min_span=Math.min(u_span, v_span);
		int thisCorrelationSize=correlation_size;
		double [] thisWindow=window;
		double uv_threshold=distortionParameters.minUVSpan*0.25*Math.sqrt(2.0);

		if (	(min_span < uv_threshold) &&
				(window2 != null) &&
				(thisCorrelationSize < max_correlation_size)) { // trying to increase only twice
			thisCorrelationSize *= 2;
			min_span *= 2;
			thisWindow=window2;
			if (	(min_span < uv_threshold) &&
					(window4 != null) &&
					(thisCorrelationSize < max_correlation_size)) {
				thisCorrelationSize *= 2;
				min_span *= 2;
				thisWindow = window4;
			}
		}

		setCorrelationSizesUsed(thisCorrelationSize);
		if ((debug_level > (debug_threshold - 2))&&(thisCorrelationSize>correlation_size)) System.out.println("**** u/v span too small, increasing FFT size to "+thisCorrelationSize);
		Rectangle centerCross=correlationSelection(
				beforeXY, // initial coordinates of the pattern cross point
				(is_lwir?(thisCorrelationSize/2):(thisCorrelationSize)));

		int ixc = centerCross.x + centerCross.width/2;
		int iyc = centerCross.y + centerCross.height/2;
		double [] diffBeforeXY={beforeXY[0]-ixc, beforeXY[1]-iyc};
		double [] greens_mono; // greens or mono
		if (is_lwir) {
			greens_mono=getNoBayer (imp,centerCross);
			if (debug_level > (debug_threshold +0)) SDFA_INSTANCE.showArrays(greens_mono, "greens_mono");
			if (debug_level > (debug_threshold +0)) System.out.println("ixc="+ixc+" iyc="+iyc);
			normalizeAndWindow (greens_mono, thisWindow);
		} else {
			double[][] input_bayer=splitBayer (imp,centerCross,equalizeGreens);
			if (debug_level > (debug_threshold +1)) SDFA_INSTANCE.showArrays(input_bayer,  true, "centered");
			if (debug_level > (debug_threshold +0)) SDFA_INSTANCE.showArrays(input_bayer[4], "greens");
			if (debug_level > (debug_threshold +0)) System.out.println("ixc="+ixc+" iyc="+iyc);
			greens_mono=normalizeAndWindow (input_bayer[4], thisWindow);
		}
		if (debug_level > (debug_threshold +0)) SDFA_INSTANCE.showArrays(greens_mono, "greens_mono_Windowed");
		// average is not zero - probably
		if (debug_level > (debug_threshold + 0)) {
			System.out.println(" wv0x="+IJ.d2s(wv0x,5)+" wv0y="+IJ.d2s(wv0y,5));
			System.out.println(" wv1x="+IJ.d2s(wv1x,5)+" wv1y="+IJ.d2s(wv1y,5));
			System.out.println(" u-span="+IJ.d2s(u_span,3)+"  v-span="+IJ.d2s(v_span,3)+" threshold="+IJ.d2s(uv_threshold,3)+" ("+IJ.d2s(distortionParameters.minUVSpan,3)+")");
			if (corr!=null) {
				System.out.println(" Ax="+IJ.d2s(corr[0],8)+" Bx="+IJ.d2s(corr[1],8)+" Cx="+IJ.d2s(corr[2],8)+" Dx="+IJ.d2s(corr[6],8)+" Ex="+IJ.d2s(corr[7],8));
				System.out.println(" Ay="+IJ.d2s(corr[3],8)+" By="+IJ.d2s(corr[4],8)+" Cy="+IJ.d2s(corr[5],8)+" Dy="+IJ.d2s(corr[8],8)+" Ey="+IJ.d2s(corr[9],8));
			}
		}
		int [][] gridNeib={{0,0},{0,1},{1,0},{1,1}};
		int numOfNeib=distortionParameters.correlationAverageOnRefine?gridNeib.length:1;
		if (debug_level > (debug_threshold + 0)) {
			System.out.println(" numOfNeib="+numOfNeib+" (distortionParameters.correlationAverageOnRefine="+distortionParameters.correlationAverageOnRefine);
		}
		if (locsNeib.length==1) {
			numOfNeib=1; // on the first pass, from legacy
			if (debug_level > (debug_threshold + 1)) {
				System.out.println("Reduced numOfNeib to "+numOfNeib+" as locsNeib.length="+locsNeib.length);
			}
		}
		if (dbgStr!=null) {
			double dbgSumWindow=0.0;
			for (double dbgD:thisWindow) dbgSumWindow+=dbgD;
			// All he same - good/bad
			System.out.println(dbgStr+ ": thisCorrelationSize="+thisCorrelationSize+" min_span="+min_span+ " dbgSumWindow="+dbgSumWindow+
					"locsNeib.length="+locsNeib.length+" fast="+fast+
					" numOfNeib="+numOfNeib+" (distortionParameters.correlationAverageOnRefine="+distortionParameters.correlationAverageOnRefine);
		}

		double [][] modelCorrs=     new double[numOfNeib][];
		double [][] debugGreens=new double[numOfNeib][0];
		for (numNeib=0;numNeib<numOfNeib;numNeib++) {
			if (is_lwir) { // monochrome, use all pixels
				neibCenter[0]=diffBeforeXY[0]+gridNeib[numNeib][0];
				neibCenter[1]=diffBeforeXY[1]+gridNeib[numNeib][1];
			} else {
				neibCenter[0]=diffBeforeXY[0]+0.5*(gridNeib[numNeib][0]+gridNeib[numNeib][1]);
				neibCenter[1]=diffBeforeXY[1]+0.5*(gridNeib[numNeib][0]-gridNeib[numNeib][1]);
			}
			double [] barray;
			if (is_lwir) {
				//negative=!negative;
				dUV=matrix2x2_scale(matrix2x2_mul(wv,neibCenter),-2*Math.PI);
//				dUV[0] = 0.0; dUV[1] = 0.0;
				if (debug_level > (debug_threshold + 20)){
					double []barray0 = simulationPattern.simulatePatternFullPatternSafe( // Is it the most time-consuming part? should it be done once and then only extraction separate?
							wv0x,
							wv0y,
							(negative?(-Math.PI/2):Math.PI/2), // negative?(-Math.PI/2):Math.PI/2,
							wv1x,
							wv1y,
							Math.PI/2, //Math.PI/2,
							corr, //null, // no mesh distortion here
							thisSimulParameters.subdiv,// SIMUL.subdiv, - do not need high quality here
							thisCorrelationSize,
							false,// false); // center for greens ???
							false);//boolean mono
					double []barray1 = simulationPattern.simulatePatternFullPatternSafe( // Is it the most time-consuming part? should it be done once and then only extraction separate?
							wv0x,
							wv0y,
							(negative?(-Math.PI/2):Math.PI/2), // negative?(-Math.PI/2):Math.PI/2,
							wv1x,
							wv1y,
							Math.PI/2, //Math.PI/2,
							corr, //null, // no mesh distortion here
							thisSimulParameters.subdiv,// SIMUL.subdiv, - do not need high quality here
							thisCorrelationSize,
							true,// false); // center for greens ???
							false);//boolean mono
					double []barray2 = simulationPattern.simulatePatternFullPatternSafe( // Is it the most time-consuming part? should it be done once and then only extraction separate?
							wv0x,
							wv0y,
							(negative?(-Math.PI/2):Math.PI/2), // negative?(-Math.PI/2):Math.PI/2,
							wv1x,
							wv1y,
							Math.PI/2, //Math.PI/2,
							corr, //null, // no mesh distortion here
							thisSimulParameters.subdiv,// SIMUL.subdiv, - do not need high quality here
							thisCorrelationSize,
							true,// false); // center for greens ???
							true);//boolean mono
					double [][] dbg_barray = {barray0, barray1, barray2};
					System.out.println(">=========Showing barray01"+ixc+":"+iyc);
					SDFA_INSTANCE.showArrays(dbg_barray, true, "barray"+ixc+":"+iyc);
					double [] sim_pix0= simulationPattern.extractSimulMono (
							barray0,
							thisSimulParameters,
							1,       // subdivide output pixels
							thisCorrelationSize,    // number of Bayer cells in width of the square selection (half number of pixels)
							0,
							0);
					double [] sim_pix1= simulationPattern.extractSimulMono (
							barray1,
							thisSimulParameters,
							1,       // subdivide output pixels
							thisCorrelationSize,    // number of Bayer cells in width of the square selection (half number of pixels)
							0,
							0);
					double [] sim_pix2= simulationPattern.extractSimulMono (
							barray2,
							thisSimulParameters,
							1,       // subdivide output pixels
							thisCorrelationSize,    // number of Bayer cells in width of the square selection (half number of pixels)
							0,
							0);
					double [][] dbg_sim_pix = {sim_pix0, sim_pix1, sim_pix2};
					System.out.println(">=========Showing barray01"+ixc+":"+iyc);
					SDFA_INSTANCE.showArrays(dbg_sim_pix, true, "sim_pix"+ixc+":"+iyc);
9498

9499
				}
9500

9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537
				barray= simulationPattern.simulatePatternFullPatternSafe( // Is it the most time-consuming part? should it be done once and then only extraction separate?
						wv0x,
						wv0y,
						dUV[0]+(negative?(-Math.PI/2):Math.PI/2), // negative?(-Math.PI/2):Math.PI/2,
						wv1x,
						wv1y,
						dUV[1]+Math.PI/2, //Math.PI/2,
						corr, //null, // no mesh distortion here
						thisSimulParameters.subdiv,// SIMUL.subdiv, - do not need high quality here
						thisCorrelationSize,
						true,// false); // center for greens ???
						true);//boolean mono
				if (debug_level > (debug_threshold + 0)){
					System.out.println(">=========Showing barray"+ixc+":"+iyc);
					SDFA_INSTANCE.showArrays(barray, "barray"+ixc+":"+iyc);
				}
				// barray for dUV=={0,0} is symmetrical around center pixel,
				// sim_pix - around {center - 0.5, center - 0.5}
				// for center_for_g2 - sim_pix is symmetrical around [center,center],
				// for !mono && !center_for_g2 - [center+0.5, center+0.5]
				// TODO: reduce size of barray for mono twice in each direction
				double [] sim_pix= simulationPattern.extractSimulMono (
						barray,
						thisSimulParameters,
						1,       // subdivide output pixels
						thisCorrelationSize,    // number of Bayer cells in width of the square selection (half number of pixels)
						0,
						0);
				if (sim_pix==null){
					System.out.println("***** BUG: extractSimulPatterns() FAILED *****");
					return null;
				}
				if (dbgStr!=null) {
					double dbgSumWindow=0.0;
					for (double dbgD:sim_pix) dbgSumWindow+=dbgD;
					System.out.println(dbgStr+ ": SUM of sim_pix="+dbgSumWindow); // First difference good/bad
				}
9538

9539
				simCentered= normalizeAndWindow (sim_pix, thisWindow);
9540

9541 9542 9543
			} else {
				dUV=matrix2x2_scale(matrix2x2_mul(wv,neibCenter),-2*Math.PI);
				barray= simulationPattern.simulatePatternFullPatternSafe( // Is it the most time-consuming part? should it be done once and then only extraction separate?
Andrey Filippov's avatar
Andrey Filippov committed
9544 9545
						wv0x,
						wv0y,
9546
						dUV[0]+(negative?(-Math.PI/2):Math.PI/2), // negative?(-Math.PI/2):Math.PI/2,
Andrey Filippov's avatar
Andrey Filippov committed
9547 9548
						wv1x,
						wv1y,
9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559
						dUV[1]+Math.PI/2, //Math.PI/2,
						corr, //null, // no mesh distortion here
						thisSimulParameters.subdiv,// SIMUL.subdiv, - do not need high quality here
						thisCorrelationSize,
						true, // center for greens
						false);//boolean mono
				if (debug_level > (debug_threshold + 0)){
					System.out.println(">=========Showing barray"+ixc+":"+iyc);
					SDFA_INSTANCE.showArrays(barray, "barray"+ixc+":"+iyc);
				}
//		double[][] sim_pix;
9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571
				double[][] sim_pix=null;
				if (is_mono) {
					sim_pix= new double[1][];
					sim_pix[0]=  simulationPattern.extractSimulMono ( // TODO: can use twice smaller barray
							barray,
							thisSimulParameters,
							1,  // subdivide output pixels - now 4
							thisCorrelationSize,    // number of Bayer cells in width of the square selection (half number of pixels)
							0,    // selection center, X (in pixels)
							0);
				} else {
					sim_pix= simulationPattern.extractSimulPatterns (
9572 9573 9574 9575 9576 9577
							barray,
							thisSimulParameters,
							1,       // subdivide output pixels
							thisCorrelationSize,    // number of Bayer cells in width of the square selection (half number of pixels)
							0,
							0);
9578
				}
9579 9580 9581 9582 9583 9584 9585 9586
				if (sim_pix==null){
					System.out.println("***** BUG: extractSimulPatterns() FAILED *****");
					return null;
				}
				if (dbgStr!=null) {
					double dbgSumWindow=0.0;
					for (double[] dbgSlice:sim_pix) for (double dbgD:dbgSlice) dbgSumWindow+=dbgD;
					System.out.println(dbgStr+ ": SUM of sim_pix="+dbgSumWindow); // First difference good/bad
9587 9588


9589
				}
9590

9591 9592
				simCentered= normalizeAndWindow (sim_pix[4], thisWindow);
			}
9593 9594 9595



9596 9597 9598 9599 9600
			if (dbgStr!=null) {
				double dbgSumWindow=0.0;
				for (double dbgD:simCentered) dbgSumWindow+=dbgD;
				System.out.println(dbgStr+ ": SUM of simGreensCentered="+dbgSumWindow);
			}
Andrey Filippov's avatar
Andrey Filippov committed
9601

9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626
			debugGreens[numNeib]=simCentered.clone();

			modelCorrs[numNeib]=fht_instance.phaseCorrelate (
					greens_mono.clone(),
					simCentered,
					patternDetectParameters.phaseCoeff,
					0,//   distortionParameters.correlationHighPassSigma,
					patternDetectParameters.lowpass_sigma, // (fast?distortionParameters.correlationLowPassSigma:0.0),// moved to decimation via FFT
					null,
					null);

			if (dbgStr!=null) {
				double dbgSumWindow=0.0;
				for (double[] dbgSlice:modelCorrs) for (double dbgD:dbgSlice) dbgSumWindow+=dbgD;
				System.out.println(dbgStr+ ": SUM of modelCorrs="+dbgSumWindow);
			}
		}
		if (debug_level > (debug_threshold + 0)){
			System.out.println(">=========Showing simCentered"+ixc+":"+iyc);
			SDFA_INSTANCE.showArrays(debugGreens, true, "simCentered"+ixc+":"+iyc);
		}
		if (debug_level > (debug_threshold + 0)){
			System.out.println(">=========Showing modelCorrs, passNumber="+passNumber);
			SDFA_INSTANCE.showArrays(modelCorrs, true, "modelCorrs:"+numOfNeib);
		}
Andrey Filippov's avatar
Andrey Filippov committed
9627

9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648
		// combine 4 correlations into the double resolution, same output size (so half input size) array
		int halfSize=thisCorrelationSize/2;
		int qSize=thisCorrelationSize/4;
		int thisFFTSubdiv=distortionParameters.correlationFFTSubdiv;
		double thisLowpass=distortionParameters.correlationLowPassSigma;
		double [] modelCorr;
		if (numOfNeib>1) {
			modelCorr=new double [thisCorrelationSize*thisCorrelationSize];
			for (int i=0;i<modelCorr.length;    i++) modelCorr[i]=0.0;

			for (int dy=0;dy<2;dy++) for (int dx=0;dx<2;dx++)  {
				for (int y=0;y<halfSize;y++) for (int x=0;x<halfSize;x++) {
					modelCorr[(2*y+dy)*thisCorrelationSize+(2*x+dx)]+=
							modelCorrs[2*dy+dx][(qSize+y)*thisCorrelationSize+(qSize+x)];
				}
			}
			thisLowpass/=2.0; // the lower the value, the more filtering.  Decimated twice,so low pass filtering - accordingly
			thisFFTSubdiv=(thisFFTSubdiv>1)?(thisFFTSubdiv/2):1;
		} else {
			modelCorr=    modelCorrs[0];     // also - different size
		}
Andrey Filippov's avatar
Andrey Filippov committed
9649

9650 9651 9652 9653
		if (debug_level > (debug_threshold + 0)){
			System.out.println(">==========Showing modelCorr");
			SDFA_INSTANCE.showArrays(modelCorr, thisCorrelationSize,thisCorrelationSize, "modelCorr");
		}
Andrey Filippov's avatar
Andrey Filippov committed
9654

9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675
		if (fast) centerXY= correlationMaximum( // maybe twice actual size if
				modelCorr,
				distortionParameters.correlationMaxOffset,
				(debug_level > (debug_threshold + 0)) && (numNeib==0));  // low-pass filtering should already be done
		else      centerXY= correlationMaximum(
				modelCorr,
				distortionParameters.correlationRadius,
				distortionParameters.correlationThreshold,	//double threshold, // fraction of maximum (slightly less than 1.0) to limit the top part of the maximum for centroid

				distortionParameters.correlationSubdiv,
				thisFFTSubdiv,
				fht_instance,
				distortionParameters.correlationMaxOffset,
				thisLowpass, //distortionParameters.correlationLowPassSigma
				//						 (debug_level>2) && (passNumber>1));
				(debug_level > (debug_threshold + 0)));
		if (centerXY==null) {
			if (debug_level > (debug_threshold - 1)) System.out.println("Too far from the center01 ("+beforeXY[0]+"/"+beforeXY[1]+")");
			if (dbgStr!=null) System.out.println(dbgStr+ "- Too far from the center01 ("+beforeXY[0]+"/"+beforeXY[1]+")");
			return null;
		}
9676

9677 9678 9679 9680 9681
		if (numNeib>1){
			centerXY[0]*=0.5;
			centerXY[1]*=0.5;
			for (int i=0;i<2;i++) for (int j=0;j<2;j++) WVgreensMono[i][j]*=0.5;
		}
Andrey Filippov's avatar
Andrey Filippov committed
9682

9683 9684


9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706
		double [] contrasts= correlationContrast(
				modelCorr,
				greens_mono,
				WVgreensMono,    // wave vectors (same units as the pixels array)
				distortionParameters.contrastSelectSigmaCenter, // Gaussian sigma to select correlation (pixels, 2.0)
				distortionParameters.contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
				centerXY[0],    //  x0,              // center coordinates
				centerXY[1],    //y0,
				"test-contrast");   // title base for optional plots names
		if ((debug_level > (debug_threshold - 1))) {
			System.out.println("contrast = "+contrasts[0]);
		}
		contrast=contrasts[0];
		result[2]=contrast;
		if (Double.isNaN(contrasts[0]) || ((distortionParameters.correlationMinContrast>0) && (contrasts[0]<distortionParameters.correlationMinContrast))) {
			if ((debug_level > (debug_threshold - 1))) System.out.println("Contrast too low - "+contrasts[0]+"<"+distortionParameters.correlationMinContrast);
			if (debug_level > (debug_threshold - 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));
			if (dbgStr!=null) System.out.println(dbgStr+ " - 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));
Andrey Filippov's avatar
Andrey Filippov committed
9707

9708 9709 9710 9711 9712 9713 9714 9715 9716
			return null;
		} else {
			if (debug_level > (debug_threshold - 1)) System.out.println("Contrast "+IJ.d2s(contrasts[0],3)+" ("+distortionParameters.correlationMinContrast+")"+
					" is GOOD ("+IJ.d2s(beforeXY[0],3)+"/"+IJ.d2s(beforeXY[1],3)+")->"+
					IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
			if (dbgStr!=null) System.out.println(dbgStr+ " - Contrast "+IJ.d2s(contrasts[0],3)+" ("+distortionParameters.correlationMinContrast+")"+
					" is GOOD ("+IJ.d2s(beforeXY[0],3)+"/"+IJ.d2s(beforeXY[1],3)+")->"+
					IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
		}
Andrey Filippov's avatar
Andrey Filippov committed
9717

9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731
		if (Double.isNaN(contrasts[1]) || ((distortionParameters.correlationMinAbsoluteContrast>0) && (contrasts[1]<distortionParameters.correlationMinAbsoluteContrast))) {
			if (debug_level > (debug_threshold - 1)) System.out.println("Absolute contrast too low - "+contrasts[1]+"<"+distortionParameters.correlationMinAbsoluteContrast);
			if (debug_level > (debug_threshold - 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));
			if (dbgStr!=null) System.out.println(dbgStr+ " - 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;
		} else {
			if (dbgStr!=null) System.out.println(dbgStr+ " - Absolute contrast "+IJ.d2s(contrasts[1],3)+" ("+distortionParameters.correlationMinAbsoluteContrast+")"+
					" is GOOD ("+IJ.d2s(beforeXY[0],3)+"/"+IJ.d2s(beforeXY[1],3)+")->"+
					IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
		}
9732

9733
		if (debug_level > (debug_threshold - 0))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));
9734

9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746
// FIXME: maybe wrong for mono?
		if (is_lwir) {
			result[0]=ixc + diffBeforeXY[0] + centerXY[0];
			result[1]=iyc + diffBeforeXY[1] + centerXY[1];
		} else {
			result[0] = ixc + diffBeforeXY[0] - (-centerXY[0] -centerXY[1]);
			result[1] = iyc + diffBeforeXY[1] - ( centerXY[0] -centerXY[1]);
		}
		if (debug_level > (debug_threshold + 0)) System.out.println(">---correctedPatternCrossLocation: before x="+IJ.d2s(beforeXY[0],3)+" y="+IJ.d2s(beforeXY[1],3));
		if (debug_level > (debug_threshold + 0)) System.out.println(">+++correctedPatternCrossLocation: after  x="+IJ.d2s(result[0],3)+" y="+IJ.d2s(result[1],3));
		return result;
	}
9747

9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790
	// ======= end of private  double [] correctedPatternCrossLocationAverage4() ===
	private  double [] correctedPatternCrossLocationAverage4TestOldNew(
			double [] beforeXY, // initial coordinates of the pattern cross point
			double wv0x,
			double wv0y,
			double wv1x,
			double wv1y,
			double [][] correction,
			ImagePlus imp,      // image data (Bayer mosaic)
			DistortionParameters distortionParameters, //distortionParameters.refineCorrelations
			MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			MatchSimulatedPattern matchSimulatedPattern, // correlationSize
			SimulationPattern.SimulParameters  thisSimulParameters,
			boolean equalizeGreens,
			double [] window,   // window function
			double [] window2,   // window function - twice FFT size (or null)
			double [] window4,   // window function - 4x FFT size (or null)
			SimulationPattern simulationPattern,
			boolean negative, // invert cross phase
			DoubleFHT fht_instance,
			boolean fast, // use fast measuring of the maximum on the correlation
			double [][] locsNeib, // locations and weights of neighbors to average
			int debug_level,
			String dbgStr
			){
		int debug_threshold = 3;
		// next print - same for good and bad, correction==null
		if (dbgStr!=null) System.out.println(dbgStr+ ": wv0x="+wv0x+" wv0y="+wv0y+ " wv1x="+wv1x+" wv1y="+wv1y+
				" beforeXY[0]="+beforeXY[0]+", beforeXY[1]="+beforeXY[1]+" correction is "+((correction==null)?"null":"not null"));


		boolean dbgThis=
				(Math.abs(beforeXY[0]-patternDetectParameters.debugX)<patternDetectParameters.debugRadius) &&
				(Math.abs(beforeXY[1]-patternDetectParameters.debugY)<patternDetectParameters.debugRadius);
		dbgThis=true;
		if (dbgThis) {
			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]);
		// Just for testing
		beforeXY[0]+=distortionParameters.correlationDx;  // offset, X (in pixels)
		beforeXY[1]+=distortionParameters.correlationDy; // offset y (in pixels)
9791 9792


9793 9794
		double [][] convMatrix= {{1.0,-1.0},{1.0,1.0}}; // from greens2 to pixel WV
		double [][] invConvMatrix= matrix2x2_scale(matrix2x2_invert(convMatrix),2.0);
9795

9796 9797 9798 9799
		double [] result=new double [3];
		result[0]=beforeXY[0];
		result[1]=beforeXY[1];
		result[2]=0.0; // contrast
9800 9801


9802
		if (fht_instance==null) fht_instance=new DoubleFHT(); // move upstream to reduce number of initializations
9803 9804


9805
		//create diagonal green selection around ixc,iyc
9806

9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865
		double [][]wv={{wv0x, wv0y},
				{wv1x, wv1y}};
		double [][] WVgreens=matrix2x2_mul(wv,invConvMatrix);
		if (debug_level > debug_threshold) System.out.println("WVgreens[0][0]="+IJ.d2s(WVgreens[0][0],3)+
				" WVgreens[0][1]="+IJ.d2s(WVgreens[0][1],3)+
				" WVgreens[1][0]="+IJ.d2s(WVgreens[1][0],3)+
				" WVgreens[1][1]="+IJ.d2s(WVgreens[1][1],3));
		double [] dUV;
		double[][] sim_pix;
		double [] simGreensCentered;
		//				 double [] modelCorr;

		double [] centerXY;
		double 	contrast;
		int numNeib;
		double []corr=null;
		double [] neibCenter=new double[2];
		if (correction!=null) { // overwrite wave vectors
			wv[0][0]=correction[0][0];
			wv[0][1]=correction[0][1];
			wv[1][0]=correction[1][0];
			wv[1][1]=correction[1][1];
			if (correction[0].length>3) { // enough data for quadratic approximation
				corr=new double[10];
				corr[0]=correction[0][3]/4;
				corr[1]=correction[0][4]/4;
				corr[2]=correction[0][5]/4;
				corr[3]=correction[1][3]/4;
				corr[4]=correction[1][4]/4;
				corr[5]=correction[1][5]/4;
				corr[6]=0.0;
				corr[7]=0.0;
				corr[9]=0.0;
				corr[9]=0.0;
			}
		}
		double u_span=Math.sqrt(wv0x*wv0x+wv0y*wv0y)*distortionParameters.correlationSize;
		double v_span=Math.sqrt(wv1x*wv1x+wv1y*wv1y)*distortionParameters.correlationSize;
		double min_span=Math.min(u_span, v_span);
		int thisCorrelationSize=distortionParameters.correlationSize;
		double [] thisWindow=window;
		double uv_threshold=distortionParameters.minUVSpan*0.25*Math.sqrt(2.0);

		if (
				(min_span<uv_threshold) &&
				(window2!=null) &&
				(thisCorrelationSize<distortionParameters.maximalCorrelationSize)) { // trying to increase only twice
			thisCorrelationSize*=2;
			min_span*=2;
			thisWindow=window2;
			if (
					(min_span<uv_threshold) &&
					(window4!=null) &&
					(thisCorrelationSize<distortionParameters.maximalCorrelationSize)) {
				thisCorrelationSize*=2;
				min_span*=2;
				thisWindow=window4;
			}
		}
9866

9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912
		setCorrelationSizesUsed(thisCorrelationSize);
		if ((debug_level > (debug_threshold - 2))&&(thisCorrelationSize>distortionParameters.correlationSize)) System.out.println("**** u/v span too small, increasing FFT size to "+thisCorrelationSize);
		Rectangle centerCross=correlationSelection(
				beforeXY, // initial coordinates of the pattern cross point
				thisCorrelationSize);

		int ixc=centerCross.x+centerCross.width/2;
		int iyc=centerCross.y+centerCross.height/2;
		double [] diffBeforeXY={beforeXY[0]-ixc, beforeXY[1]-iyc};
		double[][] input_bayer=splitBayer (imp,centerCross,equalizeGreens);

		if (debug_level > (debug_threshold +1)) SDFA_INSTANCE.showArrays(input_bayer,  true, "centered");
		if (debug_level > (debug_threshold +0)) SDFA_INSTANCE.showArrays(input_bayer[4], "greens");
		if (debug_level > (debug_threshold +0)) System.out.println("ixc="+ixc+" iyc="+iyc);
		double [] greens=normalizeAndWindow (input_bayer[4], thisWindow);
		if (debug_level > (debug_threshold +0)) SDFA_INSTANCE.showArrays(greens, "greensWindowed");
		// average is not zero - probably

		if (debug_level > (debug_threshold + 0)) {
			System.out.println(" wv0x="+IJ.d2s(wv0x,5)+" wv0y="+IJ.d2s(wv0y,5));
			System.out.println(" wv1x="+IJ.d2s(wv1x,5)+" wv1y="+IJ.d2s(wv1y,5));
			System.out.println(" u-span="+IJ.d2s(u_span,3)+"  v-span="+IJ.d2s(v_span,3)+" threshold="+IJ.d2s(uv_threshold,3)+" ("+IJ.d2s(distortionParameters.minUVSpan,3)+")");
			if (corr!=null) {
				System.out.println(" Ax="+IJ.d2s(corr[0],8)+" Bx="+IJ.d2s(corr[1],8)+" Cx="+IJ.d2s(corr[2],8)+" Dx="+IJ.d2s(corr[6],8)+" Ex="+IJ.d2s(corr[7],8));
				System.out.println(" Ay="+IJ.d2s(corr[3],8)+" By="+IJ.d2s(corr[4],8)+" Cy="+IJ.d2s(corr[5],8)+" Dy="+IJ.d2s(corr[8],8)+" Ey="+IJ.d2s(corr[9],8));
			}
		}
		int [][] greenNeib={{0,0},{0,1},{1,0},{1,1}};
		int numOfNeib=distortionParameters.correlationAverageOnRefine?greenNeib.length:1;
		if (debug_level > (debug_threshold + 0)) {
			System.out.println(" numOfNeib="+numOfNeib+" (distortionParameters.correlationAverageOnRefine="+distortionParameters.correlationAverageOnRefine);
		}
		if (locsNeib.length==1) {
			numOfNeib=1; // on the first pass, from legacy
			if (debug_level > (debug_threshold + 0)) {
				System.out.println("Reduced numOfNeib to "+numOfNeib+" as locsNeib.length="+locsNeib.length);
			}
		}
		if (dbgStr!=null) {
			double dbgSumWindow=0.0;
			for (double dbgD:thisWindow) dbgSumWindow+=dbgD;
			// All he same - good/bad
			System.out.println(dbgStr+ ": thisCorrelationSize="+thisCorrelationSize+" min_span="+min_span+ " dbgSumWindow="+dbgSumWindow+
					"locsNeib.length="+locsNeib.length+" fast="+fast+
					" numOfNeib="+numOfNeib+" (distortionParameters.correlationAverageOnRefine="+distortionParameters.correlationAverageOnRefine);
		}
9913

9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947
		double [][] modelCorrs=     new double[numOfNeib][];
		double [][] modelCorrs_new= new double[numOfNeib][];
		double [][] debugGreens=new double[numOfNeib][0];
		for (numNeib=0;numNeib<numOfNeib;numNeib++) {
			neibCenter[0]=diffBeforeXY[0]+0.5*(greenNeib[numNeib][0]+greenNeib[numNeib][1]);
			neibCenter[1]=diffBeforeXY[1]+0.5*(greenNeib[numNeib][0]-greenNeib[numNeib][1]);
			dUV=matrix2x2_scale(matrix2x2_mul(wv,neibCenter),-2*Math.PI);
			double [] barray= simulationPattern.simulatePatternFullPatternSafe( // Is it the most time-consuming part? should it be done once and then only extraction separate?
					wv0x,
					wv0y,
					dUV[0]+(negative?(-Math.PI/2):Math.PI/2), // negative?(-Math.PI/2):Math.PI/2,
					wv1x,
					wv1y,
					dUV[1]+Math.PI/2, //Math.PI/2,
					corr, //null, // no mesh distortion here
					thisSimulParameters.subdiv,// SIMUL.subdiv, - do not need high quality here
					thisCorrelationSize,
					true, // center for greens
					false);//boolean mono
			sim_pix= simulationPattern.extractSimulPatterns (
					barray,
					thisSimulParameters,
					1,       // subdivide output pixels
					thisCorrelationSize,    // number of Bayer cells in width of the square selection (half number of pixels)
					0,
					0);
			if (sim_pix==null){
				System.out.println("***** BUG: extractSimulPatterns() FAILED *****");
				return null;
			}
			if (dbgStr!=null) {
				double dbgSumWindow=0.0;
				for (double[] dbgSlice:sim_pix) for (double dbgD:dbgSlice) dbgSumWindow+=dbgD;
				System.out.println(dbgStr+ ": SUM of sim_pix="+dbgSumWindow); // First difference good/bad
9948 9949


9950
			}
9951

9952
			simGreensCentered= normalizeAndWindow (sim_pix[4], thisWindow);
9953

9954 9955 9956 9957 9958
			if (dbgStr!=null) {
				double dbgSumWindow=0.0;
				for (double dbgD:simGreensCentered) dbgSumWindow+=dbgD;
				System.out.println(dbgStr+ ": SUM of simGreensCentered="+dbgSumWindow);
			}
9959

9960
			debugGreens[numNeib]=simGreensCentered.clone();
9961

9962 9963
			// testing if phase reversal would exactly inverse result pattern - tested, perfect
			double [] simGreensCenteredClone = simGreensCentered.clone();
9964

9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985
			modelCorrs[numNeib]=fht_instance.correlate (greens.clone(),  // measured pixel array
					//						 modelCorr=fht_instance.correlate (greens,  // measured pixel array
					simGreensCentered,  // simulated (model) pixel array)
					//	                     distortionParameters.correlationHighPassSigma);
					distortionParameters.correlationHighPassSigma,
					(fast ? distortionParameters.correlationLowPassSigma : 0.0),// moved to decimation via FFT
					distortionParameters.phaseCorrelationFraction);
			modelCorrs_new[numNeib]=fht_instance.phaseCorrelate (
					greens.clone(),
					simGreensCenteredClone,
					patternDetectParameters.phaseCoeff,
					0,//   distortionParameters.correlationHighPassSigma,
					patternDetectParameters.lowpass_sigma, // (fast?distortionParameters.correlationLowPassSigma:0.0),// moved to decimation via FFT
					null,
					null);

			if (dbgStr!=null) {
				double dbgSumWindow=0.0;
				for (double[] dbgSlice:modelCorrs) for (double dbgD:dbgSlice) dbgSumWindow+=dbgD;
				System.out.println(dbgStr+ ": SUM of modelCorrs="+dbgSumWindow);
			}
Andrey Filippov's avatar
Andrey Filippov committed
9986

9987

9988 9989 9990 9991 9992
		}
		if (debug_level > (debug_threshold + 0)){
			System.out.println(">=========Showing simGreensCentered"+ixc+":"+iyc);
			SDFA_INSTANCE.showArrays(debugGreens, true, "simGreensCentered"+ixc+":"+iyc);
		}
9993

9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026
		if (debug_level > (debug_threshold + 0)){
			System.out.println(">=========Showing modelCorrs, passNumber="+passNumber);
			SDFA_INSTANCE.showArrays(modelCorrs, true, "modelCorrs:"+numOfNeib);
			SDFA_INSTANCE.showArrays(modelCorrs_new, true, "modelCorrs_new:"+numOfNeib);
		}

		// combine 4 correlations into the double resolution, same output size (so half input size) array
		int halfSize=thisCorrelationSize/2;
		int qSize=thisCorrelationSize/4;
		int thisFFTSubdiv=distortionParameters.correlationFFTSubdiv;
		double thisLowpass=distortionParameters.correlationLowPassSigma;
		double [] modelCorr;
		double [] modelCorr_new;
		if (numOfNeib>1) {
			modelCorr=new double [thisCorrelationSize*thisCorrelationSize];
			modelCorr_new=new double [thisCorrelationSize*thisCorrelationSize];
			for (int i=0;i<modelCorr.length;    i++) modelCorr[i]=0.0;
			for (int i=0;i<modelCorr_new.length;i++) modelCorr_new[i]=0.0;

			for (int dy=0;dy<2;dy++) for (int dx=0;dx<2;dx++)  {
				for (int y=0;y<halfSize;y++) for (int x=0;x<halfSize;x++) {
					modelCorr[(2*y+dy)*thisCorrelationSize+(2*x+dx)]+=
							modelCorrs[2*dy+dx][(qSize+y)*thisCorrelationSize+(qSize+x)];
					modelCorr_new[(2*y+dy)*thisCorrelationSize+(2*x+dx)]+=
							modelCorrs_new[2*dy+dx][(qSize+y)*thisCorrelationSize+(qSize+x)];
				}
			}
			thisLowpass/=2.0; // the lower the value, the more filtering.  Decimated twice,so low pass filtering - accordingly
			thisFFTSubdiv=(thisFFTSubdiv>1)?(thisFFTSubdiv/2):1;
		} else {
			modelCorr=    modelCorrs[0];     // also - different size
			modelCorr_new=modelCorrs_new[0]; // also - different size
		}
10027 10028 10029



10030 10031 10032 10033
		if (debug_level > (debug_threshold + 0)){
			System.out.println(">==========Showing modelCorr");
			SDFA_INSTANCE.showArrays(modelCorr, thisCorrelationSize,thisCorrelationSize, "modelCorr");
		}
10034

10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074
		double [] centerXY_new;

		if (fast) centerXY_new= correlationMaximum( // maybe twice actual size if
				modelCorr_new,
				distortionParameters.correlationMaxOffset,
				(debug_level > (debug_threshold + 0)) && (numNeib==0));  // low-pass filtering should already be done
		else      centerXY_new= correlationMaximum(
				modelCorr_new,
				distortionParameters.correlationRadius,
				distortionParameters.correlationThreshold,	//double threshold, // fraction of maximum (slightly less than 1.0) to limit the top part of the maximum for centroid

				distortionParameters.correlationSubdiv,
				thisFFTSubdiv,
				fht_instance,
				distortionParameters.correlationMaxOffset,
				thisLowpass, //distortionParameters.correlationLowPassSigma
				//						 (debug_level>2) && (passNumber>1));
				(debug_level > (debug_threshold + 0)));

		if (fast) centerXY= correlationMaximum( // maybe twice actual size if
				modelCorr,
				distortionParameters.correlationMaxOffset,
				(debug_level > (debug_threshold + 0)) && (numNeib==0));  // low-pass filtering should already be done
		else      centerXY= correlationMaximum(
				modelCorr,
				distortionParameters.correlationRadius,
				distortionParameters.correlationThreshold,	//double threshold, // fraction of maximum (slightly less than 1.0) to limit the top part of the maximum for centroid

				distortionParameters.correlationSubdiv,
				thisFFTSubdiv,
				fht_instance,
				distortionParameters.correlationMaxOffset,
				thisLowpass, //distortionParameters.correlationLowPassSigma
				//						 (debug_level>2) && (passNumber>1));
				(debug_level > (debug_threshold + 0)));
		if (centerXY==null) {
			if (debug_level > (debug_threshold - 1)) System.out.println("Too far from the center01 ("+beforeXY[0]+"/"+beforeXY[1]+")");
			if (dbgStr!=null) System.out.println(dbgStr+ "- Too far from the center01 ("+beforeXY[0]+"/"+beforeXY[1]+")");
			return null;
		}
10075

10076
		//				 debug_level=3;
10077

10078 10079 10080 10081 10082 10083 10084
		if (numNeib>1){
			centerXY[0]*=0.5;
			centerXY[1]*=0.5;
			centerXY_new[0]*=0.5;
			centerXY_new[1]*=0.5;
			for (int i=0;i<2;i++) for (int j=0;j<2;j++) WVgreens[i][j]*=0.5;
		}
10085

10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117
		double [] contrasts_new= correlationContrast(
				modelCorr_new,
				greens,
				WVgreens,    // wave vectors (same units as the pixels array)
				distortionParameters.contrastSelectSigmaCenter, // Gaussian sigma to select correlation centers (pixels, 2.0)
				distortionParameters.contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
				centerXY[0],    //  x0,              // center coordinates
				centerXY[1],    //y0,
				"test-contrast-new");   // title base for optional plots names

		double [] contrasts= correlationContrast(
				modelCorr,
				greens,
				WVgreens,    // wave vectors (same units as the pixels array)
				distortionParameters.contrastSelectSigmaCenter, // Gaussian sigma to select correlation (pixels, 2.0)
				distortionParameters.contrastSelectSigma, // Gaussian sigma to select correlation centers (fraction of UV period), 0.1
				centerXY[0],    //  x0,              // center coordinates
				centerXY[1],    //y0,
				"test-contrast");   // title base for optional plots names
		if ((debug_level > (debug_threshold - 1))) {
			System.out.println("contrast_new = "+contrasts_new[0]+", contrast = "+contrasts[0]);
		}
		contrast=contrasts[0];
		result[2]=contrast;
		if (Double.isNaN(contrasts[0]) || ((distortionParameters.correlationMinContrast>0) && (contrasts[0]<distortionParameters.correlationMinContrast))) {
			if ((debug_level > (debug_threshold - 1))) System.out.println("Contrast too low - "+contrasts[0]+"<"+distortionParameters.correlationMinContrast);
			if (debug_level > (debug_threshold - 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));
			if (dbgStr!=null) System.out.println(dbgStr+ " - 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));
10118

10119 10120 10121 10122 10123 10124 10125 10126 10127
			return null;
		} else {
			if (debug_level > (debug_threshold - 1)) System.out.println("Contrast "+IJ.d2s(contrasts[0],3)+" ("+distortionParameters.correlationMinContrast+")"+
					" is GOOD ("+IJ.d2s(beforeXY[0],3)+"/"+IJ.d2s(beforeXY[1],3)+")->"+
					IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
			if (dbgStr!=null) System.out.println(dbgStr+ " - Contrast "+IJ.d2s(contrasts[0],3)+" ("+distortionParameters.correlationMinContrast+")"+
					" is GOOD ("+IJ.d2s(beforeXY[0],3)+"/"+IJ.d2s(beforeXY[1],3)+")->"+
					IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
		}
10128 10129


10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143
		if (Double.isNaN(contrasts[1]) || ((distortionParameters.correlationMinAbsoluteContrast>0) && (contrasts[1]<distortionParameters.correlationMinAbsoluteContrast))) {
			if (debug_level > (debug_threshold - 1)) System.out.println("Absolute contrast too low - "+contrasts[1]+"<"+distortionParameters.correlationMinAbsoluteContrast);
			if (debug_level > (debug_threshold - 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));
			if (dbgStr!=null) System.out.println(dbgStr+ " - 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;
		} else {
			if (dbgStr!=null) System.out.println(dbgStr+ " - Absolute contrast "+IJ.d2s(contrasts[1],3)+" ("+distortionParameters.correlationMinAbsoluteContrast+")"+
					" is GOOD ("+IJ.d2s(beforeXY[0],3)+"/"+IJ.d2s(beforeXY[1],3)+")->"+
					IJ.d2s(centerXY[0],3)+"/"+IJ.d2s(centerXY[1],3));
		}
10144

10145 10146 10147
		if (debug_level > (debug_threshold - 0))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];
10148

10149 10150
		if (debug_level > (debug_threshold + 0)) System.out.println(">---correctedPatternCrossLocation: before x="+IJ.d2s(beforeXY[0],3)+" y="+IJ.d2s(beforeXY[1],3));
		if (debug_level > (debug_threshold + 0)) System.out.println(">+++correctedPatternCrossLocation: after  x="+IJ.d2s(result[0],3)+" y="+IJ.d2s(result[1],3));
10151

10152 10153
		return result;
	}
10154 10155


10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191
	/* ======= Debugging only - returns 2-d array of x,y as a function of initial estimation =================== */
	public  double [][][] scanPatternCrossLocation(
			double range, // size of the scanning square
			int    size,  // number of scan points in each direction (total size*size)
			double [] beforeCenterXY, // initial coordinates of the pattern cross point
			double wv0x,
			double wv0y,
			double wv1x,
			double wv1y,
			ImagePlus imp,      // image data (Bayer mosaic)
			DistortionParameters distortionParameters, //
			MatchSimulatedPattern.PatternDetectParameters patternDetectParameters,
			MatchSimulatedPattern matchSimulatedPattern, // correlationSize
			SimulationPattern.SimulParameters  thisSimulParameters,
			boolean equalizeGreens,
			double [] window,   // window function
			SimulationPattern simulationPattern,
			boolean negative, // invert cross phase
			DoubleFHT fht_instance
			){
		//		   	double [] result=beforeXY.clone();
		double [][][] result=new double [size][size][4];
		if (fht_instance==null) fht_instance=new DoubleFHT(); // move upstream to reduce number of initializations
		double [] beforeXY=new double[2];
		double [] filter=fht_instance.createFrequencyFilter(
				new double [distortionParameters.correlationSize*distortionParameters.correlationSize], //distortionParameters.correlationSize,
				distortionParameters.correlationHighPassSigma,
				distortionParameters.correlationLowPassSigma);
		if (debugLevel>2){
			double [] maskFull = new double [distortionParameters.correlationSize*distortionParameters.correlationSize];
			for (int i=0;i<maskFull.length;i++) {
				if (i<filter.length) maskFull[i]=filter[i];
				else {
					int rowMod = (distortionParameters.correlationSize - (i/distortionParameters.correlationSize)) % distortionParameters.correlationSize;
					int colMod = (distortionParameters.correlationSize - (i%distortionParameters.correlationSize)) % distortionParameters.correlationSize;
					maskFull[i]=filter[rowMod*distortionParameters.correlationSize+colMod];
Andrey Filippov's avatar
Andrey Filippov committed
10192
				}
10193

Andrey Filippov's avatar
Andrey Filippov committed
10194
			}
10195 10196
			SDFA_INSTANCE.showArrays(maskFull, "filter");
		}
10197 10198


10199 10200 10201 10202 10203 10204
		for (int i=0;i<size;i++) for (int j=0;j<size;j++) {
			beforeXY[1]=beforeCenterXY[1]-range/2+ (range*i)/(size-1);
			beforeXY[0]=beforeCenterXY[0]-range/2+ (range*j)/(size-1);
			Rectangle centerCross=correlationSelection(
					beforeXY, // initial coordinates of the pattern cross point
					distortionParameters.correlationSize);
Andrey Filippov's avatar
Andrey Filippov committed
10205

10206 10207 10208
			int ixc=centerCross.x+centerCross.width/2;
			int iyc=centerCross.y+centerCross.height/2;
			double [] diffBeforeXY={beforeXY[0]-ixc, beforeXY[1]-iyc};
Andrey Filippov's avatar
Andrey Filippov committed
10209

10210 10211 10212
			//create diagonal green selection around ixc,iyc
			double[][] input_bayer=splitBayer (imp,centerCross,equalizeGreens);
			/*
Andrey Filippov's avatar
Andrey Filippov committed
10213 10214 10215 10216 10217 10218 10219
			 double[][] corrWindow=null;
			 if (distortionParameters.correlationRadiusScale>=0.0) {
				 corrWindow=generateWeights (
						 distortionParameters.correlationWeightSigma,
						 distortionParameters.correlationRadiusScale); //  if 0 - use sigma as radius, inside - 1.0, outside 0.0. If >0 - size of array n*sigma

			 }
10220

10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291
			 */
			if (debugLevel>3) SDFA_INSTANCE.showArrays(input_bayer,  true, "centered");
			if (debugLevel>1) System.out.println(i+"/"+j+": ixc="+ixc+" iyc="+iyc);
			// alternative way to  generate shifted pattern
			double [][]wv={{wv0x, wv0y},
					{wv1x, wv1y}};
			double [] dUV=matrix2x2_scale(matrix2x2_mul(wv,diffBeforeXY),-2*Math.PI);

			//correlationHighPassSigma
			double [] greens=normalizeAndWindow (input_bayer[4], window);
			simulationPattern.simulatePatternFullPattern(
					wv0x,
					wv0y,
					dUV[0]+(negative?(-Math.PI/2):Math.PI/2),
					wv1x,
					wv1y,
					dUV[1]+Math.PI/2, //0.0,
					null, // no mesh distortion here
					thisSimulParameters.subdiv,// SIMUL.subdiv, - do not need high quality here
					distortionParameters.correlationSize,
					true, // center for greens
					false);//boolean mono

			double[][] sim_pix= simulationPattern.extractSimulPatterns (
					thisSimulParameters,
					1,       // subdivide output pixels
					distortionParameters.correlationSize,    // number of Bayer cells in width of the square selection (half number of pixels)
					0.0, //-diffBeforeXY[0],
					0.0); //-diffBeforeXY[1]);

			double [] simGreensCentered= normalizeAndWindow (sim_pix[4], window);
			if (debugLevel>2) SDFA_INSTANCE.showArrays(greens.clone(), "greens-i"+i+"-j"+j);
			if (debugLevel>2) SDFA_INSTANCE.showArrays(simGreensCentered.clone(), "simGreensCentered-i"+i+"-j"+j);

			double [] modelCorr=fht_instance.correlate (greens,  // measured pixel array
					simGreensCentered,  // simulated (model) pixel array)
					//                     distortionParameters.correlationHighPassSigma);
					filter);
			if (debugLevel>2) SDFA_INSTANCE.showArrays(modelCorr.clone(), "modelCorr-i"+i+"-j"+j);
			double [] xyCorr=new double[2];
			double [] centerXY;
			//			 if (distortionParameters.correlationRadiusScale>=0.0)  centerXY= correlationMaximum(modelCorr,corrWindow);
			if (distortionParameters.correlationRadius>0){
				centerXY= correlationMaximum(modelCorr,
						distortionParameters.correlationRadius,
						distortionParameters.correlationThreshold,
						distortionParameters.correlationSubdiv,
						distortionParameters.correlationFFTSubdiv,
						fht_instance,
						distortionParameters.correlationMaxOffset,
						0.0, // low-pass filtering already done
						(debugLevel>2)
						);
			} 	 else centerXY= correlationMaximum(modelCorr,distortionParameters.correlationMaxOffset,(debugLevel>2));
			if (centerXY==null) {
				centerXY=new double[2];
				centerXY[0]=0.0;
				centerXY[1]=0.0;
			}
			if (debugLevel>2) System.out.println("correctedPatternCrossLocation: Center x="+IJ.d2s(centerXY[0],3)+" y="+ 	IJ.d2s(centerXY[1],3));
			xyCorr[0]=-centerXY[0]-centerXY[1];
			xyCorr[1]= centerXY[0]-centerXY[1];
			if (debugLevel>1) System.out.println("correctedPatternCrossLocation: "+i+"/"+j+": dist="+IJ.d2s(Math.sqrt(xyCorr[0]*xyCorr[0]+xyCorr[1]*xyCorr[1]),4)+" xyCorr[0]="+IJ.d2s(xyCorr[0],4)+" xyCorr[1]="+ 	IJ.d2s(xyCorr[1],4));
			//			 result[0]=ixc-xyCorr[0];
			//			 result[1]=iyc-xyCorr[1];
			result[i][j][0]=ixc-xyCorr[0]+diffBeforeXY[0];
			result[i][j][1]=iyc-xyCorr[1]+diffBeforeXY[1];
			result[i][j][2]=beforeXY[0];
			result[i][j][3]=beforeXY[1];
			if (debugLevel>1) System.out.println("---correctedPatternCrossLocation: "+i+"/"+j+" before x="+IJ.d2s(beforeXY[0],3)+" y="+IJ.d2s(beforeXY[1],3));
			if (debugLevel>1) System.out.println("+++correctedPatternCrossLocation: "+i+"/"+j+" after  x="+IJ.d2s(result[i][j][0],3)+" y="+IJ.d2s(result[i][j][1],3));
Andrey Filippov's avatar
Andrey Filippov committed
10292
		}
10293 10294 10295
		return result;
	}
	/*
Andrey Filippov's avatar
Andrey Filippov committed
10296 10297
			distortionParameters.correlationWeightSigma=  gd.getNextNumber();
			distortionParameters.correlationRadiusScale=  gd.getNextNumber();
10298

10299
	 */
Andrey Filippov's avatar
Andrey Filippov committed
10300

10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346
	/* ======================================================================== */
	/**
	 * Interpolate maximum on a square correlation array, return vector from the center
	 */
	// one quater of the weights function to be used to approximate maximum on correlation by a second-degree polynominal
	public double [][] generateWeights (double sigma,
			double n) { //  if 0 - use sigma as radius, inside - 1.0, outside 0.0. If >0 - size of array n*sigma
		double r0=((n>0)?n:1.0)*sigma;
		double r2=r0*r0;
		int size = (int)  Math.ceil(r0);
		double [][] mask=new double [size][size];
		int i,j;
		double [] gaussian=new double [size];
		if (n>0) {
			double k=0.5/sigma/sigma;
			for (i=0;i<size;i++) gaussian[i]=Math.exp(-(k*i*i));
		}
		for (i=0;i<size;i++) for (j=0;j<size;j++){
			if ((i*i+j*j)>r2) mask[i][j]=0.0;
			else if (n>0)     mask[i][j]=gaussian[i]*gaussian[j];
			else              mask[i][j]=1.0;
		}
		return mask;
	}
	public double [] correlationMaximum(
			double [] corr,   // square (correlation) array to find location of the maximum ([0.0,0.0] in the center of the arrray)
			double [][] weights,
			double maxOffset) {
		if ((corr==null) || (corr.length==0)) return null;
		//  	   		double [] corrMax= new double[2];
		int size= (int) Math.sqrt(corr.length);
		int i,j,imax=0,ix,iy, ixc, iyc;
		double max=corr[0];
		for (i=1;i<corr.length;i++) if (max<corr[i]) {
			max=corr[i];
			imax=i;
		}
		iyc=imax/size;
		ixc=imax%size;
		int ixc0=ixc-size/2;
		int iyc0=iyc-size/2;
		if ((maxOffset>0) && (maxOffset*maxOffset<(ixc0*ixc0+iyc0*iyc0))) {
			if (debugLevel>1) System.out.println("Too far from the center1: ixc="+ixc+" iyc="+iyc);
			return null;
		}
		if (debugLevel>1) System.out.println("correlationMaximum: ixc="+ixc+" iyc="+iyc);
10347 10348


10349 10350
		/* ix, iy - the location of the point with maximal value. We'll approximate the vicinity of that maximum using a
		 * second degree polunominal:
Andrey Filippov's avatar
Andrey Filippov committed
10351
   Z(x,y)~=A*x^2+B*y^2+C*x*y+D*x+E*y+F
10352
   by minimizing sum of squared differences between the actual (Z(x,uy)) and approximated values.
Andrey Filippov's avatar
Andrey Filippov committed
10353
   and then find the maximum on the approximated surface. Here is the math:
10354

Andrey Filippov's avatar
Andrey Filippov committed
10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442
Z(x,y)~=A*x^2+B*y^2+C*x*y+D*x+E*y+F
minimizing squared error, using W(x,y) as weight function

error=Sum(W(x,y)*((A*x^2+B*y^2+C*x*y+D*x+E*y+F)-Z(x,y))^2)

error=Sum(W(x,y)*(A^2*x^4 + 2*A*x^2*(B*y^2+C*x*y+D*x+E*y+F-Z(x,y)) +(...) )
0=derror/dA=Sum(W(x,y)*(2*A*x^4 + 2*x^2*(B*y^2+C*x*y+D*x+E*y+F-Z(x,y)))
0=Sum(W(x,y)*(A*x^4 + x^2*(B*y^2+C*x*y+D*x+E*y+F-Z(x,y)))

SX4=Sum(W(x,y)*x^4), etc

(1) 0=A*SX4 + B*SX2Y2 + C*SX3Y +D*SX3 +E*SX2Y +F*SX2 - SZX2

derror/dB:

error=Sum(W(x,y)*(B^2*y^4 + 2*B*y^2*(A*x^2+C*x*y+D*x+E*y+F-Z(x,y)) +(...) )
0=derror/dB=Sum(W(x,y)*(2*B*y^4 + 2*y^2*(A*x^2+C*x*y+D*x+E*y+F-Z(x,y)))
0=Sum(W(x,y)*(B*y^4 + y^2*(A*x^2+C*x*y+D*x+E*y+F-Z(x,y)))

(2) 0=B*SY4 + A*SX2Y2 + C*SXY3 +D*SXY2 +E*SY3 +F*SY2 - SZY2
(2) 0=A*SX2Y2 + B*SY4 + C*SXY3 +D*SXY2 +E*SY3 +F*SY2 - SZY2

derror/dC:

error=Sum(W(x,y)*(C^2*x^2*y^2 + 2*C*x*y*(A*x^2+B*y^2+D*x+E*y+F-Z(x,y)) +(...) )
0=derror/dC=Sum(W(x,y)*(2*C*x^2*y^2 + 2*x*y*(A*x^2+B*y^2+D*x+E*y+F-Z(x,y)) )
0=Sum(W(x,y)*(C*x^2*y^2 + x*y*(A*x^2+B*y^2+D*x+E*y+F-Z(x,y)) )

(3) 0= A*SX3Y +  B*SXY3 +  C*SX2Y2 + D*SX2Y + E*SXY2 + F*SXY - SZXY

derror/dD:

error=Sum(W(x,y)*(D^2*x^2 + 2*D*x*(A*x^2+B*y^2+C*x*y+E*y+F-Z(x,y)) +(...) )
0=derror/dD=Sum(W(x,y)*(2*D*x^2 + 2*x*(A*x^2+B*y^2+C*x*y+E*y+F-Z(x,y)) )
0=Sum(W(x,y)*(D*x^2 + x*(A*x^2+B*y^2+C*x*y+E*y+F-Z(x,y)) )

(4) 0= A*SX3 +   B*SXY2 +  C*SX2Y + D*SX2  + E*SXY +  F*SX  - SZX

derror/dE:

error=Sum(W(x,y)*(E^2*y^2 + 2*E*y*(A*x^2+B*y^2+C*x*y+D*x+F-Z(x,y)) +(...) )
0=derror/dE=Sum(W(x,y)*(2*E*y^2 + 2*y*(A*x^2+B*y^2+C*x*y+D*x+F-Z(x,y)) )
0=Sum(W(x,y)*(E*y^2 + y*(A*x^2+B*y^2+C*x*y+D*x+F-Z(x,y)) )
(5) 0= A*SX2Y +  B*SY3 +   C*SXY2 + D*SXY +  E*SY2  + F*SY  - SZY

derror/dF:

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)) +(...) )
0=derror/dF=Sum(W(x,y)*(2*F +  2*(A*x^2+B*y^2+C*x*y+D*x+E*y-Z(x,y)) )
0=Sum(W(x,y)*(F +  (A*x^2+B*y^2+C*x*y+D*x+E*y-Z(x,y)) )
(6) 0= A*SX2 +   B*SY2 +   C*SXY +  D*SX +   E*SY   + F*S   - SZ




(1) 0= A*SX4 +   B*SX2Y2 + C*SX3Y +  D*SX3 +  E*SX2Y + F*SX2 - SZX2
(2) 0= A*SX2Y2 + B*SY4 +   C*SXY3 +  D*SXY2 + E*SY3  + F*SY2 - SZY2
(3) 0= A*SX3Y +  B*SXY3 +  C*SX2Y2 + D*SX2Y + E*SXY2 + F*SXY - SZXY
(4) 0= A*SX3 +   B*SXY2 +  C*SX2Y +  D*SX2  + E*SXY  + F*SX  - SZX
(5) 0= A*SX2Y +  B*SY3 +   C*SXY2 +  D*SXY +  E*SY2  + F*SY  - SZY
(6) 0= A*SX2 +   B*SY2 +   C*SXY +   D*SX +   E*SY   + F*S   - SZ


(1) 0= A*S40 + B*S22 + C*S31 + D*S30 + E*S21 + F*S20 - SZ20
(2) 0= A*S22 + B*S04 + C*S13 + D*S12 + E*S03 + F*S02 - SZ02
(3) 0= A*S31 + B*S13 + C*S22 + D*S21 + E*S12 + F*S11 - SZ11
(4) 0= A*S30 + B*S12 + C*S21 + D*S20 + E*S11 + F*S10 - SZ10
(5) 0= A*S21 + B*S03 + C*S12 + D*S11 + E*S02 + F*S01 - SZ01
(6) 0= A*S20 + B*S02 + C*S11 + D*S10 + E*S01 + F*S00 - SZ00


we beed x,y of maximum, so
d(A*x^2+B*y^2+C*x*y+D*x+E*y+F)/dx=0
d(A*x^2+B*y^2+C*x*y+D*x+E*y+F)/dy=0
d()/dx=2*A*x+C*y+D=0
d()/dy=C*x+2*B*y+E=0


  | S40 S22 S31 S30 S21 S20 |   | A |   | SZ20 |
  | S22 S04 S13 S12 S03 S02 |   | B |   | SZ02 |
  | S31 S13 S22 S21 S12 S11 |   | C |   | SZ11 |
  | S30 S12 S21 S20 S11 S10 | * | D | = | SZ10 |
  | S21 S03 S12 S11 S02 S01 |   | E |   | SZ01 |
  | S20 S02 S11 S10 S01 S00 |   | F |   | SZ00 |


  | 2*A    C | * | x | = | -D |
  |   C  2*B |   | Y |   | -E |
10443

10444
		 */
10445

10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494
		double S00=0.0,
				S10=0.0,S01=0.0,
				S20=0.0,S11=0.0,S02=0.0,
				S30=0.0,S21=0.0,S12=0.0,S03=0.0,
				S40=0.0,S31=0.0,S22=0.0,S13=0.0,S04=0.0,
				SZ00=0.0,
				SZ10=0.0,SZ01=0.0,
				SZ20=0.0,SZ11=0.0,SZ02=0.0;
		int wsize=weights.length;
		double w,z,x,x2,x3,x4,y,y2,y3,y4,wz;
		for (i=iyc-wsize+1;i<iyc+wsize;i++) if ((i>0) && (i<size)) for (j=ixc-wsize+1;j<ixc+wsize;j++) if ((j>0) && (j<size)) {
			iy=i-iyc;
			ix=j-ixc;
			w=weights[(iy>=0)?iy:-iy][(ix>=0)?ix:-ix];
			if (w>0) {
				z=corr[i*size+j];
				wz=w*z;
				x=ix;
				x2=x*x;
				x3=x2*x;
				x4=x3*x;
				y=iy;
				y2=y*y;
				y3=y2*y;
				y4=y3*y;
				S00+=w;
				S10+=w*x;
				S01+=w*y;
				S20+=w*x2;
				S11+=w*x*y;
				S02+=w*y2;
				S30+=w*x3;
				S21+=w*x2*y;
				S12+=w*x*y2;
				S03+=w*y3;
				S40+=w*x4;
				S31+=w*x3*y;
				S22+=w*x2*y2;
				S13+=w*x*y3;
				S04+=w*y4;
				SZ00+=wz;
				SZ10+=wz*x;
				SZ01+=wz*y;
				SZ20+=wz*x2;
				SZ11+=wz*x*y;
				SZ02+=wz*y2;
			}
		}
		/*
Andrey Filippov's avatar
Andrey Filippov committed
10495 10496 10497 10498 10499 10500 10501
  | S40 S22 S31 S30 S21 S20 |   | A |   | SZ20 |
  | S22 S04 S13 S12 S03 S02 |   | B |   | SZ02 |
  | S31 S13 S22 S21 S12 S11 |   | C |   | SZ11 |
  | S30 S12 S21 S20 S11 S10 | * | D | = | SZ10 |
  | S21 S03 S12 S11 S02 S01 |   | E |   | SZ01 |
  | S20 S02 S11 S10 S01 S00 |   | F |   | SZ00 |

10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515
		 */
		double [][] mAarray= {
				{S40,S22,S31,S30,S21,S20},
				{S22,S04,S13,S12,S03,S02},
				{S31,S13,S22,S21,S12,S11},
				{S30,S12,S21,S20,S11,S10},
				{S21,S03,S12,S11,S02,S01},
				{S20,S02,S11,S10,S01,S00}};

		double [] zAarray={SZ20,SZ02,SZ11,SZ10,SZ01,SZ00};
		Matrix M=new Matrix (mAarray);
		Matrix Z=new Matrix (zAarray,6);
		double [] ABCDEF= M.solve(Z).getRowPackedCopy();
		/*
Andrey Filippov's avatar
Andrey Filippov committed
10516 10517
  | 2*A    C | * | x | = | -D |
  |   C  2*B |   | Y |   | -E |
10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596
		 */
		double [][] mXYarray= {{2*ABCDEF[0],ABCDEF[2]},{ABCDEF[2],2*ABCDEF[1]}};
		double []   mDEarray={-ABCDEF[3],-ABCDEF[4]};

		Matrix mXY=new Matrix (mXYarray);
		Matrix mDE= new Matrix (mDEarray,2);
		double [] corrMax= mXY.solve(mDE).getRowPackedCopy();
		if (debugLevel>1) System.out.println("correlationMaximum: ixc="+ixc+" iyc="+iyc+" corrMax[0]="+corrMax[0]+" corrMax[1]="+corrMax[1]);
		corrMax[0]+=ixc-size/2;
		corrMax[1]+=iyc-size/2;

		if (debugLevel>2){
			double [] approx=new double [size*size];
			for (i=0;i<approx.length;i++) approx[i]=0.0;
			for (i=iyc-wsize+1;i<iyc+wsize;i++) if ((i>0) && (i<size)) for (j=ixc-wsize+1;j<ixc+wsize;j++) if ((j>0) && (j<size)) {
				iy=i-iyc;
				ix=j-ixc;
				x=ix;
				y=iy;

				//  	   				z=corr[i*size+j];
				approx[i*size+j]=ABCDEF[0]*x*x+
						ABCDEF[1]*y*y+
						ABCDEF[2]*x*y+
						ABCDEF[3]*x+
						ABCDEF[4]*y+
						ABCDEF[5];
			}
			double [][] both= new double[2][];
			both[0]=corr;
			both[1]=approx;
			//corr
			SDFA_INSTANCE.showArrays(both, true, "corr-approx"); // stack
		}
		//  	   		if (debugLevel>2) System.out.println("correlationMaximum: ix="+ix+" iy="+iy);
		//  	   		if (debugLevel>2) System.out.println("correlationMaximum: maxInHor[0] ="+maxInHor[0]+ " maxInHor[1]= "+maxInHor[1]+ " maxInHor[2]= "+maxInHor[2]);
		//  	   		if (debugLevel>2) System.out.println("correlationMaximum: maxInVert[0]="+maxInVert[0]+" maxInVert[1]="+maxInVert[1]+" maxInVert[2]="+maxInVert[2]);
		return corrMax;
	}


	private double [] correlationMaximum(
			double [] corr,
			int dist,       // maximal distance from the maximum to consider
			double threshold, // fraction of maximum (slightly less than 1.0) to limit the top part of the maximum for centroid
			int decimate, // interpolate to finer grid (both FFT and linear)
			int decimateFFT, // should be power of 2
			DoubleFHT fht_instance,
			double maxOffset,
			double lowpass, // relative to original corr size (will be scaled for decimation). Will only be applied if decimateFFT >1!
			boolean showDebug
			){
		if ((corr==null) || (corr.length==0)) return null;
		int size= (int) Math.sqrt(corr.length);
		int i,j,imax=0,ixc,iyc,index;
		//	   	   	if (showDebug) System.out.println("correlationMaximum(), decimateFFT="+decimateFFT);
		/**
		 * Reduces size of the correlation area (using center part) and simultaneously interpolating pixels, so the result is a
		 * scaled version of the center (total FFT suize remains the same)
		 */
		if (showDebug){
			System.out.println("correlationMaximum(): decimate="+decimate+" decimateFFT="+decimateFFT);
		}
		if (decimateFFT>1){
			if (fht_instance==null) fht_instance=new DoubleFHT();
			double scale=decimateFFT*decimateFFT;
			double [] corr1=new double [corr.length];
			/**
			 * As we are interested only in the center part of the image, we'll use flat-top
			 * window based on Hamming.
			 */

			int size1=size/decimateFFT;
			for (i=0;i<corr1.length;i++) corr1[i]=0.0;
			double borderAverage=0;
			index=size*(size+1)*(decimateFFT-1)/decimateFFT/2;
			int i0=index,i1=index+size1,i2=i1+size1*size,i3=i2-size1; // on right and bottom edge goes 1 pixel outside of the used area
			for (i=0;i<size1;i++)	{
				/*	   	   				if (showDebug){
Andrey Filippov's avatar
Andrey Filippov committed
10597 10598
	   	   					System.out.println(":: size="+size+" size1="+size1+ " i="+i+" i0="+i0+" i1="+i1+" i2="+i2+" i3="+i3+" scale="+scale);
	   	   				}
10599 10600
				 */
				borderAverage+=corr[i0++]+corr[i1+=size1]+corr[i2--]+corr[i3-=size1];
Andrey Filippov's avatar
Andrey Filippov committed
10601

10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623
			}
			borderAverage/=4*size1;
			double [] preHammingMod=fht_instance.getHamming1d(size1/2);
			double [] hammingMod=new double [size1];
			for (i=0;i<size1/4;i++) hammingMod[i]=preHammingMod[i];
			for (i=1;i<size1/4;i++) hammingMod[size1-i]=preHammingMod[i];
			for (i=size1/4;i<=(size1-size1/4);i++) hammingMod[i]=1.0;

			if (showDebug) System.out.println("scale="+scale+ " borderAverage="+borderAverage);
			for (i=0;i<size1;i++) for (j=0;j<size1;j++) {
				corr1[(i*size+j)*decimateFFT] = scale*(corr[index+i*size+j]-borderAverage)*hammingMod[i]*hammingMod[j]+borderAverage;
			}
			if (showDebug) SDFA_INSTANCE.showArrays(corr1.clone(), "decimatedForFFT");
			fht_instance.swapQuadrants(corr1);
			if (!fht_instance.transform(corr1,false)) return null; // direct FHT
			if (showDebug) SDFA_INSTANCE.showArrays(corr1.clone(), "FFT");
			// zero out aliases
			for (i=0; i<=size1/2;i++) for (j=size1/2+1;j<size -(size1/2);j++) corr1[i*size+j]=0.0;
			for (i=size1/2+1;i<size -(size1/2);i++) for (j=0;j<size;j++) corr1[i*size+j]=0.0;
			for (i=size -(size1/2); i<size;i++) for (j=size1/2+1;j<size -(size1/2);j++) corr1[i*size+j]=0.0;
			// apply window for now - just
			/*
10624 10625 10626
	    	    	if (showDebug) {
	    	    		System.out.println("Getting hamming1d ("+size1+")");
	    	    	}
10627 10628 10629
			 */
			double [] hamming=fht_instance.getHamming1d(size1).clone();
			/*
10630
	    	    	if (showDebug) {
10631
	    	    		for (i=0;i<hamming.length;i++) System.out.println("hamming["+i+"]="+hamming[i]);
10632 10633
	    	    	}
	   	   			if (showDebug) SDFA_INSTANCE.showArrays(corr1.clone(), "NO_ALIAS");
Andrey Filippov's avatar
Andrey Filippov committed
10634 10635 10636 10637
	   	   			// Combine with low-pass Gaussian (if it is >0)
	   	   			if (lowpass>0){
	   	   				double [] gaussian1d=fht_instance.getGaussian1d(lowpass,size1); // no need to divide by /decimateFFT as we use size1, not size
	   	   				for (i=0;i<hammingMod.length;i++) hamming[i]*=gaussian1d[i];
10638 10639
		    	    	if (showDebug) {
		    	    		System.out.println("lowpass="+lowpass);
10640
		    	    		for (i=0;i<gaussian1d.length;i++) System.out.println("gaussian1d["+i+"]="+gaussian1d[i]);
10641
		    	    	}
Andrey Filippov's avatar
Andrey Filippov committed
10642 10643
	   	   			}

10644
	    	    	if (showDebug) {
10645
	    	    		for (i=0;i<hamming.length;i++) System.out.println("hamming["+i+"]="+hamming[i]);
10646
	    	    	}
10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729
			 */
			int halfSize1=size1/2, shiftZero=size-halfSize1;
			for (i=0;i<=size1;i++) for (j=0;j<=size1;j++){
				int im=i%size1,jm=j%size1;
				corr1[((i+shiftZero)%size)*size+((j+shiftZero)%size)]*=hamming[im]*hamming[jm];
			}


			if (showDebug) SDFA_INSTANCE.showArrays(corr1.clone(), "FFT-masked");
			if (!fht_instance.transform(corr1,true)) return null; // inverse FHT
			fht_instance.swapQuadrants(corr1);
			if (showDebug) SDFA_INSTANCE.showArrays(corr1.clone(), "decimatedAfterFFT");
			dist*=decimateFFT;
			maxOffset*=decimateFFT;
			decimate/=decimateFFT;
			corr=corr1; // replace
		}


		double max=corr[0];
		for (i=1;i<corr.length;i++) if (max<corr[i]) {
			max=corr[i];
			imax=i;
		}
		iyc=imax/size;
		ixc=imax%size;
		int ixc0=ixc-size/2;
		int iyc0=iyc-size/2;
		if ((maxOffset>0) && (maxOffset*maxOffset<(ixc0*ixc0+iyc0*iyc0))) {
			if (showDebug || (debugLevel>1)) System.out.println("Too far from the center2: ixc="+ixc+" iyc="+iyc+" ixc0="+ixc0+" iyc0="+iyc0+" maxOffset="+maxOffset);
			//					if (showDebug || (debugLevel>0)) System.out.println("Too far from the center2: ixc="+ixc+" iyc="+iyc+" ixc0="+ixc0+" iyc0="+iyc0+" maxOffset="+maxOffset);
			return null;
		}
		if (showDebug || (debugLevel>1)) System.out.println("correlationMaximum: ixc="+ixc+" iyc="+iyc+" ixc0="+ixc0+" iyc0="+iyc0+" maxOffset="+maxOffset);

		// reduce dist if it hits borders
		if (dist>iyc) dist=iyc;
		if (dist>ixc) dist=ixc;
		if (dist>(size-iyc-1)) dist=(size-iyc-1);
		if (dist>(size-ixc-1)) dist=(size-ixc-1);
		int interpSize=2*dist*decimate+1;
		double [][] cell=new double[2][2];
		double [] row = new double [2];
		double [] ki= new double [2];
		double kj;
		double [] interpCorr=new double[interpSize*interpSize];
		int i1,j1;
		int i1Range,j1Range;
		for (i=0;i<2*dist;i++) for (j=0;j<2*dist;j++) {
			index=(iyc-dist+i)*size+(ixc-dist+j);
			cell[0][0]= corr[index];
			cell[0][1]= corr[index+1];
			cell[1][0]= corr[index+size];
			cell[1][1]= corr[index+size+1];
			i1Range=decimate+((i==(2*dist-1))?1:0);
			j1Range=decimate+((j==(2*dist-1))?1:0);
			ki[0]=(cell[1][0]-cell[0][0])/decimate;
			ki[1]=(cell[1][1]-cell[0][1])/decimate;

			for (i1=0;i1<i1Range;i1++){
				row[0]=cell[0][0]+ki[0]*i1;
				row[1]=cell[0][1]+ki[1]*i1;
				kj=(row[1]-row[0])/decimate;
				for (j1=0;j1<j1Range;j1++) {
					interpCorr[(i*decimate+i1)*interpSize+(j*decimate+j1)]=row[0]+kj*j1;
				}
			}
		}
		// Gaussian blur the after linear interpolation, use sigma = 0.75* decimate ?
		// now find the maximal value on the border - it will be a threshold for a wave from the center
		double interpolationBlurSigma=0.75* decimate;
		DoubleGaussianBlur gb=new DoubleGaussianBlur();
		gb.blurDouble(interpCorr, interpSize, interpSize, interpolationBlurSigma, interpolationBlurSigma, 0.01);

		double limit=interpCorr[0];
		for (i=0;i<interpSize;i++) {
			if (limit<interpCorr[i]) limit=interpCorr[i];
			if (limit<interpCorr[interpSize*interpSize-i-1])   limit=interpCorr[interpSize*interpSize-i-1];
			if (limit<interpCorr[interpSize*i])                limit=interpCorr[interpSize*i];
			if (limit<interpCorr[interpSize*i+(interpSize-1)]) limit=interpCorr[interpSize*i+(interpSize-1)];
		}
		// Now modify the limit if it is below threshold*max (sharp maximum)
		if (limit <threshold*max) limit =threshold*max;
10730

10731
		// run wave from the center, border pixels <=limit, so no need to verify array limits
10732

10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783
		List <Integer> pixelList=new ArrayList<Integer>(100);
		Integer Index, newIndex;
		int []clusterMap=new int[interpSize*interpSize];
		for (i=0;i<clusterMap.length;i++) clusterMap[i]=0;
		int [] dirs={-1,-interpSize-1,-interpSize,-interpSize+1,1,interpSize+1,interpSize,interpSize-1};
		Index=dist*decimate*(interpSize+1); // center
		pixelList.clear();
		pixelList.add (Index);
		if (showDebug || (debugLevel>1)) System.out.println("correlationMaximum: pixelList.add ("+Index+ "), i="+(Index/interpSize)+ " j= "+(Index%interpSize));
		clusterMap[Index]=1;
		while (pixelList.size()>0) {
			Index=pixelList.remove(0);
			for (i=0;i<dirs.length;i++) {
				newIndex=Index+dirs[i];
				if ((clusterMap[newIndex]==0) && (interpCorr[newIndex]>limit)){
					pixelList.add (newIndex);
					clusterMap[newIndex]=1;
				}
			}
		}
		// Calculate centroid
		double s=0.0,sx=0.0, sy=0.0,x,y,d;
		if (showDebug || (debugLevel>1)) System.out.println("correlationMaximum: dist ="+dist+ " decimate= "+decimate+ " interpSize= "+interpSize);
		for (i=0;i<clusterMap.length;i++) if (clusterMap[i]>0){
			x=(i%interpSize-dist*decimate);
			y=(i / interpSize-dist*decimate);
			d=interpCorr[i]-limit;
			s+=d;
			sx+=x*d;
			sy+=y*d;
		}
		double [] corrXY={sx/s/decimate+ixc-size/2,sy/s/decimate+iyc-size/2};
		if (showDebug || (debugLevel>1)) System.out.println("correlationMaximum: s ="+s+ " sx= "+sx+ " sy= "+sy);
		if (showDebug || (debugLevel>1)) System.out.println("correlationMaximum: sx/s/decimate ="+(sx/s/decimate)+ " sy/s/decimate= "+(sy/s/decimate));
		if (showDebug || (debugLevel>1)) System.out.println("correlationMaximum: dx="+IJ.d2s(corrXY[0],3)+" dy="+IJ.d2s(corrXY[1],3));

		//			    if ((debugLevel>1) && (showDebug)) {
		if (showDebug) {
			double [] decimatedMasked=interpCorr.clone();
			for (i=0;i<decimatedMasked.length;i++) {
				if (clusterMap[i]==0) decimatedMasked[i]=limit;
			}
			double [][] both={interpCorr,decimatedMasked};
			SDFA_INSTANCE.showArrays(both, true, "centerCorr");
		}
		if (decimateFFT>1) {
			corrXY[0]/=decimateFFT;
			corrXY[1]/=decimateFFT;
		}
		return corrXY;
	}
10784

10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799
	private double [] correlationMaximum(
			double [] corr,
			double maxOffset,
			boolean showDebug) {
		if ((corr==null) || (corr.length==0)) return null;
		double [] corrMax= new double[2];
		int size= (int) Math.sqrt(corr.length);
		int i,imax=0,ix,iy;
		double max=corr[0];
		for (i=1;i<corr.length;i++) if (max<corr[i]) {
			max=corr[i];
			imax=i;
		}
		iy=imax/size;
		ix=imax%size;
10800

10801 10802 10803 10804 10805 10806
		corrMax[0]=ix-size/2;
		corrMax[1]=iy-size/2;
		if ((maxOffset>0) && (maxOffset*maxOffset<(corrMax[0]*corrMax[0]+corrMax[1]*corrMax[1]))) {
			if (debugLevel>1) System.out.println("Too far from the center3: corrMax[0]="+corrMax[0]+" corrMax[1]="+corrMax[1]);
			return null;
		}
10807

10808
		if ((ix==0) || (iy==0) || (ix==(size-1))  || (iy==(size-1))) return corrMax; // on the border - no interpolation;
Andrey Filippov's avatar
Andrey Filippov committed
10809

10810 10811 10812
		double [] maxInHor= new double[3]; // locations of interpolated maximums for each of 3 rows   (iy-1, iy, iy+1)
		double [] maxInVert=new double[3]; // locations of interpolated maximums for each of 3 columns(ix-1, ix, ix+1)
		if (debugLevel>2) System.out.println("correlationMaximum: ix="+ix+" iy="+iy);
Andrey Filippov's avatar
Andrey Filippov committed
10813

10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841
		for (i=0;i<3;i++) {
			maxInHor[i]= -0.5+(corr[imax+size*(i-1)]-corr[imax+size*(i-1)-1])/
					(2*corr[imax+size*(i-1)]-corr[imax+size*(i-1)-1]-corr[imax+size*(i-1)+1]);
			maxInVert[i]=-0.5+(corr[imax+(i-1)]-corr[imax+     (i-1)-size])/
					(2*corr[imax+ (i-1)]-corr[imax+(i-1)-size]-corr[imax+(i-1)+size]);
		}
		if (debugLevel>2) System.out.println("correlationMaximum: maxInHor[0] ="+maxInHor[0]+ " maxInHor[1]= "+maxInHor[1]+ " maxInHor[2]= "+maxInHor[2]);
		if (debugLevel>2) System.out.println("correlationMaximum: maxInVert[0]="+maxInVert[0]+" maxInVert[1]="+maxInVert[1]+" maxInVert[2]="+maxInVert[2]);
		int maxInHorIndex=0;
		int maxInVertIndex=0;
		if ((maxInHor[0] <maxInHor[1] ) && (maxInHor[0] <maxInHor[2]) ) maxInHorIndex=1;
		if ((maxInVert[0]<maxInVert[1]) && (maxInVert[0]<maxInVert[2])) maxInVertIndex=1;
		if (debugLevel>2) System.out.println("correlationMaximum: maxInHorIndex="+maxInHorIndex+" maxInVertIndex="+maxInVertIndex);
		/*
		 * y= (y0+x0(y1-y0))/(1-(x1-x0)(y1-y0))
		 * x= (x0+y0(x1-x0))/(1-(x1-x0)(y1-y0))
		 * d= (1-(x1-x0)(y1-y0))
		 * y= (y0+x0(y1-y0))/d
		 * x= (x0+y0(x1-x0))/d
		 */
		double d=1-(maxInHor[maxInHorIndex+1]-maxInHor[maxInHorIndex])*(maxInVert[maxInVertIndex+1]-maxInVert[maxInVertIndex]);
		corrMax[0]=(maxInHor [maxInHorIndex]+ maxInVert[maxInVertIndex]*(maxInHor [maxInHorIndex +1]-maxInHor [maxInHorIndex ]))/d;
		corrMax[1]=(maxInVert[maxInVertIndex]+maxInHor [maxInHorIndex ]*(maxInVert[maxInVertIndex+1]-maxInVert[maxInVertIndex]))/d;
		if (debugLevel>2) System.out.println("correlationMaximum: corrMax[0]="+corrMax[0]+" corrMax[1]="+corrMax[1]);
		corrMax[0]+=ix-size/2;
		corrMax[1]+=iy-size/2;
		return corrMax;
	}
10842 10843


10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932
	/* ======================================================================== */
	public Rectangle correlationSelection(
			double [] beforeXY, // initial coordinates of the pattern cross point
			int size){
		int ixc=2*((int) Math.round(beforeXY[0]/2));
		int iyc=2*((int) Math.round(beforeXY[1]/2));
		Rectangle centerCross=new Rectangle(ixc-size,
				iyc-size,
				2*size,2*size);
		return centerCross;
	}
	/* ======================================================================== */
	// Estimate center xy and wave vectors from the neigbors
	// returns {{x,y},{wv1x,wv1y},{wv2x,wv2y}}


	public double [][] estimateCell(
			double [][][][] grid,
			int [] uv0,
			double [][] weights, // quadrant of sample weights
			boolean useContrast, // do not use cells with undefined contrast
			boolean forceLinear,  // use linear approximation (instead of quadratic)
			double thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
			double thresholdQuad  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
			){
		int dist=weights.length-1;
		int size=dist*2+1;
		double [][][] samples0 = new double [size*size][3][];
		int index=0;
		int [] uv=new int[2];
		double w;
		int maxU=-dist-1,minU=dist+1,maxV=-dist-1,minV=dist+1,maxUpV=-2*dist-1,minUpV=2*dist+1,maxUmV=-2*dist-1,minUmV=2*dist+1;
		for (int iDv=-dist;iDv<=dist;iDv++) for (int iDu=-dist;iDu<=dist;iDu++) {
			uv[0]=uv0[0]+iDu;
			uv[1]=uv0[1]+iDv;
			if ((!useContrast && isCellDefined(grid,uv)) || isCellDefinedC(grid,uv)) {
				w=weights[(iDv>=0)?iDv:-iDv][(iDu>=0)?iDu:-iDu];
				if (w!=0.0){
					if (maxU<iDu) maxU=iDu;
					if (minU>iDu) minU=iDu;
					if (maxV<iDv) maxV=iDv;
					if (minV>iDv) minV=iDv;
					if (maxUpV<(iDu+iDv)) maxUpV= iDu+iDv;
					if (minUpV>(iDu+iDv)) minUpV= iDu+iDv;
					if (maxUmV<(iDu-iDv)) maxUmV= iDu-iDv;
					if (minUmV>(iDu-iDv)) minUmV= iDu-iDv;
					samples0[index][0]=new double[2];
					samples0[index][1]=new double[useContrast?3:2];
					samples0[index][2]=new double[1];
					samples0[index][2][0]=w;
					samples0[index][0][0]=iDu;
					samples0[index][0][1]=iDv;
					samples0[index][1][0]=grid[uv[1]][uv[0]][0][0];
					samples0[index][1][1]=grid[uv[1]][uv[0]][0][1];
					if (useContrast){
						samples0[index][1][2]=grid[uv[1]][uv[0]][0][2]; // contrast
					}
					index++;
				}
			}
		}
		if (debugLevel>3) System.out.println(" maxU-minU="+(maxU-minU)+" maxV-minV="+(maxV-minV));
		if (debugLevel>3) System.out.println(" maxUpV-minUpV="+(maxUpV-minUpV)+" maxUmV-minUmV="+(maxUmV-minUmV));

		int diameter=maxU-minU;
		if (diameter>(maxV-minV)) diameter= maxV-minV;
		diameter*=2;
		if (diameter>(maxUpV-minUpV)) diameter= (maxUpV-minUpV);
		if (diameter>(maxUmV-minUmV)) diameter= (maxUmV-minUmV);
		if (debugLevel>3) System.out.println(" diameter="+diameter+" number="+index);
		if (diameter<2) return null;

		double [][][] samples = new double [index][][];
		System.arraycopy(samples0, 0, samples, 0, index);
		double [][] estimatedCell= interpolateQuadraticWithWvAtZero(
				samples,   // see quadraticApproximation()
				forceLinear || (diameter<5),  // use linear approximation diameter <4 should be enough, 5 - just to be safe
				thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
				thresholdQuad);  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
		if ((estimatedCell==null) || (estimatedCell[0]==null) || useContrast) return estimatedCell;
		double contrast=Double.NaN;
		if (isCellDefined(grid,uv0)) {
			double [] xycOld=grid[uv0[1]][uv0[0]][0];
			if (xycOld.length>2) contrast=xycOld[2];
		}
		double [] xyc={estimatedCell[0][0],estimatedCell[0][1],contrast};
		estimatedCell[0]=xyc;
		return estimatedCell;
	}
Andrey Filippov's avatar
Andrey Filippov committed
10933

10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953
	public double [] interpolateQuadratic(
			double [] xy,         // coordinates for which the interpolation is needed
			double [][][] data,   // see quadraticApproximation()
			boolean forceLinear,  // use linear approximation
			double thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
			double thresholdQuad){  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
		double [][] coeff = new PolynomialApproximation(this.debugLevel).quadraticApproximation(
				data,
				forceLinear,  // use linear approximation
				thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
				thresholdQuad);
		if (coeff==null) return null;
		double [] result = new double [coeff.length];
		int offset=(coeff[0].length>3)?3:0;
		for (int i=0;i<coeff.length;i++) {
			result[i]=coeff[i][offset+0]*xy[0]+coeff[i][offset+1]*xy[1]+coeff[i][offset+2];
			if (offset>0) result[i]+=coeff[i][0]*xy[0]*xy[0]+coeff[i][1]*xy[1]*xy[1]+coeff[i][2]*xy[0]*xy[1];
		}
		return result;
	}
10954

10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034
	// returns {{x,y},{wv1x,wv1y},{wv2x,wv2y}}
	public double [][] interpolateQuadraticWithWvAtZero(
			double [][][] data,   // see quadraticApproximation()
			boolean forceLinear,  // use linear approximation
			double thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
			double thresholdQuad){  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
		double [][] coeff = new PolynomialApproximation(this.debugLevel).quadraticApproximation(
				data,
				forceLinear,  // use linear approximation
				thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
				thresholdQuad);
		if (coeff==null) return null;
		if (coeff.length!=2) return null;
		double [][] result = new double [3][2];
		double [][] uv2xy=new double [2][2];
		int offset=(coeff[0].length>3)?3:0;
		for (int i=0;i<2;i++) {
			result[0][i]=coeff[i][offset+2];
			uv2xy[0][i]=coeff[0][offset+i];
			uv2xy[1][i]=coeff[1][offset+i];
		}
		double[][] wv=matrix2x2_invert(matrix2x2_scale(uv2xy,2.0));
		for (int i=0;i<2;i++) {
			result[1][i]=wv[0][i];
			result[2][i]=wv[1][i];
		}
		if ((debugLevel>2) && forceLinear) {
			System.out.println("*************** interpolateQuadraticWithWvAtZero() linear forced, data.length="+data.length);
		}
		if (debugLevel>3) {
			for (int i=0;i<data.length;i++) {
				System.out.println(i+": uv=["+IJ.d2s(data[i][0][0],3)+":"+IJ.d2s(data[i][0][1],3)+"]"+
						" xy=["+IJ.d2s(data[i][1][0],3)+":"+IJ.d2s(data[i][1][1],3)+"]"+
						" weight="+IJ.d2s(data[i][2][0],3));
			}
			String dbgStr="";
			dbgStr+=" [["+IJ.d2s(coeff[0][0],5)+"/"+IJ.d2s(coeff[0][1],5)+"/"+IJ.d2s(coeff[0][2],5);
			if (coeff[0].length>3) dbgStr+="/"+IJ.d2s(coeff[0][3],5)+"/"+IJ.d2s(coeff[0][4],5)+"/"+IJ.d2s(coeff[0][5],5)+"]]";
			dbgStr+=" [["+IJ.d2s(coeff[1][0],5)+"/"+IJ.d2s(coeff[1][1],5)+"/"+IJ.d2s(coeff[1][2],5);
			if (coeff[1].length>3) dbgStr+="/"+IJ.d2s(coeff[1][3],5)+"/"+IJ.d2s(coeff[1][4],5)+"/"+IJ.d2s(coeff[1][5],5)+"]]";
			System.out.println(dbgStr);
			for (int i=0;i<2;i++) {
				System.out.println(i+": uv2xy="+IJ.d2s(uv2xy[i][0],3)+":"+IJ.d2s(uv2xy[i][1],3));
			}
			for (int i=0;i<2;i++) {
				System.out.println(i+": wv="+IJ.d2s(wv[i][0],3)+":"+IJ.d2s(wv[i][1],3));
			}
		}
		return result;
	}
	// calculate simulation parameters for quadratic distortion of the pattern, compatible with SimulationPattern class
	// Returns 2 lines {{wv1x, wv1y, u0, Ax, Bx, Cx},{wv2x, wv2y, v0, Ay, By, Cy}}
	// if quadratic is not possible, only {{wv1x, wv1y, u0},{wv2x, wv2y}} will be returned
	// or just null if even linear is not possible
	// data array consists of lines of either 2 or 3 vectors:
	//  2-element vector x,y
	//  2 element vector u,v
	//  optional 1- element vector w (weight of the sample)
	public double [][] getSimulationParametersFromGrid(
			double [][][][] grid,
			int [] uv0,          // U,V of the center point (for which the simulation pattern should be built
			double [] xy0,          // x,y of the center point (or null to use grid)
			double [][] weights, // quadrant of sample weights
			boolean forceLinear,  // use linear approximation (instead of quadratic)
			double thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
			double thresholdQuad  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
			){
		int dist=weights.length-1;
		int size=dist*2+1;
		double [][][] samples0 = new double [size*size][3][];
		int index=0;
		int [] uv=new int[2];
		double w;
		if (xy0==null) {
			if (isCellDefined(grid,uv0)) {
				xy0=new double [2];
				xy0[0]=grid[uv0[1]][uv0[0]][0][0];
				xy0[1]=grid[uv0[1]][uv0[0]][0][1];
			} else {
				return null; //xy of the center is not known
Andrey Filippov's avatar
Andrey Filippov committed
11035
			}
11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065
		}
		int maxU=-dist-1,minU=dist+1,maxV=-dist-1,minV=dist+1,maxUpV=-2*dist-1,minUpV=2*dist+1,maxUmV=-2*dist-1,minUmV=2*dist+1;
		for (int iDv=-dist;iDv<=dist;iDv++) for (int iDu=-dist;iDu<=dist;iDu++) {
			uv[0]=uv0[0]+iDu;
			uv[1]=uv0[1]+iDv;
			if ((uv[0]>=0) && (uv[1]>=0) && (uv[1]<grid.length) && (uv[0]<grid[uv[1]].length) && (isCellDefined(grid,uv))) {
				w=weights[(iDv>=0)?iDv:-iDv][(iDu>=0)?iDu:-iDu];
				if (w!=0.0){
					if (maxU<iDu) maxU=iDu;
					if (minU>iDu) minU=iDu;
					if (maxV<iDv) maxV=iDv;
					if (minV>iDv) minV=iDv;
					if (maxUpV<(iDu+iDv)) maxUpV= iDu+iDv;
					if (minUpV>(iDu+iDv)) minUpV= iDu+iDv;
					if (maxUmV<(iDu-iDv)) maxUmV= iDu-iDv;
					if (minUmV>(iDu-iDv)) minUmV= iDu-iDv;
					samples0[index][0]=new double[2];
					samples0[index][1]=new double[2];
					samples0[index][2]=new double[1];
					samples0[index][2][0]=w;
					samples0[index][0][0]=grid[uv[1]][uv[0]][0][0]-xy0[0];
					samples0[index][0][1]=grid[uv[1]][uv[0]][0][1]-xy0[1];
					samples0[index][1][0]=uv[0];
					samples0[index][1][1]=uv[1];

					if (debugLevel>20) {
						System.out.println("iDu="+iDu+" iDv="+iDv+" "+
								" uv[0]="+IJ.d2s(uv[0],3)+" uv[1]="+IJ.d2s(uv[1],3)+" "+
								" samples0["+index+"][0][0]="+IJ.d2s(samples0[index][0][0],3)+" samples0["+index+"][0][1]="+IJ.d2s(samples0[index][0][1],3)+" "+
								" samples0["+index+"][1][0]="+IJ.d2s(samples0[index][1][0],3)+" samples0["+index+"][1][1]="+IJ.d2s(samples0[index][1][1],3));
Andrey Filippov's avatar
Andrey Filippov committed
11066
					}
11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113
					index++;
				}
			}
		}
		int diameter=maxU-minU;
		if (diameter>(maxV-minV)) diameter= maxV-minV;
		diameter*=2;
		if (diameter>(maxUpV-minUpV)) diameter= (maxUpV-minUpV);
		if (diameter>(maxUmV-minUmV)) diameter= (maxUmV-minUmV);
		if (debugLevel>2) System.out.println(" diameter="+diameter+" number="+index);
		if (diameter<2) return null;
		double [][][] samples = new double [index][][];
		System.arraycopy(samples0, 0, samples, 0, index);
		double [][] simulParams= getSimulationParametersFromSamples(
				samples,   // see quadraticApproximation()
				forceLinear || (diameter<5),  // use linear approximation diameter <4 should be enough, 5 - just to be safe
				thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
				thresholdQuad);  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
		return simulParams;
	}


	public double [][] getSimulationParametersFromSamples(
			double [][][] data,   // see quadraticApproximation()
			boolean forceLinear,  // use linear approximation
			double thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
			double thresholdQuad){  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
		double [][] coeff = new PolynomialApproximation(this.debugLevel).quadraticApproximation(
				data,
				forceLinear,  // use linear approximation
				thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
				thresholdQuad);

		if (debugLevel>2) {
			for (int i=0;i<data.length;i++) {
				System.out.println(i+": xy=["+IJ.d2s(data[i][0][0],3)+":"+IJ.d2s(data[i][0][1],3)+"]"+
						" uv=["+IJ.d2s(data[i][1][0],3)+":"+IJ.d2s(data[i][1][1],3)+"]"+
						" weight="+IJ.d2s(data[i][2][0],3));
			}
			String dbgStr="";
			dbgStr+=" ["+IJ.d2s(coeff[0][0],5)+"/"+IJ.d2s(coeff[0][1],5)+"/"+IJ.d2s(coeff[0][2],5);
			if (coeff[0].length>3) dbgStr+="/"+IJ.d2s(coeff[0][3],5)+"/"+IJ.d2s(coeff[0][4],5)+"/"+IJ.d2s(coeff[0][5],5)+"]";
			dbgStr+=" ["+IJ.d2s(coeff[1][0],5)+"/"+IJ.d2s(coeff[1][1],5)+"/"+IJ.d2s(coeff[1][2],5);
			if (coeff[1].length>3) dbgStr+="/"+IJ.d2s(coeff[1][3],5)+"/"+IJ.d2s(coeff[1][4],5)+"/"+IJ.d2s(coeff[1][5],5)+"]";
			System.out.println(dbgStr);
		}

Andrey Filippov's avatar
Andrey Filippov committed
11114

11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173
		if (coeff==null) return null;
		if (coeff.length!=2) return null;
		boolean isQuad=coeff[0].length>3;
		int offset=isQuad?3:0;
		double [][] result = new double [2][isQuad?6:3];
		double [][] xy2uv = new double [2][2];
		for (int i=0;i<2;i++) {
			result[i][2]=coeff[i][offset+2]; // F
			xy2uv[i][0]=coeff[i][offset+0]; // D
			xy2uv[i][1]=coeff[i][offset+1]; // E
			result[i][0]=0.5*xy2uv[i][0]; // 0.5 because uv grid is 0.5 (pos/neg)
			result[i][1]=0.5*xy2uv[i][1]; //
		}
		if (isQuad) {
			double [][] uv2xy=matrix2x2_invert(xy2uv);
			//        	   double [][] ABCuv={{coeff[0][0],coeff[0][1],0.5*coeff[0][2]},
			//        			              {coeff[1][0],coeff[1][1],0.5*coeff[1][2]}};
			double [][] ABCuv={
					{4*coeff[0][0],4*coeff[0][1],2*coeff[0][2]}, // correction that uv grid is 0.5
					{4*coeff[1][0],4*coeff[1][1],2*coeff[1][2]}};
			double [][] ABCxy=new double [2][3];
			for (int i=0;i<2;i++) for (int j=0;j<3;j++) {
				ABCxy[i][j]=0.0;
				for (int k=0;k<2;k++) ABCxy[i][j]+=uv2xy[i][k]*ABCuv[k][j];
			}
			for (int i=0;i<2;i++) for (int j=0;j<3;j++)result[i][j+3]=ABCxy[i][j];
		}
		return result;
	}

	public double [][] findPatternFromGrid(
			int x0, // top-left pixel of the square WOI
			int y0,
			int size, // size of square (pixels)
			double[] halfWindow,
			boolean forceLinear,  // use linear approximation (instead of quadratic)
			double thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
			double thresholdQuad  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
			){ // only half-window - half height by half width
		if (this.PATTERN_GRID==null) {
			String msg="PATTERN_GRID is needed, but undefined";
			IJ.showMessage("Error",msg);
			throw new IllegalArgumentException (msg);
		}
		if (this.PATTERN_GRID.length==0) return null;
		double x1=x0,y1=y0;
		double x2=x1+size;
		double y2=y1+size;
		double x,y;
		List <Integer> nodeList=new ArrayList<Integer>(1000);
		Integer Index;
		int len=this.PATTERN_GRID[0].length;
		for (int v=0;v<this.PATTERN_GRID.length;v++) for (int u=0;u<len;u++)
			if ((this.PATTERN_GRID[v][u]!=null) && (this.PATTERN_GRID[v][u][0]!=null)){
				x=this.PATTERN_GRID[v][u][0][0];
				y=this.PATTERN_GRID[v][u][0][1];
				if ((x>=x1) && (x<x2) && (y>=y1) && (y<y2)) {
					Index=v*len+u;
					nodeList.add(Index);
Andrey Filippov's avatar
Andrey Filippov committed
11174
				}
11175

11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203
			}
		double [][][] samples =new double [nodeList.size()][3][];
		// pattern parameters are referenced to the center of the square
		double xc=x0+size/2;
		double yc=y0+size/2;
		int halfSize=size/2;
		for (int i=0;i<samples.length;i++){
			int uv=nodeList.get(i);
			int v=uv/len;
			int u=uv%len;
			samples[i][0]=new double [2];
			samples[i][0][0]=this.PATTERN_GRID[v][u][0][0]-xc;
			samples[i][0][1]=this.PATTERN_GRID[v][u][0][1]-yc;
			samples[i][1]=new double [2];
			samples[i][1][0]=u;
			samples[i][1][1]=v;
			samples[i][2]=new double [1];
			int iy=((int) Math.round((this.PATTERN_GRID[v][u][0][1]-y1)/2));
			int ix=((int) Math.round((this.PATTERN_GRID[v][u][0][0]-x1)/2));
			samples[i][2][0]=((iy>=0) && (iy<halfSize) && (ix>=0) && (ix<halfSize))?halfWindow[iy*halfSize+ix]:0.0;
		}
		double [][] simulParams= getSimulationParametersFromSamples(
				samples,   // see quadraticApproximation()
				forceLinear || (halfSize<5),  // use linear approximation diameter <4 should be enough, 5 - just to be safe
				thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
				thresholdQuad);  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
		return simulParams;
	}
11204 11205


11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224
	/**		Moved to PolyninomialApproximation class */

	/**
	 * Approximate function z(x,y) as a second degree polynomial
	 * f(x,y)=A*x^2+B*y^2+C*x*y+D*x+E*y+F
	 * data array consists of lines of either 2 or 3 vectors:
	 *  2-element vector x,y
	 *  variable length vector z (should be the same for all samples)
	 *  optional 1- element vector w (weight of the sample)
	 *
	 * returns arrrray of vectors or null
	 * each vector (one per each z component) is either 6-element-  (A,B,C,D,E,F) if quadratic is possible and enabled
	 * or 3-element - (D,E,F) if linear is possible and quadratic is not possible or disbled
	 * returns null if not enough data even for the linear approximation

	 */

	/* ======================================================================== */
	/*
Andrey Filippov's avatar
Andrey Filippov committed
11225 11226 11227 11228 11229 11230 11231
	   public double [][] quadraticApproximation(
			   double [][][] data,
			   boolean forceLinear,  // use linear approximation
			   double thresholdLin,  // thershold ratio of matrix determinant to norm for linear approximation (det too low - fail)
			   double thresholdQuad  // thershold ratio of matrix determinant to norm for quadratic approximation (det too low - fail)
			   ){
/* ix, iy - the location of the point with maximal value. We'll approximate the vicinity of that maximum using a
11232
	 * second degree polynominal:
Andrey Filippov's avatar
Andrey Filippov committed
11233
   Z(x,y)~=A*x^2+B*y^2+C*x*y+D*x+E*y+F
11234
   by minimizing sum of squared differenceS00between the actual (Z(x,uy)) and approximated values.
Andrey Filippov's avatar
Andrey Filippov committed
11235
   and then find the maximum on the approximated surface. Here iS00the math:
11236

Andrey Filippov's avatar
Andrey Filippov committed
11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295
Z(x,y)~=A*x^2+B*y^2+C*x*y+D*x+E*y+F
minimizing squared error, using W(x,y) aS00weight function

error=Sum(W(x,y)*((A*x^2+B*y^2+C*x*y+D*x+E*y+F)-Z(x,y))^2)

error=Sum(W(x,y)*(A^2*x^4 + 2*A*x^2*(B*y^2+C*x*y+D*x+E*y+F-Z(x,y)) +(...) )
0=derror/dA=Sum(W(x,y)*(2*A*x^4 + 2*x^2*(B*y^2+C*x*y+D*x+E*y+F-Z(x,y)))
0=Sum(W(x,y)*(A*x^4 + x^2*(B*y^2+C*x*y+D*x+E*y+F-Z(x,y)))

S40=Sum(W(x,y)*x^4), etc

(1) 0=A*S40 + B*S22 + C*S31 +D*S30 +E*S21 +F*S20 - SZ20

derror/dB:

error=Sum(W(x,y)*(B^2*y^4 + 2*B*y^2*(A*x^2+C*x*y+D*x+E*y+F-Z(x,y)) +(...) )
0=derror/dB=Sum(W(x,y)*(2*B*y^4 + 2*y^2*(A*x^2+C*x*y+D*x+E*y+F-Z(x,y)))
0=Sum(W(x,y)*(B*y^4 + y^2*(A*x^2+C*x*y+D*x+E*y+F-Z(x,y)))

(2) 0=B*S04 + A*S22 + C*S13 +D*S12 +E*S03 +F*SY2 - SZ02
(2) 0=A*S22 + B*S04 + C*S13 +D*S12 +E*S03 +F*SY2 - SZ02

derror/dC:

error=Sum(W(x,y)*(C^2*x^2*y^2 + 2*C*x*y*(A*x^2+B*y^2+D*x+E*y+F-Z(x,y)) +(...) )
0=derror/dC=Sum(W(x,y)*(2*C*x^2*y^2 + 2*x*y*(A*x^2+B*y^2+D*x+E*y+F-Z(x,y)) )
0=Sum(W(x,y)*(C*x^2*y^2 + x*y*(A*x^2+B*y^2+D*x+E*y+F-Z(x,y)) )

(3) 0= A*S31 +  B*S13 +  C*S22 + D*S21 + E*S12 + F*S11 - SZ11

derror/dD:

error=Sum(W(x,y)*(D^2*x^2 + 2*D*x*(A*x^2+B*y^2+C*x*y+E*y+F-Z(x,y)) +(...) )
0=derror/dD=Sum(W(x,y)*(2*D*x^2 + 2*x*(A*x^2+B*y^2+C*x*y+E*y+F-Z(x,y)) )
0=Sum(W(x,y)*(D*x^2 + x*(A*x^2+B*y^2+C*x*y+E*y+F-Z(x,y)) )

(4) 0= A*S30 +   B*S12 +  C*S21 + D*S20  + E*S11 +  F*S10  - SZ10

derror/dE:

error=Sum(W(x,y)*(E^2*y^2 + 2*E*y*(A*x^2+B*y^2+C*x*y+D*x+F-Z(x,y)) +(...) )
0=derror/dE=Sum(W(x,y)*(2*E*y^2 + 2*y*(A*x^2+B*y^2+C*x*y+D*x+F-Z(x,y)) )
0=Sum(W(x,y)*(E*y^2 + y*(A*x^2+B*y^2+C*x*y+D*x+F-Z(x,y)) )
(5) 0= A*S21 +  B*S03 +   C*S12 + D*S11 +  E*SY2  + F*SY  - SZ01

derror/dF:

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)) +(...) )
0=derror/dF=Sum(W(x,y)*(2*F +  2*(A*x^2+B*y^2+C*x*y+D*x+E*y-Z(x,y)) )
0=Sum(W(x,y)*(F +  (A*x^2+B*y^2+C*x*y+D*x+E*y-Z(x,y)) )
(6) 0= A*S20 +   B*SY2 +   C*S11 +  D*S10 +   E*SY   + F*S00  - SZ00


(1) 0= A*S40 + B*S22 + C*S31 + D*S30 + E*S21 + F*S20 - SZ20
(2) 0= A*S22 + B*S04 + C*S13 + D*S12 + E*S03 + F*S02 - SZ02
(3) 0= A*S31 + B*S13 + C*S22 + D*S21 + E*S12 + F*S11 - SZ11
(4) 0= A*S30 + B*S12 + C*S21 + D*S20 + E*S11 + F*S10 - SZ10
(5) 0= A*S21 + B*S03 + C*S12 + D*S11 + E*S02 + F*S01 - SZ01
(6) 0= A*S20 + B*S02 + C*S11 + D*S10 + E*S01 + F*S00 - SZ00
11296
	 * /
11297
		   int zDim=data[0][1].length;
Andrey Filippov's avatar
Andrey Filippov committed
11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360

		   double w,z,x,x2,x3,x4,y,y2,y3,y4,wz;
		   int i,j,n=0;
		   double S00=0.0,
		   S10=0.0,S01=0.0,
		   S20=0.0,S11=0.0,S02=0.0,
		   S30=0.0,S21=0.0,S12=0.0,S03=0.0,
		   S40=0.0,S31=0.0,S22=0.0,S13=0.0,S04=0.0;
		   double [] SZ00=new double [zDim];
		   double [] SZ01=new double [zDim];
		   double [] SZ10=new double [zDim];
		   double [] SZ11=new double [zDim];
		   double [] SZ02=new double [zDim];
		   double [] SZ20=new double [zDim];
		   for (i=0;i<zDim;i++) {
			   SZ00[i]=0.0;
			   SZ01[i]=0.0;
			   SZ10[i]=0.0;
			   SZ11[i]=0.0;
			   SZ02[i]=0.0;
			   SZ20[i]=0.0;
		   }
		   for (i=0;i<data.length;i++)  {
			   w=(data[i].length>2)? data[i][2][0]:1.0;
			   if (w>0) {
				   n++;
				   x=data[i][0][0];
				   y=data[i][0][1];
				   x2=x*x;
				   y2=y*y;
				   S00+=w;
				   S10+=w*x;
				   S01+=w*y;
				   S11+=w*x*y;
				   S20+=w*x2;
				   S02+=w*y2;
				   if (!forceLinear) {
					   x3=x2*x;
					   x4=x3*x;
					   y3=y2*y;
					   y4=y3*y;
					   S30+=w*x3;
					   S21+=w*x2*y;
					   S12+=w*x*y2;
					   S03+=w*y3;
					   S40+=w*x4;
					   S31+=w*x3*y;
					   S22+=w*x2*y2;
					   S13+=w*x*y3;
					   S04+=w*y4;
				   }
				   for (j=0;j<zDim;j++) {
					   z=data[i][1][j];
					   wz=w*z;
					   SZ00[j]+=wz;
					   SZ10[j]+=wz*x;
					   SZ01[j]+=wz*y;
					   if (!forceLinear) {
						   SZ20[j]+=wz*x2;
						   SZ11[j]+=wz*x*y;
						   SZ02[j]+=wz*y2;
					   }
				   }
11361

Andrey Filippov's avatar
Andrey Filippov committed
11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384
			   }
		   }
		   //need to decide if there is enough data for linear and quadratic
		   double [][] mAarrayL= {
				   {S20,S11,S10},
				   {S11,S02,S01},
				   {S10,S01,S00}};
		   Matrix M=new Matrix (mAarrayL);
		   Matrix Z;
 	   	   if (debugLevel>3) System.out.println(">>> n="+n+" det_lin="+M.det()+" norm_lin="+normMatix(mAarrayL));
 	   	   double nmL=normMatix(mAarrayL);
		   if ((nmL==0.0) || (Math.abs(M.det())/nmL<thresholdLin)) return null; // not enough data even for the linear approximation
		   double []zAarrayL=new double [3];
		   double [][] ABCDEF=new double[zDim][];
//		   double [] zAarrayL={SZ10,SZ01,SZ00};
		   for (i=0;i<zDim;i++) {
			   zAarrayL[0]=SZ10[i];
			   zAarrayL[1]=SZ01[i];
			   zAarrayL[2]=SZ00[i];
		       Z=new Matrix (zAarrayL,3);
		       ABCDEF[i]= M.solve(Z).getRowPackedCopy();
		   }
		   if (forceLinear) return ABCDEF;
11385
		   // quote try quadratic approximation
Andrey Filippov's avatar
Andrey Filippov committed
11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414
		   double [][] mAarrayQ= {
				   {S40,S22,S31,S30,S21,S20},
				   {S22,S04,S13,S12,S03,S02},
				   {S31,S13,S22,S21,S12,S11},
				   {S30,S12,S21,S20,S11,S10},
				   {S21,S03,S12,S11,S02,S01},
				   {S20,S02,S11,S10,S01,S00}};
		   M=new Matrix (mAarrayQ);
 	   	   if (debugLevel>3) System.out.println("    n="+n+" det_quad="+M.det()+" norm_quad="+normMatix(mAarrayQ)+" data.length="+data.length);
 	   	   double nmQ=normMatix(mAarrayQ);
		   if ((nmQ==0.0) || (Math.abs(M.det())/normMatix(mAarrayQ)<thresholdQuad)) {
			   System.out.println("Using linear approximation, M.det()="+M.det()+" normMatix(mAarrayQ)="+normMatix(mAarrayQ)); //did not happen
			   return ABCDEF; // not enough data for the quadratic approximation, return linear
		   }
//		   double [] zAarrayQ={SZ20,SZ02,SZ11,SZ10,SZ01,SZ00};
		   double [] zAarrayQ=new double [6];
		   for (i=0;i<zDim;i++) {
			   zAarrayQ[0]=SZ20[i];
			   zAarrayQ[1]=SZ02[i];
			   zAarrayQ[2]=SZ11[i];
			   zAarrayQ[3]=SZ10[i];
			   zAarrayQ[4]=SZ01[i];
			   zAarrayQ[5]=SZ00[i];
			   Z=new Matrix (zAarrayQ,6);
			   ABCDEF[i]= M.solve(Z).getRowPackedCopy();
		   }
		   return ABCDEF;
	   }
//	calcualte "volume" made of the matrix row-vectors, placed orthogonally
11415
// to be compared to determinant
Andrey Filippov's avatar
Andrey Filippov committed
11416 11417 11418 11419 11420 11421 11422 11423 11424
	public double normMatix(double [][] a) {
        double d,norm=1.0;
        for (int i=0;i<a.length;i++) {
        	d=0;
        	for (int j=0;j<a[i].length;j++) d+=a[i][j]*a[i][j];
        	norm*=Math.sqrt(d);
        }
		return norm;
	}
11425 11426
	 */
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
11427 11428 11429 11430 11431 11432 11433
	public double[][][][] setPatternGridArray(int size) {
		return setPatternGridArray(size,size);
	}
	public double[][][][] setPatternGridArray(int width, int height) {
		int i,j;
		double[][][][] result= new double [height][width][][];
		for (i=0;i<height;i++) for (j=0;j<width;j++) result[i][j]=null;
11434
		return result;
Andrey Filippov's avatar
Andrey Filippov committed
11435 11436
	}

11437

Andrey Filippov's avatar
Andrey Filippov committed
11438 11439 11440 11441
	public static class PatternDetectParameters {
		public double gaussWidth; // <=0 - use Hamming window
		public double corrGamma;
		public double corrSigma;
11442
		public int    diffSpectrCorr;
Andrey Filippov's avatar
Andrey Filippov committed
11443
		public double shrinkClusters;
11444
		public int    multiplesToTry;
Andrey Filippov's avatar
Andrey Filippov committed
11445
		public double deviation;
11446
		public int    deviationSteps;
Andrey Filippov's avatar
Andrey Filippov committed
11447 11448 11449 11450 11451
		public double highpass;
		public double corrRingWidth;
		public double minCorrContrast;
		public double minGridPeriod;
		public double maxGridPeriod;
11452 11453
		public double minGridPeriodLwir;
		public double maxGridPeriodLwir;
11454 11455 11456
		public double debugX;
		public double debugY;
		public double debugRadius;
11457 11458
		// added for large cell findPattern (based on phase correlation)
		public boolean use_large_cells = false; // new method based on phase correlation should work with large cells,
11459
		// so only first negative correlation (1/2 period) fits in window
11460 11461 11462
		public double phaseCoeff =      0.5; // "phasiness" of correlation
		public double lowpass_sigma =   .3;  // for phase correlation - frequency fraction of maximal
		public double min_frac =        0.03; // do not use higher order autocorrelation if min/max
11463
		// is weaker than this fraction of the zero maximum
11464 11465 11466
		public double  min_sin =  0.5;  // minimal sine for the angle between two pattern vectors
		public boolean no_crazy = true; // fail if quadratic approximation fails or returns outside of +/- 1.5

Andrey Filippov's avatar
Andrey Filippov committed
11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480

		public PatternDetectParameters(
				double gaussWidth,
				double corrGamma,
				double corrSigma,
				int diffSpectrCorr,
				double shrinkClusters,
				int multiplesToTry,
				double deviation,
				int deviationSteps,
				double highpass,
				double corrRingWidth,
				double minCorrContrast,
				double minGridPeriod,
11481
				double maxGridPeriod,
11482 11483
				double minGridPeriodLwir,
				double maxGridPeriodLwir,
11484 11485
				double debugX,
				double debugY,
11486 11487 11488 11489 11490 11491 11492
				double debugRadius,
				boolean use_large_cells,
				double phaseCoeff,
				double lowpass_sigma,
				double min_frac,
				double  min_sin,
				boolean no_crazy
11493
				) {
Andrey Filippov's avatar
Andrey Filippov committed
11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506
			this.gaussWidth=gaussWidth;
			this.corrGamma = corrGamma;
			this.corrSigma = corrSigma;
			this.diffSpectrCorr = diffSpectrCorr;
			this.shrinkClusters = shrinkClusters;
			this.multiplesToTry = multiplesToTry;
			this.deviation = deviation;
			this.deviationSteps = deviationSteps;
			this.highpass = highpass;
			this.corrRingWidth = corrRingWidth;
			this.minCorrContrast = minCorrContrast;
			this.minGridPeriod=minGridPeriod;
			this.maxGridPeriod=maxGridPeriod;
11507 11508
			this.minGridPeriodLwir=minGridPeriodLwir;
			this.maxGridPeriodLwir=maxGridPeriodLwir;
11509 11510 11511
			this.debugX=debugX;
			this.debugY=debugY;
			this.debugRadius=debugRadius;
11512 11513 11514 11515 11516 11517
			this.use_large_cells = use_large_cells;
			this.phaseCoeff =      phaseCoeff;
			this.lowpass_sigma =   lowpass_sigma;
			this.min_frac =        min_frac;
			this.min_sin =         min_sin;
			this.no_crazy =        no_crazy;
Andrey Filippov's avatar
Andrey Filippov committed
11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533
		}

		public void setProperties(String prefix,Properties properties){
			properties.setProperty(prefix+"gaussWidth",this.gaussWidth+"");
			properties.setProperty(prefix+"corrGamma",this.corrGamma+"");
			properties.setProperty(prefix+"corrSigma",this.corrSigma+"");
			properties.setProperty(prefix+"diffSpectrCorr",this.diffSpectrCorr+"");
			properties.setProperty(prefix+"shrinkClusters",this.shrinkClusters+"");
			properties.setProperty(prefix+"multiplesToTry",this.multiplesToTry+"");
			properties.setProperty(prefix+"deviation",this.deviation+"");
			properties.setProperty(prefix+"deviationSteps",this.deviationSteps+"");
			properties.setProperty(prefix+"highpass",this.highpass+"");
			properties.setProperty(prefix+"corrRingWidth",this.corrRingWidth+"");
			properties.setProperty(prefix+"minCorrContrast",this.minCorrContrast+"");
			properties.setProperty(prefix+"minGridPeriod",this.minGridPeriod+"");
			properties.setProperty(prefix+"maxGridPeriod",this.maxGridPeriod+"");
11534 11535
			properties.setProperty(prefix+"minGridPeriodLwir",this.minGridPeriodLwir+"");
			properties.setProperty(prefix+"maxGridPeriodLwir",this.maxGridPeriodLwir+"");
11536 11537 11538
			properties.setProperty(prefix+"debugX",this.debugX+"");
			properties.setProperty(prefix+"debugY",this.debugY+"");
			properties.setProperty(prefix+"debugRadius",this.debugRadius+"");
11539 11540 11541 11542 11543 11544 11545

			properties.setProperty(prefix+"use_large_cells",this.use_large_cells+"");
			properties.setProperty(prefix+"phaseCoeff",     this.phaseCoeff+"");
			properties.setProperty(prefix+"lowpass_sigma",  this.lowpass_sigma+"");
			properties.setProperty(prefix+"min_frac",       this.min_frac+"");
			properties.setProperty(prefix+"min_sin",        this.min_sin+"");
			properties.setProperty(prefix+"no_crazy",       this.no_crazy+"");
Andrey Filippov's avatar
Andrey Filippov committed
11546
		}
11547

Andrey Filippov's avatar
Andrey Filippov committed
11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565
		public void getProperties(String prefix,Properties properties){
			this.gaussWidth=Double.parseDouble(properties.getProperty(prefix+"gaussWidth"));
			this.corrGamma=Double.parseDouble(properties.getProperty(prefix+"corrGamma"));
			this.corrSigma=Double.parseDouble(properties.getProperty(prefix+"corrSigma"));
			this.diffSpectrCorr=Integer.parseInt(properties.getProperty(prefix+"diffSpectrCorr"));
			this.shrinkClusters=Double.parseDouble(properties.getProperty(prefix+"shrinkClusters"));
			this.multiplesToTry=Integer.parseInt(properties.getProperty(prefix+"multiplesToTry"));
			this.deviation=Double.parseDouble(properties.getProperty(prefix+"deviation"));
			this.deviationSteps=Integer.parseInt(properties.getProperty(prefix+"deviationSteps"));
			this.highpass=Double.parseDouble(properties.getProperty(prefix+"highpass"));
			this.corrRingWidth=Double.parseDouble(properties.getProperty(prefix+"corrRingWidth"));
			this.minCorrContrast=Double.parseDouble(properties.getProperty(prefix+"minCorrContrast"));
			if (properties.getProperty(prefix+"minGridPeriod")!=null)
				this.minGridPeriod=Double.parseDouble(properties.getProperty(prefix+"minGridPeriod"));
			else this.minGridPeriod=0.0;
			if (properties.getProperty(prefix+"maxGridPeriod")!=null)
				this.minGridPeriod=Double.parseDouble(properties.getProperty(prefix+"maxGridPeriod"));
			else this.maxGridPeriod=0.0;
11566 11567 11568 11569 11570 11571 11572 11573

			if (properties.getProperty(prefix+"minGridPeriodLwir")!=null)
				this.minGridPeriodLwir=Double.parseDouble(properties.getProperty(prefix+"minGridPeriodLwir"));
			else this.minGridPeriodLwir=0.0;
			if (properties.getProperty(prefix+"maxGridPeriodLwir")!=null)
				this.minGridPeriodLwir=Double.parseDouble(properties.getProperty(prefix+"maxGridPeriodLwir"));
			else this.maxGridPeriodLwir=0.0;

11574 11575 11576 11577 11578 11579
			if (properties.getProperty(prefix+"debugX")!=null)
				this.debugX=Double.parseDouble(properties.getProperty(prefix+"debugX"));
			if (properties.getProperty(prefix+"debugY")!=null)
				this.debugY=Double.parseDouble(properties.getProperty(prefix+"debugY"));
			if (properties.getProperty(prefix+"debugRadius")!=null)
				this.debugRadius=Double.parseDouble(properties.getProperty(prefix+"debugRadius"));
11580 11581 11582 11583 11584 11585 11586 11587

			if (properties.getProperty(prefix+"use_large_cells")!=null) this.use_large_cells=Boolean.parseBoolean(properties.getProperty(prefix+"use_large_cells"));
			if (properties.getProperty(prefix+"phaseCoeff")!=null)      this.phaseCoeff=Double.parseDouble(properties.getProperty(prefix+"phaseCoeff"));
			if (properties.getProperty(prefix+"lowpass_sigma")!=null)   this.lowpass_sigma=Double.parseDouble(properties.getProperty(prefix+"lowpass_sigma"));
			if (properties.getProperty(prefix+"min_frac")!=null)        this.min_frac=Double.parseDouble(properties.getProperty(prefix+"min_frac"));

			if (properties.getProperty(prefix+"min_sin")!=null)         this.min_sin=Double.parseDouble(properties.getProperty(prefix+"min_sin"));
			if (properties.getProperty(prefix+"no_crazy")!=null)        this.no_crazy=Boolean.parseBoolean(properties.getProperty(prefix+"no_crazy"));
Andrey Filippov's avatar
Andrey Filippov committed
11588 11589 11590
		}
	}

Andrey Filippov's avatar
Andrey Filippov committed
11591
	/* ======================================================================== */
11592

Andrey Filippov's avatar
Andrey Filippov committed
11593
	public static class DistortionParameters {
11594 11595 11596 11597
		public int   correlationSize;            // FFTSize/4
		public int   correlationSizeLwir;
		public int   maximalCorrelationSize;     // FFTSize/2
		public int   maximalCorrelationSizeLwir;
Andrey Filippov's avatar
Andrey Filippov committed
11598
		public double correlationGaussWidth; // 0 - no window, <0 - use Hamming
11599 11600
		public boolean absoluteCorrelationGaussWidth=false; // do not scale correlationGaussWidth when the FFT size is increased
		public int zeros; // leave this number of zeros on the margins of the window (toatal from both sides). If correlationGaussWidth>0 will
11601
		// additionally multiply by Hamming
Andrey Filippov's avatar
Andrey Filippov committed
11602
		public int   FFTSize;
11603
		public int   FFTSize_lwir;
11604 11605 11606
		public int   FFTOverlap;      // 32 used for aberration kernels, former FFT_OVERLAP
		public int   FFTOverlap_lwir; // 4

Andrey Filippov's avatar
Andrey Filippov committed
11607
		public double fftGaussWidth;
11608 11609
		public double phaseCorrelationFraction=1.0; // 1.0 - phase correlation, 0.0 - just cross-correlation
		public double correlationHighPassSigma;
Andrey Filippov's avatar
Andrey Filippov committed
11610 11611 11612 11613 11614
		public double correlationLowPassSigma;
		public double correlationRingWidth; // ring (around r=0.5 dist to opposite corr) width , center circle r=0.5*correlationRingWidth
		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)
11615 11616
		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)
11617

11618
		public double scaleFirstPassContrast; // Decrease contrast of cells that are too close to the border to be processed in refinement pass
11619
		public double contrastSelectSigmaCenter; // Gaussian sigma to select correlation centers in pixels, 2.0  (center spot)
11620 11621
		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
11622

Andrey Filippov's avatar
Andrey Filippov committed
11623
		public int    minimalPatternCluster;       //    minimal pattern cluster size (0 - disable retries)
11624
		public int    minimalPatternClusterLwir;   //    minimal pattern cluster size (0 - disable retries)
Andrey Filippov's avatar
Andrey Filippov committed
11625
		public double scaleMinimalInitialContrast; // increase/decrease minimal contrast if initial cluster is >0 but less than minimalPatternCluster
11626

Andrey Filippov's avatar
Andrey Filippov committed
11627 11628 11629 11630
		public double searchOverlap;         // when searching for grid, step this amount of the FFTSize
		public int    patternSubdiv;
		public double correlationDx; // not saved
		public double correlationDy; // not saved
11631
		public int    gridSize;
Andrey Filippov's avatar
Andrey Filippov committed
11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642
		public int loop_debug_level;
		public boolean refineCorrelations;
		public boolean fastCorrelationOnFirstPass;
		public boolean fastCorrelationOnFinalPass;
		public double bPatternSigma; // blur bPattern with this sigma
		public double barraySigma; // blur barray with this sigma, multiplied by subdiv
		public double correlationWeightSigma; // sigma (in pixels) for maximum approximation - UNUSED (other maximum methods)
		public double correlationRadiusScale; // maximal radius to consider, in sigmas (if 0 - use sigma as radius) - UNUSED
		public int    correlationRadius;    // radius (green pixel) of the correlation maximum to use for x/y measurement
		public double correlationThreshold; // fraction of the value of the maximum fro the point to be included in centroid calculation
		public int    correlationSubdiv;    // Total subdivision of the correlation maximum (linear and FFT)
11643
		public int    correlationFFTSubdiv; // Increase density of the correlation using FFT
11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669
		public boolean correlationAverageOnRefine; // average position between neighbor samples
		public boolean refineInPlace;       // Update coordinates of the grid points as they are recalculated (false - then update all at once)
		public double averageOrthoDist;     // distance to up/down/right left neighbors (0.5)
		public double averageOrthoWeight;   // weight of 4 ortho neighbors (combined) - 0.4), weight of center -s 1.0-averageOrthoWeight-averageDiagWeight
		public double averageDiagDist;     // distance to diagonal neighbors (projection on x/y) (0.5)
		public double averageDiagWeight;   // weight of 4 diagonal neighbors (combined) - 0.4)
		public boolean useQuadratic;       // use quadratic extrapolation to predict position/wave vectors of a new pixel (false - use linear)
		public boolean removeLast;         // remove outer (unreliable) row of nodes
		public int    numberExtrapolated;  // add this number of extrapolated nodes
		public double extrapolationSigma;  // use instead of the correlationWeightSigma during final extrapolation
		public double minUVSpan;           // Minimal u/v span in correlation window that triggers increase of the correlation FFT size
		public boolean flatFieldCorrection=true; // compensate grid uneven intensity (vignetting, illumination)
		public double flatFieldExtarpolate=1.0;  // extrapolate flat field intensity map (relative to the average grid period)
		public double flatFieldBlur=1.0;   // blur the intensity map (relative to the average grid period)
		public double flatFieldMin=0.1;    // do not try to compensate if intensity less than this part of maximal
		public double flatFieldShrink=1.0;     // Shrink before extrapolating intensity map (relative to the average grid period)
		public double flatFieldExpand=3.0;     // Expand during extrapolation (relative to the average grid period)
		public double flatFieldSigmaRadius=1.0;// Extrapolation weight effective radius (relative to the average grid period)
		public double flatFieldExtraRadius=1.5;// Consider pixels in a square with the side twice this (relative to flatFieldSigmaRadius)
		public double averagingAreaScale=  2.0;  // multiply the average grid period to determine the area for averaging the grig brightness


		//        match pointers errors
		public int errTooFewCells=    -10;
		public int errPatternNotFound=-11;
		public boolean legacyMode=false;   // legacy mode
11670

Andrey Filippov's avatar
Andrey Filippov committed
11671 11672
		public DistortionParameters(
				int correlationSize,
11673
				int correlationSizeLwir,
Andrey Filippov's avatar
Andrey Filippov committed
11674
				int maximalCorrelationSize,
11675
				int maximalCorrelationSizeLwir,
Andrey Filippov's avatar
Andrey Filippov committed
11676 11677
				double correlationGaussWidth,
				boolean absoluteCorrelationGaussWidth,
11678
				int zeros,
Andrey Filippov's avatar
Andrey Filippov committed
11679
				int FFTSize,
11680
				int FFTSize_lwir,
11681 11682
				int FFTOverlap,
				int FFTOverlap_lwir,
Andrey Filippov's avatar
Andrey Filippov committed
11683 11684 11685 11686 11687 11688 11689 11690
				double fftGaussWidth,
				double phaseCorrelationFraction,
				double correlationHighPassSigma,
				double correlationLowPassSigma,
				double correlationRingWidth,
				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)
11691 11692 11693
				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)

11694
				double scaleFirstPassContrast, // Decrease contrast of cells that are too close to the border to be processed in refinement pass
11695
				double contrastSelectSigmaCenter, // Gaussian sigma to select correlation centers (fraction of UV period), 0.02 (center spot)
11696 11697
				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
Andrey Filippov's avatar
Andrey Filippov committed
11698
				int    minimalPatternCluster,       //    minimal pattern cluster size (0 - disable retries)
11699
				int    minimalPatternClusterLwir,   //    minimal pattern cluster size (0 - disable retries)
Andrey Filippov's avatar
Andrey Filippov committed
11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717
				double scaleMinimalInitialContrast, // increase/decrease minimal contrast if initial cluster is >0 but less than minimalPatternCluster
				double searchOverlap,         // when searching for grid, step this amount of the FFTSize
				int patternSubdiv,
				double correlationDx,
				double correlationDy,
				int gridSize,
				int loop_debug_level,
				boolean refineCorrelations,
				boolean fastCorrelationOnFirstPass, // use fast (less precise) correlation on first pass
				boolean fastCorrelationOnFinalPass, // use fast (less precise) correlation on refine pass
				double bPatternSigma,          // blur bPattern with this sigma
				double barraySigma,            // blur barray with this sigma, multiplied by subdiv
				double correlationWeightSigma, // sigma (in pixels) for maximum approximation
				double correlationRadiusScale, // maximal radius to consider, in sigmas (if 0 - use sigma as radius)
				int    correlationRadius,      // radius (green pixel) of the correlation maximum to use for x/y measurement
				double correlationThreshold,   // fraction of the value of the maximum fro the point to be included in centroid calculation
				int    correlationSubdiv,      // Total subdivision of the correlation maximum (linear and FFT)
				int    correlationFFTSubdiv,
11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738
				boolean correlationAverageOnRefine, // average position between neighbor samples
				boolean refineInPlace,         // Update coordinates of the grid points as they are recalculated (false - then update all at once)
				double averageOrthoDist,       // distance to up/down/right left neighbors (0.5)
				double averageOrthoWeight,     // weight of 4 ortho neighbors (combined) - 0.4), weight of center -s 1.0-averageOrthoWeight-averageDiagWeight
				double averageDiagDist,        // distance to diagonal neighbors (projection on x/y) (0.5)
				double averageDiagWeight,      // weight of 4 diagonal neighbors (combined) - 0.4)
				boolean useQuadratic,          // use quadratic extrapolation to predict position/wave vectors of a new pixel (false - use linear)
				boolean removeLast,            // remove outer (unreliable) row of nodes
				int    numberExtrapolated,     // add this number of extrapolated nodes
				double extrapolationSigma,     // use instead of the correlationWeightSigma during final extrapolation
				double minUVSpan,              // Minimal u/v span in correlation window that triggers increase of the correlation FFT size
				boolean flatFieldCorrection,   // compensate grid uneven intensity (vignetting, illumination)
				double flatFieldExtarpolate,   // extrapolate flat field intensity map (relative to the average grid period)
				double flatFieldBlur,          // blur the intensity map (relative to the average grid period)
				double flatFieldMin,           // do not try to compensate if intensity less than this part of maximal
				double flatFieldShrink,        // Shrink before extrapolating intensity map (relative to the average grid period)
				double flatFieldExpand,        // Expand during extrapolation (relative to the average grid period)
				double flatFieldSigmaRadius,   // Extrapolation weight effective radius (relative to the average grid period)
				double flatFieldExtraRadius,   // Consider pixels in a square with the side twice this (relative to flatFieldSigmaRadius)
				double averagingAreaScale,     // multiply the average grid period to determine the area for averaging the grig brightness
				boolean legacyMode
Andrey Filippov's avatar
Andrey Filippov committed
11739 11740 11741
				){

			this.correlationSize = correlationSize;
11742
			this.correlationSizeLwir = correlationSizeLwir;
Andrey Filippov's avatar
Andrey Filippov committed
11743
			this.maximalCorrelationSize=maximalCorrelationSize;
11744
			this.maximalCorrelationSizeLwir=maximalCorrelationSizeLwir;
Andrey Filippov's avatar
Andrey Filippov committed
11745 11746 11747 11748
			this.correlationGaussWidth = correlationGaussWidth;
			this.absoluteCorrelationGaussWidth=absoluteCorrelationGaussWidth;
			this.zeros=zeros;
			this.FFTSize = FFTSize;
11749
			this.FFTSize_lwir = FFTSize_lwir;
11750 11751
			this.FFTOverlap = FFTOverlap;
			this.FFTOverlap_lwir = FFTOverlap_lwir;
Andrey Filippov's avatar
Andrey Filippov committed
11752 11753 11754 11755 11756 11757 11758 11759
			this.fftGaussWidth = fftGaussWidth;
			this.phaseCorrelationFraction=phaseCorrelationFraction;
			this.correlationHighPassSigma=correlationHighPassSigma;
			this.correlationLowPassSigma=correlationLowPassSigma;
			this.correlationRingWidth=correlationRingWidth;
			this.correlationMaxOffset=correlationMaxOffset;
			this.correlationMinContrast=correlationMinContrast;
			this.correlationMinInitialContrast=correlationMinInitialContrast;
11760 11761
			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)
11762
			this.scaleFirstPassContrast=scaleFirstPassContrast; // Decrease contrast of cells that are too close to the border to be processed in refinement pass
11763
			this.contrastSelectSigmaCenter = contrastSelectSigmaCenter; // Gaussian sigma to select correlation centers (pixels, 2.0)
11764 11765
			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
Andrey Filippov's avatar
Andrey Filippov committed
11766
			this.minimalPatternCluster=minimalPatternCluster;        //    minimal pattern cluster size (0 - disable retries)
11767
			this.minimalPatternClusterLwir=minimalPatternClusterLwir;        //    minimal pattern cluster size (0 - disable retries)
Andrey Filippov's avatar
Andrey Filippov committed
11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800
			this.scaleMinimalInitialContrast=scaleMinimalInitialContrast; // increase/decrease minimal contrast if initial cluster is >0 but less than minimalPatternCluster
			this.searchOverlap=searchOverlap;         // when searching for grid, step this amount of the FFTSize
			this.patternSubdiv=patternSubdiv;
			this.correlationDx=correlationDx;
			this.correlationDy=correlationDy;
			this.gridSize=gridSize;
			this.loop_debug_level=loop_debug_level;
			this.refineCorrelations=refineCorrelations;
			this.fastCorrelationOnFirstPass=fastCorrelationOnFirstPass;
			this.fastCorrelationOnFinalPass=fastCorrelationOnFinalPass;
			this.bPatternSigma=bPatternSigma; // overwrites SimulationParameters.bPatternSigma
			this.barraySigma=barraySigma;
			this.correlationWeightSigma=correlationWeightSigma;
			this.correlationRadiusScale=correlationRadiusScale;
			this.correlationRadius=correlationRadius;
			this.correlationThreshold=correlationThreshold;
			this.correlationSubdiv=correlationSubdiv;
			this.correlationFFTSubdiv=correlationFFTSubdiv;
			this.correlationAverageOnRefine=correlationAverageOnRefine;
			this.refineInPlace=refineInPlace;
			this.averageOrthoDist=averageOrthoDist;
			this.averageOrthoWeight=averageOrthoWeight;
			this.averageDiagDist=averageDiagDist;
			this.averageDiagWeight=averageDiagWeight;
			this.useQuadratic=useQuadratic;
			this.removeLast=removeLast;
			this.numberExtrapolated=numberExtrapolated;
			this.extrapolationSigma=extrapolationSigma;
			this.minUVSpan=minUVSpan;
			this.flatFieldCorrection=flatFieldCorrection;   // compensate grid uneven intensity (vignetting, illumination)
			this.flatFieldExtarpolate=flatFieldExtarpolate;   // extrapolate flat field intensity map (relative to the average grid period)
			this.flatFieldBlur=flatFieldBlur;          // blur the intensity map (relative to the average grid period)
			this.flatFieldMin=flatFieldMin;
11801
			this.flatFieldShrink=flatFieldShrink;
Andrey Filippov's avatar
Andrey Filippov committed
11802 11803 11804
			this.flatFieldExpand=flatFieldExpand;
			this.flatFieldSigmaRadius=flatFieldSigmaRadius;
			this.flatFieldExtraRadius=flatFieldExtraRadius;
11805
			this.averagingAreaScale=averagingAreaScale;
Andrey Filippov's avatar
Andrey Filippov committed
11806 11807 11808
			this.legacyMode=legacyMode;

		}
11809
		@Override
Andrey Filippov's avatar
Andrey Filippov committed
11810 11811
		public DistortionParameters clone() {
			return new DistortionParameters(
11812 11813 11814 11815 11816 11817 11818 11819 11820
					this.correlationSize,
					this.correlationSizeLwir,
					this.maximalCorrelationSize,
					this.maximalCorrelationSizeLwir,
					this.correlationGaussWidth,
					this.absoluteCorrelationGaussWidth,
					this.zeros,
					this.FFTSize,
					this.FFTSize_lwir,
11821 11822
					this.FFTOverlap,
					this.FFTOverlap_lwir,
11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878
					this.fftGaussWidth,
					this.phaseCorrelationFraction,
					this.correlationHighPassSigma,
					this.correlationLowPassSigma,
					this.correlationRingWidth,
					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 refinement pass
					this.contrastSelectSigmaCenter, // Gaussian sigma to select correlation centers (pixels, 2.0)
					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
					this.minimalPatternCluster,        //    minimal pattern cluster size (0 - disable retries)
					this.minimalPatternClusterLwir,        //    minimal pattern cluster size (0 - disable retries)
					this.scaleMinimalInitialContrast,  // increase/decrease minimal contrast if initial cluster is >0 but less than minimalPatternCluster
					this.searchOverlap,         // when searching for grid, step this amount of the FFTSize
					this.patternSubdiv,
					this.correlationDx,
					this.correlationDy,
					this.gridSize,
					this.loop_debug_level,
					this.refineCorrelations,
					this.fastCorrelationOnFirstPass, // use fast (less precise) correlation on first pass
					this.fastCorrelationOnFinalPass, // use fast (less precise) correlation on refine pass
					this.bPatternSigma, // blur bPattern with this sigma
					this.barraySigma,
					this.correlationWeightSigma, // sigma (in pixels) for maximum approximation
					this.correlationRadiusScale, // maximal radius to consider, in sigmas (if 0 - use sigma as radius)
					this.correlationRadius,    // radius (green pixel) of the correlation maximum to use for x/y measurement
					this.correlationThreshold,
					this.correlationSubdiv,    // Total subdivision of the correlation maximum (linear and FFT)
					this.correlationFFTSubdiv,
					this.correlationAverageOnRefine, // average position between neighbor samples
					this.refineInPlace,       // Update coordinates of the grid points as they are recalculated (false - then update all at once)
					this.averageOrthoDist,    // distance to up/down/right left neighbors (0.5)
					this.averageOrthoWeight,  // weight of 4 ortho neighbors (combined) - 0.4), weight of center -s 1.0-averageOrthoWeight-averageDiagWeight
					this.averageDiagDist,     // distance to diagonal neighbors (projection on x/y) (0.5)
					this.averageDiagWeight,   // weight of 4 diagonal neighbors (combined) - 0.4)
					this.useQuadratic,        // use quadratic extrapolation to predict position/wave vectors of a new pixel (false - use linear)
					this.removeLast,          // remove outer (unreliable) row of nodes
					this.numberExtrapolated,  // add this number of extrapolated nodes
					this.extrapolationSigma,   // use instead of the correlationWeightSigma during final extrapolation
					this.minUVSpan,            // Minimal u/v span in correlation window that triggers increase of the correlation FFT size
					this.flatFieldCorrection,  // compensate grid uneven intensity (vignetting, illumination)
					this.flatFieldExtarpolate, // extrapolate flat field intensity map (relative to the average grid period)
					this.flatFieldBlur,        // blur the intensity map (relative to the average grid period)
					this.flatFieldMin,
					this.flatFieldShrink,
					this.flatFieldExpand,
					this.flatFieldSigmaRadius,
					this.flatFieldExtraRadius,
					this.averagingAreaScale,
					this.legacyMode
					);
Andrey Filippov's avatar
Andrey Filippov committed
11879 11880 11881 11882
		}

		public void setProperties(String prefix,Properties properties){
			properties.setProperty(prefix+"correlationSize",this.correlationSize+"");
11883
			properties.setProperty(prefix+"correlationSizeLwir",this.correlationSizeLwir+"");
Andrey Filippov's avatar
Andrey Filippov committed
11884
			properties.setProperty(prefix+"maximalCorrelationSize",this.maximalCorrelationSize+"");
11885
			properties.setProperty(prefix+"maximalCorrelationSizeLwir",this.maximalCorrelationSizeLwir+"");
Andrey Filippov's avatar
Andrey Filippov committed
11886 11887 11888 11889
			properties.setProperty(prefix+"correlationGaussWidth",this.correlationGaussWidth+"");
			properties.setProperty(prefix+"absoluteCorrelationGaussWidth",this.absoluteCorrelationGaussWidth+"");
			properties.setProperty(prefix+"zeros",this.zeros+"");
			properties.setProperty(prefix+"FFTSize",this.FFTSize+"");
11890
			properties.setProperty(prefix+"FFTSize_lwir",this.FFTSize_lwir+"");
11891 11892
			properties.setProperty(prefix+"FFTOverlap",this.FFTOverlap+"");
			properties.setProperty(prefix+"FFTOverlap_lwir",this.FFTOverlap_lwir+"");
Andrey Filippov's avatar
Andrey Filippov committed
11893 11894 11895 11896 11897 11898 11899 11900
			properties.setProperty(prefix+"fftGaussWidth",this.fftGaussWidth+"");
			properties.setProperty(prefix+"phaseCorrelationFraction",this.phaseCorrelationFraction+"");
			properties.setProperty(prefix+"correlationHighPassSigma",this.correlationHighPassSigma+"");
			properties.setProperty(prefix+"correlationLowPassSigma",this.correlationLowPassSigma+"");
			properties.setProperty(prefix+"correlationRingWidth",this.correlationRingWidth+"");
			properties.setProperty(prefix+"correlationMaxOffset",this.correlationMaxOffset+"");
			properties.setProperty(prefix+"correlationMinContrast",this.correlationMinContrast+"");
			properties.setProperty(prefix+"correlationMinInitialContrast",this.correlationMinInitialContrast+"");
11901 11902
			properties.setProperty(prefix+"correlationMinAbsoluteContrast",this.correlationMinAbsoluteContrast+"");
			properties.setProperty(prefix+"correlationMinAbsoluteInitialContrast",this.correlationMinAbsoluteInitialContrast+"");
11903
			properties.setProperty(prefix+"scaleFirstPassContrast",this.scaleFirstPassContrast+"");
11904
			properties.setProperty(prefix+"contrastSelectSigmaCenter",this.contrastSelectSigmaCenter+"");
11905 11906
			properties.setProperty(prefix+"contrastSelectSigma",this.contrastSelectSigma+"");
			properties.setProperty(prefix+"contrastAverageSigma",this.contrastAverageSigma+"");
Andrey Filippov's avatar
Andrey Filippov committed
11907
			properties.setProperty(prefix+"minimalPatternCluster",this.minimalPatternCluster+"");
11908
			properties.setProperty(prefix+"minimalPatternClusterLwir",this.minimalPatternClusterLwir+"");
Andrey Filippov's avatar
Andrey Filippov committed
11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937
			properties.setProperty(prefix+"scaleMinimalInitialContrast",this.scaleMinimalInitialContrast+"");
			properties.setProperty(prefix+"searchOverlap",this.searchOverlap+"");
			properties.setProperty(prefix+"patternSubdiv",this.patternSubdiv+"");
			properties.setProperty(prefix+"correlationDx",this.correlationDx+"");
			properties.setProperty(prefix+"correlationDy",this.correlationDy+"");
			properties.setProperty(prefix+"gridSize",this.gridSize+"");
			properties.setProperty(prefix+"loop_debug_level",this.loop_debug_level+"");
			properties.setProperty(prefix+"refineCorrelations",this.refineCorrelations+"");
			properties.setProperty(prefix+"fastCorrelationOnFirstPass",this.fastCorrelationOnFirstPass+"");
			properties.setProperty(prefix+"fastCorrelationOnFinalPass",this.fastCorrelationOnFinalPass+"");
			properties.setProperty(prefix+"bPatternSigma",this.bPatternSigma+"");
			properties.setProperty(prefix+"barraySigma",this.barraySigma+"");
			properties.setProperty(prefix+"correlationWeightSigma",this.correlationWeightSigma+"");
			properties.setProperty(prefix+"correlationRadiusScale",this.correlationRadiusScale+"");
			properties.setProperty(prefix+"correlationRadius",this.correlationRadius+"");
			properties.setProperty(prefix+"correlationThreshold",this.correlationThreshold+"");
			properties.setProperty(prefix+"correlationSubdiv",this.correlationSubdiv+"");
			properties.setProperty(prefix+"correlationFFTSubdiv",this.correlationFFTSubdiv+"");
			properties.setProperty(prefix+"correlationAverageOnRefine",this.correlationAverageOnRefine+"");
			properties.setProperty(prefix+"refineInPlace",this.refineInPlace+"");
			properties.setProperty(prefix+"averageOrthoDist",this.averageOrthoDist+"");
			properties.setProperty(prefix+"averageOrthoWeight",this.averageOrthoWeight+"");
			properties.setProperty(prefix+"averageDiagDist",this.averageDiagDist+"");
			properties.setProperty(prefix+"averageDiagWeight",this.averageDiagWeight+"");
			properties.setProperty(prefix+"useQuadratic",this.useQuadratic+"");
			properties.setProperty(prefix+"removeLast",this.removeLast+"");
			properties.setProperty(prefix+"numberExtrapolated",this.numberExtrapolated+"");
			properties.setProperty(prefix+"extrapolationSigma",this.extrapolationSigma+"");
			properties.setProperty(prefix+"minUVSpan",this.minUVSpan+"");
11938

Andrey Filippov's avatar
Andrey Filippov committed
11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951
			properties.setProperty(prefix+"flatFieldCorrection",this.flatFieldCorrection+"");
			properties.setProperty(prefix+"flatFieldExtarpolate",this.flatFieldExtarpolate+"");
			properties.setProperty(prefix+"flatFieldBlur",this.flatFieldBlur+"");
			properties.setProperty(prefix+"flatFieldMin",this.flatFieldMin+"");

			properties.setProperty(prefix+"flatFieldShrink",this.flatFieldShrink+"");
			properties.setProperty(prefix+"flatFieldExpand",this.flatFieldExpand+"");
			properties.setProperty(prefix+"flatFieldSigmaRadius",this.flatFieldSigmaRadius+"");
			properties.setProperty(prefix+"flatFieldExtraRadius",this.flatFieldExtraRadius+"");
			properties.setProperty(prefix+"averagingAreaScale",this.averagingAreaScale+"");
			properties.setProperty(prefix+"legacyMode",this.minUVSpan+"");
		}
		public void getProperties(String prefix,Properties properties){
11952
//			EProperties properties = (EProperties) pproperties;
Andrey Filippov's avatar
Andrey Filippov committed
11953
			if (properties.getProperty(prefix+"correlationSize")!=null)
11954 11955 11956
				this.correlationSize=Integer.parseInt(properties.getProperty(prefix+"correlationSize"));
			if (properties.getProperty(prefix+"correlationSizeLwir")!=null)
				this.correlationSizeLwir=Integer.parseInt(properties.getProperty(prefix+"correlationSizeLwir"));
Andrey Filippov's avatar
Andrey Filippov committed
11957
			if (properties.getProperty(prefix+"maximalCorrelationSize")!=null)
11958 11959 11960
				this.maximalCorrelationSize=Integer.parseInt(properties.getProperty(prefix+"maximalCorrelationSize"));
			if (properties.getProperty(prefix+"maximalCorrelationSizeLwir")!=null)
				this.maximalCorrelationSizeLwir=Integer.parseInt(properties.getProperty(prefix+"maximalCorrelationSizeLwir"));
Andrey Filippov's avatar
Andrey Filippov committed
11961
			if (properties.getProperty(prefix+"correlationGaussWidth")!=null)
11962
				this.correlationGaussWidth=Double.parseDouble(properties.getProperty(prefix+"correlationGaussWidth"));
Andrey Filippov's avatar
Andrey Filippov committed
11963
			if (properties.getProperty(prefix+"FFTSize")!=null)
11964 11965 11966
				this.FFTSize=Integer.parseInt(properties.getProperty(prefix+"FFTSize"));
			if (properties.getProperty(prefix+"FFTSize_lwir")!=null)
				this.FFTSize_lwir=Integer.parseInt(properties.getProperty(prefix+"FFTSize_lwir"));
11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977

			if (properties.getProperty(prefix+"FFTOverlap")!=null)
				this.FFTOverlap=Integer.parseInt(properties.getProperty(prefix+"FFTOverlap"));
			if (properties.getProperty(prefix+"FFTOverlap_lwir")!=null)
				this.FFTOverlap_lwir=Integer.parseInt(properties.getProperty(prefix+"FFTOverlap_lwir"));


// finally shortened :
//			this.FFTOverlap=      properties.getProperty(prefix+"FFTOverlap",     this.FFTOverlap);
//			this.FFTOverlap_lwir= properties.getProperty(prefix+"FFTOverlap_lwir",this.FFTOverlap_lwir);

Andrey Filippov's avatar
Andrey Filippov committed
11978
			if (properties.getProperty(prefix+"absoluteCorrelationGaussWidth")!=null)
11979
				this.absoluteCorrelationGaussWidth=Boolean.parseBoolean(properties.getProperty(prefix+"absoluteCorrelationGaussWidth"));
Andrey Filippov's avatar
Andrey Filippov committed
11980
			if (properties.getProperty(prefix+"zeros")!=null)
11981
				this.zeros=Integer.parseInt(properties.getProperty(prefix+"zeros"));
Andrey Filippov's avatar
Andrey Filippov committed
11982
			if (properties.getProperty(prefix+"fftGaussWidth")!=null)
11983
				this.fftGaussWidth=Double.parseDouble(properties.getProperty(prefix+"fftGaussWidth"));
Andrey Filippov's avatar
Andrey Filippov committed
11984
			if (properties.getProperty(prefix+"phaseCorrelationFraction")!=null)
11985
				this.phaseCorrelationFraction=Double.parseDouble(properties.getProperty(prefix+"phaseCorrelationFraction"));
Andrey Filippov's avatar
Andrey Filippov committed
11986
			if (properties.getProperty(prefix+"correlationHighPassSigma")!=null)
11987
				this.correlationHighPassSigma=Double.parseDouble(properties.getProperty(prefix+"correlationHighPassSigma"));
Andrey Filippov's avatar
Andrey Filippov committed
11988
			if (properties.getProperty(prefix+"correlationLowPassSigma")!=null)
11989
				this.correlationLowPassSigma=Double.parseDouble(properties.getProperty(prefix+"correlationLowPassSigma"));
Andrey Filippov's avatar
Andrey Filippov committed
11990
			if (properties.getProperty(prefix+"correlationRingWidth")!=null)
11991
				this.correlationRingWidth=Double.parseDouble(properties.getProperty(prefix+"correlationRingWidth"));
Andrey Filippov's avatar
Andrey Filippov committed
11992
			if (properties.getProperty(prefix+"correlationMaxOffset")!=null)
11993
				this.correlationMaxOffset=Double.parseDouble(properties.getProperty(prefix+"correlationMaxOffset"));
Andrey Filippov's avatar
Andrey Filippov committed
11994
			if (properties.getProperty(prefix+"correlationMinContrast")!=null)
11995
				this.correlationMinContrast=Double.parseDouble(properties.getProperty(prefix+"correlationMinContrast"));
Andrey Filippov's avatar
Andrey Filippov committed
11996
			if (properties.getProperty(prefix+"correlationMinInitialContrast")!=null)
11997
				this.correlationMinInitialContrast=Double.parseDouble(properties.getProperty(prefix+"correlationMinInitialContrast"));
11998

11999
			if (properties.getProperty(prefix+"correlationMinAbsoluteContrast")!=null)
12000
				this.correlationMinAbsoluteContrast=Double.parseDouble(properties.getProperty(prefix+"correlationMinAbsoluteContrast"));
12001
			if (properties.getProperty(prefix+"correlationMinAbsoluteInitialContrast")!=null)
12002
				this.correlationMinAbsoluteInitialContrast=Double.parseDouble(properties.getProperty(prefix+"correlationMinAbsoluteInitialContrast"));
12003

12004
			if (properties.getProperty(prefix+"scaleFirstPassContrast")!=null)
12005
				this.scaleFirstPassContrast=Double.parseDouble(properties.getProperty(prefix+"scaleFirstPassContrast"));
12006
			if (properties.getProperty(prefix+"contrastSelectSigmaCenter")!=null)
12007
				this.contrastSelectSigmaCenter=Double.parseDouble(properties.getProperty(prefix+"contrastSelectSigmaCenter"));
12008
			if (properties.getProperty(prefix+"contrastSelectSigma")!=null)
12009
				this.contrastSelectSigma=Double.parseDouble(properties.getProperty(prefix+"contrastSelectSigma"));
12010
			if (properties.getProperty(prefix+"contrastAverageSigma")!=null)
12011
				this.contrastAverageSigma=Double.parseDouble(properties.getProperty(prefix+"contrastAverageSigma"));
12012

Andrey Filippov's avatar
Andrey Filippov committed
12013
			if (properties.getProperty(prefix+"minimalPatternCluster")!=null)
12014 12015 12016 12017
				this.minimalPatternCluster=Integer.parseInt(properties.getProperty(prefix+"minimalPatternCluster"));
			if (properties.getProperty(prefix+"minimalPatternClusterLwir")!=null)
				this.minimalPatternClusterLwir=Integer.parseInt(properties.getProperty(prefix+"minimalPatternClusterLwir"));

Andrey Filippov's avatar
Andrey Filippov committed
12018
			if (properties.getProperty(prefix+"scaleMinimalInitialContrast")!=null)
12019
				this.scaleMinimalInitialContrast=Double.parseDouble(properties.getProperty(prefix+"scaleMinimalInitialContrast"));
Andrey Filippov's avatar
Andrey Filippov committed
12020
			if (properties.getProperty(prefix+"searchOverlap")!=null)
12021
				this.searchOverlap=Double.parseDouble(properties.getProperty(prefix+"searchOverlap"));
Andrey Filippov's avatar
Andrey Filippov committed
12022
			if (properties.getProperty(prefix+"patternSubdiv")!=null)
12023
				this.patternSubdiv=Integer.parseInt(properties.getProperty(prefix+"patternSubdiv"));
Andrey Filippov's avatar
Andrey Filippov committed
12024
			if (properties.getProperty(prefix+"correlationDx")!=null)
12025
				this.correlationDx=Double.parseDouble(properties.getProperty(prefix+"correlationDx"));
Andrey Filippov's avatar
Andrey Filippov committed
12026
			if (properties.getProperty(prefix+"correlationDy")!=null)
12027
				this.correlationDy=Double.parseDouble(properties.getProperty(prefix+"correlationDy"));
Andrey Filippov's avatar
Andrey Filippov committed
12028
			if (properties.getProperty(prefix+"gridSize")!=null)
12029
				this.gridSize=Integer.parseInt(properties.getProperty(prefix+"gridSize"));
Andrey Filippov's avatar
Andrey Filippov committed
12030
			if (properties.getProperty(prefix+"loop_debug_level")!=null)
12031
				this.loop_debug_level=Integer.parseInt(properties.getProperty(prefix+"loop_debug_level"));
Andrey Filippov's avatar
Andrey Filippov committed
12032
			if (properties.getProperty(prefix+"refineCorrelations")!=null)
12033
				this.refineCorrelations=Boolean.parseBoolean(properties.getProperty(prefix+"refineCorrelations"));
Andrey Filippov's avatar
Andrey Filippov committed
12034
			if (properties.getProperty(prefix+"fastCorrelationOnFirstPass")!=null)
12035
				this.fastCorrelationOnFirstPass=Boolean.parseBoolean(properties.getProperty(prefix+"fastCorrelationOnFirstPass"));
Andrey Filippov's avatar
Andrey Filippov committed
12036
			if (properties.getProperty(prefix+"fastCorrelationOnFinalPass")!=null)
12037
				this.fastCorrelationOnFinalPass=Boolean.parseBoolean(properties.getProperty(prefix+"fastCorrelationOnFinalPass"));
Andrey Filippov's avatar
Andrey Filippov committed
12038
			if (properties.getProperty(prefix+"bPatternSigma")!=null)
12039
				this.bPatternSigma=Double.parseDouble(properties.getProperty(prefix+"bPatternSigma"));
Andrey Filippov's avatar
Andrey Filippov committed
12040
			if (properties.getProperty(prefix+"barraySigma")!=null)
12041
				this.barraySigma=Double.parseDouble(properties.getProperty(prefix+"barraySigma"));
Andrey Filippov's avatar
Andrey Filippov committed
12042
			if (properties.getProperty(prefix+"correlationWeightSigma")!=null)
12043
				this.correlationWeightSigma=Double.parseDouble(properties.getProperty(prefix+"correlationWeightSigma"));
Andrey Filippov's avatar
Andrey Filippov committed
12044
			if (properties.getProperty(prefix+"correlationRadiusScale")!=null)
12045
				this.correlationRadiusScale=Double.parseDouble(properties.getProperty(prefix+"correlationRadiusScale"));
Andrey Filippov's avatar
Andrey Filippov committed
12046
			if (properties.getProperty(prefix+"correlationRadius")!=null)
12047
				this.correlationRadius=Integer.parseInt(properties.getProperty(prefix+"correlationRadius"));
Andrey Filippov's avatar
Andrey Filippov committed
12048
			if (properties.getProperty(prefix+"correlationThreshold")!=null)
12049
				this.correlationThreshold=Double.parseDouble(properties.getProperty(prefix+"correlationThreshold"));
Andrey Filippov's avatar
Andrey Filippov committed
12050
			if (properties.getProperty(prefix+"correlationSubdiv")!=null)
12051
				this.correlationSubdiv=Integer.parseInt(properties.getProperty(prefix+"correlationSubdiv"));
Andrey Filippov's avatar
Andrey Filippov committed
12052
			if (properties.getProperty(prefix+"correlationFFTSubdiv")!=null)
12053
				this.correlationFFTSubdiv=Integer.parseInt(properties.getProperty(prefix+"correlationFFTSubdiv"));
Andrey Filippov's avatar
Andrey Filippov committed
12054
			if (properties.getProperty(prefix+"correlationAverageOnRefine")!=null)
12055
				this.correlationAverageOnRefine=Boolean.parseBoolean(properties.getProperty(prefix+"correlationAverageOnRefine"));
Andrey Filippov's avatar
Andrey Filippov committed
12056
			if (properties.getProperty(prefix+"refineInPlace")!=null)
12057
				this.refineInPlace=Boolean.parseBoolean(properties.getProperty(prefix+"refineInPlace"));
Andrey Filippov's avatar
Andrey Filippov committed
12058
			if (properties.getProperty(prefix+"averageOrthoDist")!=null)
12059
				this.averageOrthoDist=Double.parseDouble(properties.getProperty(prefix+"averageOrthoDist"));
Andrey Filippov's avatar
Andrey Filippov committed
12060
			if (properties.getProperty(prefix+"averageOrthoWeight")!=null)
12061
				this.averageOrthoWeight=Double.parseDouble(properties.getProperty(prefix+"averageOrthoWeight"));
Andrey Filippov's avatar
Andrey Filippov committed
12062
			if (properties.getProperty(prefix+"averageDiagDist")!=null)
12063
				this.averageDiagDist=Double.parseDouble(properties.getProperty(prefix+"averageDiagDist"));
Andrey Filippov's avatar
Andrey Filippov committed
12064
			if (properties.getProperty(prefix+"correlationRadiusScale")!=null)
12065
				this.averageDiagWeight=Double.parseDouble(properties.getProperty(prefix+"averageDiagWeight"));
Andrey Filippov's avatar
Andrey Filippov committed
12066
			if (properties.getProperty(prefix+"useQuadratic")!=null)
12067
				this.useQuadratic=Boolean.parseBoolean(properties.getProperty(prefix+"useQuadratic"));
Andrey Filippov's avatar
Andrey Filippov committed
12068
			if (properties.getProperty(prefix+"removeLast")!=null)
12069
				this.removeLast=Boolean.parseBoolean(properties.getProperty(prefix+"removeLast"));
Andrey Filippov's avatar
Andrey Filippov committed
12070 12071 12072
			if (properties.getProperty(prefix+"numberExtrapolated")!=null)
				this.numberExtrapolated=Integer.parseInt(properties.getProperty(prefix+"numberExtrapolated"));
			if (properties.getProperty(prefix+"extrapolationSigma")!=null)
12073
				this.extrapolationSigma=Double.parseDouble(properties.getProperty(prefix+"extrapolationSigma"));
Andrey Filippov's avatar
Andrey Filippov committed
12074
			if (properties.getProperty(prefix+"minUVSpan")!=null)
12075
				this.minUVSpan=Double.parseDouble(properties.getProperty(prefix+"minUVSpan"));
Andrey Filippov's avatar
Andrey Filippov committed
12076
			if (properties.getProperty(prefix+"flatFieldCorrection")!=null)
12077
				this.flatFieldCorrection=Boolean.parseBoolean(properties.getProperty(prefix+"flatFieldCorrection"));
Andrey Filippov's avatar
Andrey Filippov committed
12078
			if (properties.getProperty(prefix+"flatFieldExtarpolate")!=null)
12079
				this.flatFieldExtarpolate=Double.parseDouble(properties.getProperty(prefix+"flatFieldExtarpolate"));
Andrey Filippov's avatar
Andrey Filippov committed
12080
			if (properties.getProperty(prefix+"flatFieldBlur")!=null)
12081
				this.flatFieldBlur=Double.parseDouble(properties.getProperty(prefix+"flatFieldBlur"));
Andrey Filippov's avatar
Andrey Filippov committed
12082
			if (properties.getProperty(prefix+"flatFieldMin")!=null)
12083
				this.flatFieldMin=Double.parseDouble(properties.getProperty(prefix+"flatFieldMin"));
Andrey Filippov's avatar
Andrey Filippov committed
12084
			if (properties.getProperty(prefix+"flatFieldShrink")!=null)
12085
				this.flatFieldShrink=Double.parseDouble(properties.getProperty(prefix+"flatFieldShrink"));
Andrey Filippov's avatar
Andrey Filippov committed
12086
			if (properties.getProperty(prefix+"flatFieldExpand")!=null)
12087
				this.flatFieldExpand=Double.parseDouble(properties.getProperty(prefix+"flatFieldExpand"));
Andrey Filippov's avatar
Andrey Filippov committed
12088
			if (properties.getProperty(prefix+"flatFieldSigmaRadius")!=null)
12089
				this.flatFieldSigmaRadius=Double.parseDouble(properties.getProperty(prefix+"flatFieldSigmaRadius"));
Andrey Filippov's avatar
Andrey Filippov committed
12090
			if (properties.getProperty(prefix+"flatFieldExtraRadius")!=null)
12091
				this.flatFieldExtraRadius=Double.parseDouble(properties.getProperty(prefix+"flatFieldExtraRadius"));
Andrey Filippov's avatar
Andrey Filippov committed
12092
			if (properties.getProperty(prefix+"averagingAreaScale")!=null)
12093
				this.averagingAreaScale=Double.parseDouble(properties.getProperty(prefix+"averagingAreaScale"));
Andrey Filippov's avatar
Andrey Filippov committed
12094
			if (properties.getProperty(prefix+"legacyMode")!=null)
12095 12096
				this.legacyMode=Boolean.parseBoolean(properties.getProperty(prefix+"legacyMode"));
		}
Andrey Filippov's avatar
Andrey Filippov committed
12097
	}
12098
	///===== end of public static class DistortionParameters ==============================
Andrey Filippov's avatar
Andrey Filippov committed
12099 12100
	/* Use ROI */
	/* Supply rectangle */
12101

Andrey Filippov's avatar
Andrey Filippov committed
12102
	// Now accepts rectangles not completely contained in the image, pixels will be copied from the image edge
12103 12104 12105 12106 12107
	// getNoBayer() - replacement for splitBayer for monochrome images
	public double[] getNoBayer(ImagePlus imp, Rectangle r) {
		return getNoBayer (imp, 1, r);
	}
	//	private double[][] splitBayer (ImagePlus imp, Rectangle r, boolean equalize_greens) {
Andrey Filippov's avatar
Andrey Filippov committed
12108 12109 12110
	public double[][] splitBayer (ImagePlus imp, Rectangle r, boolean equalize_greens) {
		return splitBayer (imp, 1, r, equalize_greens);
	}
12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140
	public double[] getNoBayer (ImagePlus imp,  int sliceNumber, Rectangle r) {
		if (imp==null) return null;
		ImageProcessor ip=null;
		float [] pixels;
		if (imp.getStackSize()>1){
			ip=imp.getStack().getProcessor(sliceNumber);
		} else {
			ip=imp.getProcessor();
		}
		pixels=(float[])ip.getPixels();
		int full_width= imp.getWidth();  // full image width
		int full_height=imp.getHeight(); // full image height
		if (r==null) r=new Rectangle(0,0,full_width,full_height);
		double [] dpixels = new double[r.height * r.width];
		int out_indx = 0;
		for (int y = 0; y < r.height; y++) {
			int y_in = y + r.y;
			if      (y_in < 0)            y_in = 0;
			else if (y_in >= full_height) y_in = full_height - 1;
			int base = y_in * full_width;
			for (int x = 0; x < r.width; x++) {
				int x_in = x + r.x;
				if      (x_in < 0)           x_in = 0;
				else if (x_in >= full_width) x_in = full_width - 1;
				dpixels[out_indx++] = pixels[base+x_in];
			}
		}
		return dpixels;
	}

Andrey Filippov's avatar
Andrey Filippov committed
12141 12142 12143 12144 12145
	public double[][] splitBayer (ImagePlus imp,  int sliceNumber, Rectangle r, boolean equalize_greens) {
		if (imp==null) return null;
		ImageProcessor ip=null;
		float [] pixels;
		if (imp.getStackSize()>1){
12146
			ip=imp.getStack().getProcessor(sliceNumber);
Andrey Filippov's avatar
Andrey Filippov committed
12147
		} else {
12148
			ip=imp.getProcessor();
12149 12150
		}
		pixels=(float[])ip.getPixels();   // null pointer
Andrey Filippov's avatar
Andrey Filippov committed
12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163
		int full_width= imp.getWidth();  // full image width
		int full_height=imp.getHeight(); // full image height
		if (r==null) r=new Rectangle(0,0,full_width,full_height);
		if (debugLevel>10) IJ.showMessage("splitBayer","r.width="+r.width+
				"\nr.height="+r.height+
				"\nr.x="+r.x+
				"\nr.y="+r.y+
				"\nlength="+pixels.length);
		if ((debugLevel>2) && ((r.x<0) || (r.y<0) || ((r.x+r.width)>=full_width) || ((r.y+r.height)>=full_height))) System.out.println("r.width="+r.width+
				" r.height="+r.height+
				" r.x="+r.x+
				" r.y="+r.y);
		int x,y,base,base_b,bv,i,j;
12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236
		int half_height = (r.height >> 1);
		int half_width =  (r.width >>  1);
		// make them all 0 if not a single pixel falls into the image
		int numColors=(half_height==half_width)?5:4;
		int pixX,pixY;
		double [][] bayer_pixels=new double[numColors][half_height * half_width];
		if ((r.x>=full_width) || (r.y>=full_height) || ((r.x+r.width)<0)  || ((r.y+r.height)<0)) {
			for (i=0;i<bayer_pixels.length;i++) for (j=0;j<bayer_pixels[i].length;j++) bayer_pixels[i][j]=0.0;
			return bayer_pixels;
		}
		//      base=r.width*((y<<1)+bv);
		for (y=0; y<half_height; y++) for (bv=0;bv<2;bv++){
			pixY=(y*2)+bv+r.y;
			base_b=half_width*y;
			//					if ((pixY>=0)

			if (pixY<0) {
				pixY=bv;
			} else if (pixY>=full_height){
				pixY=full_height-2+bv;
			}
			base=full_width*pixY+((r.x>0)?r.x:0);
			//						base=full_width*((y*2)+bv+r.y)+r.x;
			pixX=r.x;
			if (bv==0) for (x=0; x<half_width; x++) {
				if ((pixX<0) || (pixX>=(full_width-2))) {
					bayer_pixels[0][base_b]= pixels[base];
					bayer_pixels[1][base_b]= pixels[base+1];
				} else {
					bayer_pixels[0][base_b]= pixels[base++];
					bayer_pixels[1][base_b]= pixels[base++];
				}
				base_b++;
				pixX+=2;
			} else  for (x=0; x<half_width; x++) {
				if ((pixX<0) || (pixX>=(full_width-2))) {
					bayer_pixels[2][base_b]= pixels[base];
					bayer_pixels[3][base_b]= pixels[base+1];
				} else {
					bayer_pixels[2][base_b]= pixels[base++];
					bayer_pixels[3][base_b]= pixels[base++];
				}
				base_b++;
				pixX+=2;
			}
		}
		if (equalize_greens) {
			double g0=0.0,g3=0.0,g02=0.0,g32=0.0,a0,a3,b0,b3;
			int n=bayer_pixels[0].length;
			for (i=0;i<bayer_pixels[0].length;i++) {
				g0 +=bayer_pixels[0][i];
				g02+=bayer_pixels[0][i]*bayer_pixels[0][i];
				g3 +=bayer_pixels[3][i];
				g32+=bayer_pixels[3][i]*bayer_pixels[3][i];
			}
			g0/=n; // mean value
			g3/=n; // meran value
			g02=g02/n-g0*g0;
			g32=g32/n-g3*g3;
			b0=Math.sqrt(Math.sqrt(g32/g02));
			b3 = 1.0/b0;
			a0= (g0+g3)/2 -b0*g0;
			a3= (g0+g3)/2 -b3*g3;
			if (debugLevel>2) {
				System.out.println("g0= "+g0+ ", g3= "+g3);
				System.out.println("g02="+g02+", g32="+g32);
				System.out.println("a0="+a0+", b0="+b0);
				System.out.println("a3="+a3+", b3="+b3);
			}
			for (i=0;i<bayer_pixels[0].length;i++) {
				bayer_pixels[0][i]=a0+bayer_pixels[0][i]*b0;
				bayer_pixels[3][i]=a3+bayer_pixels[3][i]*b3;
			}
Andrey Filippov's avatar
Andrey Filippov committed
12237

12238
		}
Andrey Filippov's avatar
Andrey Filippov committed
12239

12240 12241
		if (numColors>4) bayer_pixels[4]=combineDiagonalGreens (bayer_pixels[0], bayer_pixels[3],  half_width, half_height);
		return bayer_pixels;
Andrey Filippov's avatar
Andrey Filippov committed
12242
	}
12243

Andrey Filippov's avatar
Andrey Filippov committed
12244 12245 12246
	public double[][] splitBayerOne (ImagePlus imp, Rectangle r, boolean equalize_greens) {
		ImageProcessor ip=imp.getProcessor();
		float [] pixels;
12247
		pixels=(float[])ip.getPixels();
Andrey Filippov's avatar
Andrey Filippov committed
12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260
		int full_width= imp.getWidth();  // full image width
		int full_height=imp.getHeight(); // full image height
		if (debugLevel>10) IJ.showMessage("splitBayer","r.width="+r.width+
				"\nr.height="+r.height+
				"\nr.x="+r.x+
				"\nr.y="+r.y+
				"\nlength="+pixels.length);
		if ((debugLevel>2) && ((r.x<0) || (r.y<0) || ((r.x+r.width)>=full_width) || ((r.y+r.height)>=full_height))) System.out.println("r.width="+r.width+
				" r.height="+r.height+
				" r.x="+r.x+
				" r.y="+r.y);
		int x,y,base,base_b,bv,i,j;
		int half_height=r.height>>1;
12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332
		int half_width=r.width>>1;
			// make them all 0 if not a single pixel falls into the image
			int numColors=(half_height==half_width)?5:4;
			int pixX,pixY;
			double [][] bayer_pixels=new double[numColors][half_height * half_width];
			if ((r.x>=full_width) || (r.y>=full_height) || ((r.x+r.width)<0)  || ((r.y+r.height)<0)) {
				for (i=0;i<bayer_pixels.length;i++) for (j=0;j<bayer_pixels[i].length;j++) bayer_pixels[i][j]=0.0;
				return bayer_pixels;
			}
			//      base=r.width*((y<<1)+bv);
			for (y=0; y<half_height; y++) for (bv=0;bv<2;bv++){
				pixY=(y*2)+bv+r.y;
				base_b=half_width*y;
				//					if ((pixY>=0)

				if (pixY<0) {
					pixY=bv;
				} else if (pixY>=full_height){
					pixY=full_height-2+bv;
				}
				base=full_width*pixY+((r.x>0)?r.x:0);
				//						base=full_width*((y*2)+bv+r.y)+r.x;
				pixX=r.x;
				if (bv==0) for (x=0; x<half_width; x++) {
					if ((pixX<0) || (pixX>=(full_width-2))) {
						bayer_pixels[0][base_b]= pixels[base];
						bayer_pixels[1][base_b]= pixels[base+1];
					} else {
						bayer_pixels[0][base_b]= pixels[base++];
						bayer_pixels[1][base_b]= pixels[base++];
					}
					base_b++;
					pixX+=2;
				} else  for (x=0; x<half_width; x++) {
					if ((pixX<0) || (pixX>=(full_width-2))) {
						bayer_pixels[2][base_b]= pixels[base];
						bayer_pixels[3][base_b]= pixels[base+1];
					} else {
						bayer_pixels[2][base_b]= pixels[base++];
						bayer_pixels[3][base_b]= pixels[base++];
					}
					base_b++;
					pixX+=2;
				}
			}
			if (equalize_greens) {
				double g0=0.0,g3=0.0,g02=0.0,g32=0.0,a0,a3,b0,b3;
				int n=bayer_pixels[0].length;
				for (i=0;i<bayer_pixels[0].length;i++) {
					g0 +=bayer_pixels[0][i];
					g02+=bayer_pixels[0][i]*bayer_pixels[0][i];
					g3 +=bayer_pixels[3][i];
					g32+=bayer_pixels[3][i]*bayer_pixels[3][i];
				}
				g0/=n; // mean value
				g3/=n; // meran value
				g02=g02/n-g0*g0;
				g32=g32/n-g3*g3;
				b0=Math.sqrt(Math.sqrt(g32/g02));
				b3 = 1.0/b0;
				a0= (g0+g3)/2 -b0*g0;
				a3= (g0+g3)/2 -b3*g3;
				if (debugLevel>2) {
					System.out.println("g0= "+g0+ ", g3= "+g3);
					System.out.println("g02="+g02+", g32="+g32);
					System.out.println("a0="+a0+", b0="+b0);
					System.out.println("a3="+a3+", b3="+b3);
				}
				for (i=0;i<bayer_pixels[0].length;i++) {
					bayer_pixels[0][i]=a0+bayer_pixels[0][i]*b0;
					bayer_pixels[3][i]=a3+bayer_pixels[3][i]*b3;
				}
Andrey Filippov's avatar
Andrey Filippov committed
12333

12334
			}
Andrey Filippov's avatar
Andrey Filippov committed
12335

12336 12337
			if (numColors>4) bayer_pixels[4]=combineDiagonalGreens (bayer_pixels[0], bayer_pixels[3],  half_width, half_height);
			return bayer_pixels;
Andrey Filippov's avatar
Andrey Filippov committed
12338 12339 12340 12341 12342
	}

	public double[][] splitBayerZero (ImagePlus imp, Rectangle r, boolean equalize_greens) {
		ImageProcessor ip=imp.getProcessor();
		float [] pixels;
12343
		pixels=(float[])ip.getPixels();
Andrey Filippov's avatar
Andrey Filippov committed
12344 12345 12346 12347 12348 12349 12350
		if (debugLevel>10) IJ.showMessage("splitBayer","r.width="+r.width+
				"\nr.height="+r.height+
				"\nr.x="+r.x+
				"\nr.y="+r.y+
				"\nlength="+pixels.length);
		int x,y,base,base_b,bv,i;
		int half_height=r.height>>1;
12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425
				int half_width=r.width>>1;
				int full_width= imp.getWidth();  // full image width
				int full_height=imp.getHeight(); // full image height
				int numColors=(half_height==half_width)?5:4;
				int pixX,pixY;
				double [][] bayer_pixels=new double[numColors][half_height * half_width];
				//      base=r.width*((y<<1)+bv);
				for (y=0; y<half_height; y++) for (bv=0;bv<2;bv++){
					pixY=(y*2)+bv+r.y;
					base_b=half_width*y;
					if ((pixY<0) || (pixY>=full_height)) {
						if (bv==0) for (x=0; x<half_width; x++) {
							bayer_pixels[0][base_b]= 0.0;
							bayer_pixels[1][base_b]= 0.0;
							base_b++;
						} else  for (x=0; x<half_width; x++) {
							bayer_pixels[2][base_b]= 0.0;
							bayer_pixels[3][base_b]= 0.0;
							base_b++;
						}
					} else {
						base=full_width*((y*2)+bv+r.y)+r.x;
						pixX=r.x;
						if (bv==0) for (x=0; x<half_width; x++) {
							if ((pixX<0) || (pixX>=(full_width-1))) {
								bayer_pixels[0][base_b]= 0.0;
								bayer_pixels[1][base_b]= 0.0;
								base+=2;
							} else {
								bayer_pixels[0][base_b]= pixels[base++];
								bayer_pixels[1][base_b]= pixels[base++];
							}
							base_b++;
							pixX+=2;
						} else  for (x=0; x<half_width; x++) {
							if ((pixX<0) || (pixX>=(full_width-1))) {
								bayer_pixels[2][base_b]= 0.0;
								bayer_pixels[3][base_b]= 0.0;
								base+=2;
							} else {
								bayer_pixels[2][base_b]= pixels[base++];
								bayer_pixels[3][base_b]= pixels[base++];
							}
							base_b++;
							pixX+=2;
						}
					}
				}
				if (equalize_greens) {
					double g0=0.0,g3=0.0,g02=0.0,g32=0.0,a0,a3,b0,b3;
					int n=bayer_pixels[0].length;
					for (i=0;i<bayer_pixels[0].length;i++) {
						g0 +=bayer_pixels[0][i];
						g02+=bayer_pixels[0][i]*bayer_pixels[0][i];
						g3 +=bayer_pixels[3][i];
						g32+=bayer_pixels[3][i]*bayer_pixels[3][i];
					}
					g0/=n; // mean value
					g3/=n; // meran value
					g02=g02/n-g0*g0;
					g32=g32/n-g3*g3;
					b0=Math.sqrt(Math.sqrt(g32/g02));
					b3 = 1.0/b0;
					a0= (g0+g3)/2 -b0*g0;
					a3= (g0+g3)/2 -b3*g3;
					if (debugLevel>2) {
						System.out.println("g0= "+g0+ ", g3= "+g3);
						System.out.println("g02="+g02+", g32="+g32);
						System.out.println("a0="+a0+", b0="+b0);
						System.out.println("a3="+a3+", b3="+b3);
					}
					for (i=0;i<bayer_pixels[0].length;i++) {
						bayer_pixels[0][i]=a0+bayer_pixels[0][i]*b0;
						bayer_pixels[3][i]=a3+bayer_pixels[3][i]*b3;
					}
Andrey Filippov's avatar
Andrey Filippov committed
12426

12427
				}
Andrey Filippov's avatar
Andrey Filippov committed
12428

12429 12430
				if (numColors>4) bayer_pixels[4]=combineDiagonalGreens (bayer_pixels[0], bayer_pixels[3],  half_width, half_height);
				return bayer_pixels;
Andrey Filippov's avatar
Andrey Filippov committed
12431 12432
	}

12433 12434 12435 12436 12437 12438 12439 12440 12441
	/* Create a Thread[] array as large as the number of processors available.
	 * From Stephan Preibisch's Multithreading.java class. See:
	 * http://repo.or.cz/w/trakem2.git?a=blob;f=mpi/fruitfly/general/MultiThreading.java;hb=HEAD
	 */
	private Thread[] newThreadArray(int maxCPUs) {
		int n_cpus = Runtime.getRuntime().availableProcessors();
		if (n_cpus>maxCPUs)n_cpus=maxCPUs;
		return new Thread[n_cpus];
	}
Andrey Filippov's avatar
Andrey Filippov committed
12442
	/* Start all given threads and wait on each of them until all are done.
12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454
	 * From Stephan Preibisch's Multithreading.java class. See:
	 * http://repo.or.cz/w/trakem2.git?a=blob;f=mpi/fruitfly/general/MultiThreading.java;hb=HEAD
	 */
	private static void startAndJoin(Thread[] threads)
	{
		for (int ithread = 0; ithread < threads.length; ++ithread)
		{
			threads[ithread].setPriority(Thread.NORM_PRIORITY);
			threads[ithread].start();
		}

		try
Andrey Filippov's avatar
Andrey Filippov committed
12455 12456
		{
			for (int ithread = 0; ithread < threads.length; ++ithread)
12457 12458 12459 12460 12461 12462 12463 12464
				threads[ithread].join();
		} catch (InterruptedException ie)
		{
			throw new RuntimeException(ie);
		}
	}
	// Parameters for identifying red laser pointers on the image of the pattern grid

Andrey Filippov's avatar
Andrey Filippov committed
12465
}