Commit 9a970304 authored by Andrey Filippov's avatar Andrey Filippov
Browse files

Improving basic depth map generation

parent f801b9b5
Loading
Loading
Loading
Loading
+67 −12
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ public class CLTPass3d{
		private double []       bgTileStrength =       null;
		private boolean []      border_tiles =         null; // these are border tiles, zero out alpha
		private boolean []      selected =             null; // which tiles are selected for this layer
		boolean []              has_lma =              null; // which tiles are measured with LMA (reliable strength)
		public  double [][][][] texture_tiles;
		// texture_selection is only used for the GPU and if not null means it is for the GPU
		public boolean []       texture_selection =    null; // use by the GPU to set texture to generate
@@ -70,6 +71,7 @@ public class CLTPass3d{
		public  Rectangle       texture_bounds; // in tiles, not pixels !
		public  int             dbg_index;
		public  int             disparity_index =      ImageDtt.DISPARITY_INDEX_CM; // may also be ImageDtt.DISPARITY_INDEX_POLY
		public   int            lma_disparity_index =  ImageDtt.DISPARITY_INDEX_POLY; // set to -1 to ignore and always use just CM (also applies to lma_strength - next)
		public double [][]      tiles_RBGA =           null;

		SuperTiles              superTiles = null;
@@ -362,6 +364,15 @@ public class CLTPass3d{
			this.border_tiles = border_tiles;
		}
		
		public boolean [] getLMA() {
			if (has_lma == null) {
				has_lma = hasLMADefined();
			}
			return has_lma;
		}
		public void setLMA(boolean [] has_lma) {// use for combo tiles
			this.has_lma = has_lma;
		}

		public void fixNaNDisparity()
		{
@@ -599,24 +610,31 @@ public class CLTPass3d{
		// methods to "condition" measured disparity values
		public void conditionDisparity()
		{
/*			
			conditionDisparity(disparity_index);
		}

		public void conditionDisparity(int disparity_index)
		public void conditionDisparity(int disparity_index) // only called from above
		{
*/		
			int tilesX = tileProcessor.getTilesX();
			int tilesY = tileProcessor.getTilesY();
			double corr_magic_scale =     tileProcessor.getMagicScale();


			this.disparity_index = disparity_index;
			double corr_magic_scale_LMA = 1.0;
//			int lma_disparity_index = ImageDtt.DISPARITY_INDEX_POLY;
//			this.disparity_index = disparity_index;
			calc_disparity =      new double[tilesY*tilesX];
			calc_disparity_hor =  new double[tilesY*tilesX];
			calc_disparity_vert = new double[tilesY*tilesX];
			double [] lma_disparity = (lma_disparity_index >= 0) ? disparity_map[lma_disparity_index] : null;
			for (int i = 0; i < tilesY; i++){
				for (int j = 0; j < tilesX; j++){
					int indx = i * tilesX + j;
					if ((lma_disparity != null) && !Double.isNaN(lma_disparity[indx])) {
						calc_disparity[indx] =  lma_disparity[indx]/corr_magic_scale_LMA +                            this.disparity[i][j];
					} else {
						calc_disparity[indx] =  disparity_map[disparity_index][indx]/corr_magic_scale +               this.disparity[i][j];
					}
					calc_disparity_hor[indx] =  disparity_map[ImageDtt.DISPARITY_INDEX_HOR][indx]/corr_magic_scale +  this.disparity[i][j];
					calc_disparity_vert[indx] = disparity_map[ImageDtt.DISPARITY_INDEX_VERT][indx]/corr_magic_scale + this.disparity[i][j];
				}
@@ -624,6 +642,28 @@ public class CLTPass3d{
			calc_disparity_combo = calc_disparity.clone(); // for now - just clone, can be modified separately and combined with hor/vert
		}
		
		public boolean [] hasLMADefined(){ // will try not to create this.has_lma
			if (disparity_map == null) {
				if (has_lma==null) {
					return null;
				} else {
					return has_lma;
				}
				
			}
			int tilesX = tileProcessor.getTilesX();
			int tilesY = tileProcessor.getTilesY();
			double [] lma_disparity = (lma_disparity_index >= 0) ? disparity_map[lma_disparity_index] : null;
			boolean [] lma_defined = new boolean[tilesX * tilesY];
			if (lma_disparity != null) {
				for (int i = 0; i < lma_disparity.length; i++) {
					lma_defined[i] = !Double.isNaN(lma_disparity[i]);
				}
			}
			return lma_defined;
		}
		
		
		// bypassing calculations
		public void setCalcDisparityStrength(
				double [] disparity,
@@ -644,7 +684,7 @@ public class CLTPass3d{
		 *
		 * Replace weak by a weighted average of non-weak. If there are none - use weak ones, including this one too.
		 */
		public boolean[] replaceWeakOutliers(
		public boolean[] replaceWeakOutliers( // does not replace tiles with LMA available, busts LMA-defined strengths when averaging
				final boolean [] selection,
				final double weakStrength,    // strength to be considered weak, subject to this replacement
				final double maxDiff,
@@ -654,6 +694,7 @@ public class CLTPass3d{
				final double disparityNear,
				final int debugLevel)
		{
			final double  scale_strength_lma = 5.0; // increase LMA-defined strength during averaging
			final int tilesX = tileProcessor.getTilesX();
			final int tilesY = tileProcessor.getTilesY();

@@ -663,6 +704,8 @@ public class CLTPass3d{
			final int [] dirs = dirs8;
			final double [] disparity = getDisparity(0);
			final double [] strength =  getStrength();
			final boolean [] has_lma =  getLMA();

			final double absMinDisparity = 0.5 * disparityFar; // adjust? below this is definitely wrong (weak)
			final double absMaxDisparity = 1.5 * disparityNear; // change?
			final int dbg_nTile = (debugLevel > 0) ? 43493: -1; // x=77,y=134; // 42228; // x = 108, y = 130 46462; // 41545;
@@ -674,7 +717,7 @@ public class CLTPass3d{
					@Override
					public void run() {
						for (int nTile = ai.getAndIncrement(); nTile < nTiles; nTile = ai.getAndIncrement()) {
							if (((strength[nTile] < weakStrength) ||
							if (((!has_lma[nTile] && (strength[nTile] < weakStrength)) ||
									(disparity[nTile] < absMinDisparity) ||
									(disparity[nTile] > absMaxDisparity))&& ((selection == null) || selection[nTile])) {
								if (nTile == dbg_nTile){
@@ -698,6 +741,9 @@ public class CLTPass3d{
												 (disparity[nTile1] >= disparityFar) && // don't count on too near/too far for averaging
												 (disparity[nTile1] <= disparityNear)){
											double w = strength[nTile1];
											if (has_lma[nTile1]) {
												w *= scale_strength_lma;
											}
											sw += w;
											sd += w * disparity[nTile1];
											hasNeighbors = true;
@@ -742,12 +788,18 @@ public class CLTPass3d{
									int nTile1 = nTile + dirs[dir];
									if (!weakOutliers[nTile1] && ((selection == null) || selection[nTile1 ]) ) {
										double w = strength[nTile1];
										if (has_lma[nTile1]) {
											w *= scale_strength_lma;
										}
										sw += w;
										sd += w * src_disparity[nTile1];
									}
								}
								if (sw == 0) { // Nothing strong around - repeat with weak and this one too.
									double w = strength[nTile];
									if (has_lma[nTile]) {
										w *= scale_strength_lma;
									}
									if (!Double.isNaN( src_disparity[nTile])) {
										sw += w;
										sd += w * src_disparity[nTile];
@@ -756,6 +808,9 @@ public class CLTPass3d{
										int nTile1 = nTile + dirs[dir];
										if ((selection == null) || selection[nTile1 ]) {
											w = strength[nTile1];
											if (has_lma[nTile1]) {
												w *= scale_strength_lma;
											}
											if (!Double.isNaN( src_disparity[nTile1])) {
												sw += w;
												sd += w * src_disparity[nTile1];
+5 −1
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ public class Corr2dLMA {
///	final static int          NUM_CAMS =     4; // not all have to be used, so it is maximal number of cameras
///	final static int          NUM_PAIRS =    NUM_CAMS* (NUM_CAMS -1)/2; // number of possible pairs
	final static int          NTILE0 = 0;
	final static int          DISP_INDEX =   0; // common/average disparity
	final static int          DISP_INDEX =   0; // common/average disparity (negative!)
	final static int          A_INDEX =      1; // A*(x-x0)^2
	final static int          B_INDEX =      2; // 2*B*(x-x0)*(y-y0)
	final static int          CMA_INDEX =    3; // C*(y-y0)^2, encode C-A
@@ -2116,6 +2116,7 @@ public class Corr2dLMA {

	
	public double [][] lmaDisparityStrength(
			double  lmas_min_amp,     // minimal ratio of minimal pair correlation amplitude to maximal pair correlation amplitude
			double  lma_max_rel_rms,  // maximal relative (to average max/min amplitude LMA RMS) // May be up to 0.3)
			double  lma_min_strength, // minimal composite strength (sqrt(average amp squared over absolute RMS)
			double  lma_min_max_ac,   // minimal of A and C coefficients maximum (measures sharpest point/line)
@@ -2136,6 +2137,9 @@ public class Corr2dLMA {
			if (maxmin_amp[tile][1] < 0.0) {
				continue; // inverse maximum - discard tile
			}
			if ((maxmin_amp[tile][1]/maxmin_amp[tile][0]) < lmas_min_amp) {
				continue; // inverse maximum - discard tile
			}
			double avg = 0.50*(maxmin_amp[tile][0]+maxmin_amp[tile][1]); // max_min[1] can be negative - filter it out?
			double rrms = rms[tile]/avg;
			if (((lma_max_rel_rms > 0.0) && (rrms > lma_max_rel_rms)) ||
+3 −0
Original line number Diff line number Diff line
@@ -3422,6 +3422,7 @@ public class Correlation2d {

    		//    		double [][] ds =         lma.getDisparityStrength();
    		ds = lma.lmaDisparityStrength(
    				imgdtt_params.lmas_min_amp,      //  minimal ratio of minimal pair correlation amplitude to maximal pair correlation amplitude
    				imgdtt_params.lma_max_rel_rms,  // maximal relative (to average max/min amplitude LMA RMS) // May be up to 0.3)
    				imgdtt_params.lma_min_strength, // minimal composite strength (sqrt(average amp squared over absolute RMS)
    				imgdtt_params.lma_min_ac,       // minimal of A and C coefficients maximum (measures sharpest point/line)
@@ -3521,6 +3522,7 @@ public class Correlation2d {
        			}
    				//        		double [][] ds =         lma.getDisparityStrength();
    				ds = lma.lmaDisparityStrength(
    	    				imgdtt_params.lmas_min_amp,      //  minimal ratio of minimal pair correlation amplitude to maximal pair correlation amplitude
    						imgdtt_params.lma_max_rel_rms,  // maximal relative (to average max/min amplitude LMA RMS) // May be up to 0.3)
    						imgdtt_params.lma_min_strength, // minimal composite strength (sqrt(average amp squared over absolute RMS)
    						imgdtt_params.lma_min_ac,       // minimal of A and C coefficients maximum (measures sharpest point/line)
@@ -3858,6 +3860,7 @@ public class Correlation2d {


    		double [][] dispStr = lma.lmaDisparityStrength( //TODO: add parameter to filter out negative minimums ?
    				imgdtt_params.lmas_min_amp,      //  minimal ratio of minimal pair correlation amplitude to maximal pair correlation amplitude
    				imgdtt_params.lmas_max_rel_rms,  // maximal relative (to average max/min amplitude LMA RMS) // May be up to 0.3)
    				imgdtt_params.lmas_min_strength, // minimal composite strength (sqrt(average amp squared over absolute RMS)
    				imgdtt_params.lmas_min_ac,       // minimal of A and C coefficients maximum (measures sharpest point/line)
+8 −1
Original line number Diff line number Diff line
@@ -268,6 +268,7 @@ public class DisparityProcessor {
			final double  []  disparity,          // current disparity value
			final double  []  measured_disparity, // measured disparity
			final double  []  strength,
			final boolean []  has_lma, 
			final double  []  hor_disparity, // not yet used
			final double  []  hor_strength, // not yet used
			final boolean []  selected,
@@ -276,6 +277,7 @@ public class DisparityProcessor {
			final int         threadsMax,      // maximal number of threads to launch                         
			final int         debugLevel)
	{
		final double  scale_strength_lma = 5.0; // increase LMA-defined strength during averaging
		final int dbg_tile = -1; // 28643; // x=131, y=88
		ShowDoubleFloatArrays sdfa_instance = null;
		if (debugLevel > 0) sdfa_instance = new ShowDoubleFloatArrays(); // just for debugging?
@@ -382,7 +384,12 @@ public class DisparityProcessor {
//								}
								// calculate pull by the measured disparity
								double disparity_diff = (measured_disparity[nTile] - disp_data[0][nTile]);
								double eff_strength = ((border != null) && border[nTile])? 0.0: (strength[nTile] - clt_parameters.tiStrengthOffset);
								double str = strength[nTile];
								if (has_lma[nTile]) {
									str *= scale_strength_lma;
								}
//								double eff_strength = ((border != null) && border[nTile])? 0.0: (strength[nTile] - clt_parameters.tiStrengthOffset);
								double eff_strength = ((border != null) && border[nTile])? 0.0: (str - clt_parameters.tiStrengthOffset);
								if (eff_strength < 0) eff_strength = 0;
								double disparity_pull = eff_strength;
//								if (tileY == 89){
+2 −22
Original line number Diff line number Diff line
@@ -3291,6 +3291,7 @@ public class ImageDtt extends ImageDttCPU {
							if (dbg_img != null) dbg_img[1][nclust] = 1.0;
							// was for single tile
							disp_str = lma2.lmaDisparityStrength(
				    				imgdtt_params.lmas_min_amp,      //  minimal ratio of minimal pair correlation amplitude to maximal pair correlation amplitude
									imgdtt_params.lmas_max_rel_rms,  // maximal relative (to average max/min amplitude LMA RMS) // May be up to 0.3)
									imgdtt_params.lmas_min_strength, // minimal composite strength (sqrt(average amp squared over absolute RMS)
									imgdtt_params.lmas_min_ac,       // minimal of A and C coefficients maximum (measures sharpest point/line)
@@ -3583,31 +3584,10 @@ public class ImageDtt extends ImageDttCPU {
								tileX,                        // int                 tileX, // just for debug output
								tileY);                       // int                 tileY
						
						
						/*
						double [][] poly_disp2 = {{Double.NaN, 0.0}};
						double [][][] corrs2 = {corrs};
						double [][][] tile_disp_dist2 = {tile_disp_dist};
						// TODO: maybe use corrLMA2Single again, but take care of initVector!
						Corr2dLMA lma2 = corr2d.corrLMA2Multi( // multitile num_tiles_super == 1
								imgdtt_params,                // ImageDttParameters  imgdtt_params,
								1,                            // int                 clust_width,
								corr_wnd,                     // double [][]         corr_wnd, // correlation window to save on re-calculation of the window
								corr_wnd_inv_limited,         // corr_wnd_limited, // correlation window, limited not to be smaller than threshold - used for finding max/convex areas (or null)
								corrs2, // corrs,                        // double [][]         corrs,
								tile_disp_dist2,
								rXY,                          // double [][]         rXY, // non-distorted X,Y offset per nominal pixel of disparity
								imgdtt_params.dbg_pair_mask,  // int                 pair_mask, // which pairs to process
//								null,                         // disp_str[cTile],  //corr_stat[0],                 // double    xcenter,   // preliminary center x in pixels for largest baseline
								poly_disp2,                    // double[]            poly_ds,    // null or pair of disparity/strength
								imgdtt_params.ortho_vasw_pwr, // double    vasw_pwr,  // value as weight to this power,
								tdl, // tile_lma_debug_level, //+2,         // int                 debug_level,
								tileX,                        // int                 tileX, // just for debug output
								tileY);                       // int                 tileY
						*/
						if (lma2 != null) {
							// was for single tile
							disp_str = lma2.lmaDisparityStrength(
				    				imgdtt_params.lmas_min_amp,      //  minimal ratio of minimal pair correlation amplitude to maximal pair correlation amplitude
									imgdtt_params.lmas_max_rel_rms,  // maximal relative (to average max/min amplitude LMA RMS) // May be up to 0.3)
									imgdtt_params.lmas_min_strength, // minimal composite strength (sqrt(average amp squared over absolute RMS)
									imgdtt_params.lmas_min_ac,       // minimal of A and C coefficients maximum (measures sharpest point/line)
Loading