def main(): fnames = ['aloe-texture', 'cones-texture', 'rocks1-texture', 'teddy-texture', 'garnics1-texture', 'box-1-texture', 'scene05-texture', 'scene01-texture', 'table1-texture', 'blue-pan-texture', 'red-plate-texture', 'lampshade1-texture', 'breakfast-1-texture', 'plastic-texture', 'cups1-texture'] path = './texture-evaluation/texture-values/' for fname in fnames: arr = np.loadtxt(path+fname+'.dat', dtype=float) plt.plot_array('Texture', arr, 2, 6, path + 'plots/' + fname)
def main(): fnames = [ 'aloe-texture', 'cones-texture', 'rocks1-texture', 'teddy-texture', 'garnics1-texture', 'box-1-texture', 'scene05-texture', 'scene01-texture', 'table1-texture', 'blue-pan-texture', 'red-plate-texture', 'lampshade1-texture', 'breakfast-1-texture', 'plastic-texture', 'cups1-texture' ] path = './texture-evaluation/texture-values/' for fname in fnames: arr = np.loadtxt(path + fname + '.dat', dtype=float) plt.plot_array('Texture', arr, 2, 6, path + 'plots/' + fname)
def view_disparity(path, disparity_min, disparity_max, occlusion_range): arr_sparse_disparity_incons = abs(np.loadtxt(path+'sparse_disparity_incons.dat')) arr_sparse_disparity_cons = abs(np.loadtxt(path+'sparse_disparity_cons.dat')) arr_average_disparity = abs(np.loadtxt(path+'average-disparity.dat')) arr_average_disparity_lines = abs(np.loadtxt(path+'average-disparity-lines.dat')) arr_edge_disparity = abs(np.loadtxt(path+'edge-disparity.dat')) arr_edge_disparity_final = abs(np.loadtxt(path+'edge-disparity-final.dat')) arr_initial_disparity = abs(np.loadtxt(path+'initial-disparity.dat')) arr_sigma_map = np.loadtxt(path+'sigma-map.dat') arr_occlusion_map = np.loadtxt(path+'occlusion-map.dat') arr_outliers_map = np.loadtxt(path+'outliers-map.dat') arr_edge_disparity_mask = np.loadtxt(path+'edge-disparity-mask.dat') arr_sparse_filtered = abs(np.loadtxt(path+'sparse-disparity-filtered.dat')) # get size height = arr_sparse_disparity_cons.shape[0] width = arr_sparse_disparity_cons.shape[1] print 'height = ', height print 'width = ', width # plot inconsistent sparse disparity map min_val = disparity_min max_val = disparity_max arr_sparse_disparity_incons[0:height,0:occlusion_range] = 'nan' arr_average_disparity[0:height,0:occlusion_range] = 'nan' arr_average_disparity_lines[0:height,0:occlusion_range] = 'nan' arr_edge_disparity[0:height,0:occlusion_range] = 'nan' arr_edge_disparity_final[0:height,0:occlusion_range] = 'nan' arr_initial_disparity[0:height,0:occlusion_range] = 'nan' arr_occlusion_map[0:height,0:occlusion_range] = 'nan' arr_edge_disparity_mask[0:height,0:occlusion_range] = 'nan' arr_sigma_map[0:height,0:occlusion_range] = 'nan' arr_outliers_map[0:height,0:occlusion_range] = 'nan' plt.plot_array('Inconsistent sparse disparity map (phase-based)', arr_sparse_disparity_incons, min_val, max_val, path+'sparse-disparity-incons') plt.plot_array('Consistent sparse disparity map (phase-based)', arr_sparse_disparity_cons, min_val, max_val, path+'sparse-disparity-cons') plt.plot_array('Filtered sparse disparity map', arr_sparse_filtered, min_val, max_val, path+'sparse-disparity-filtered') plt.plot_array('Average disparity', arr_average_disparity, min_val, max_val, path+'average-disparity') plt.plot_array('Average disparity (lines)', arr_average_disparity_lines, min_val, max_val, path+'average-disparity-lines') # make borders thicker extend_border_horizontal(arr_edge_disparity) extend_border_vertical(arr_edge_disparity) extend_border_horizontal(arr_edge_disparity_final) extend_border_vertical(arr_edge_disparity_final) extend_sigma_horizontal(arr_sigma_map,0.3) extend_sigma_vertical(arr_sigma_map,0.3) # replace zeros by NaN values for i in range(1,height): for j in range(1,width): if(arr_edge_disparity_final[i,j] != 0): arr_initial_disparity[i,j] = arr_edge_disparity_final[i,j] if(arr_edge_disparity[i,j] == 0): arr_edge_disparity[i,j] = 'nan' if(arr_edge_disparity_final[i,j] == 0): arr_edge_disparity_final[i,j] = 'nan' if(arr_edge_disparity_mask[i,j] == 0): arr_edge_disparity_mask[i,j] = 'nan' if(arr_occlusion_map[i,j] == 0): arr_occlusion_map[i,j] = 'nan' if(arr_outliers_map[i,j] == 0): arr_outliers_map[i,j] = 'nan' plt.plot_array('Edge disparity', arr_edge_disparity, min_val, max_val, path+'edge-disparity') plt.plot_array('Final edge disparity', arr_edge_disparity_final, min_val, max_val, path+'edge-disparity-final') plt.plot_array('Initial disparity', arr_initial_disparity, min_val, max_val, path+'initial-disparity') #plot_array('Sigma map', arr_sigma_map, arr_sigma_map.min(), arr_sigma_map.max(), 'sigma-map') plt.plot_array('Sigma map', arr_sigma_map, 0, 1, path+'sigma-map') #plot_array('Occlusion map', arr_occlusion_map, arr_occlusion_map.min(), arr_occlusion_map.max(), 'occlusion-map') plt.plot_array('Occlusion map', arr_occlusion_map, 0, 1, path+'occlusion-map') plt.plot_array('Outliers map', arr_outliers_map, 0, 1, path+'outliers-map') plt.plot_array('Edge disparity mask', arr_edge_disparity_mask, 0, 1, path+'edge-disparity-mask')
def surface_fitting_robust_lsq(local_folder, arr_segments, arr_init_disp, arr_edge_disp, disp_min, disp_max): # get segment labels labels = set(np.unique(arr_segments)).difference((0,)) # print 'labels = ', labels # matrix with regression weights arr_regr_weights_init = np.zeros(arr_init_disp.shape, dtype=float) arr_regr_weights_init[arr_init_disp.nonzero()] = 0.3 arr_regr_weights_init[arr_edge_disp.nonzero()] = 1.0 # initial sparse disparity maps without outliers for a specific model disparity_init_const = np.zeros(arr_init_disp.shape, dtype=float) disparity_init_planar = np.zeros(arr_init_disp.shape, dtype=float) disparity_init_quadric = np.zeros(arr_init_disp.shape, dtype=float) # weights for sparse disparity values without outliers weights_const = np.zeros(arr_init_disp.shape, dtype=float) weights_planar = np.zeros(arr_init_disp.shape, dtype=float) weights_quadric = np.zeros(arr_init_disp.shape, dtype=float) # dense disparity maps obtained using a specific model disparity_const = np.zeros(arr_init_disp.shape, dtype=float) disparity_planar = np.zeros(arr_init_disp.shape, dtype=float) disparity_quadric = np.zeros(arr_init_disp.shape, dtype=float) # final dense disparity map after the model selection disparity_final = np.zeros(arr_init_disp.shape, dtype=float) t1 = dt.datetime.now() # go over all segments and fit sparse disparity input to the model for label in labels: # print '' # print 'current label = ', label # masks of the considered segment and its sparse disparity map segment_mask = (arr_segments == label) * 1 disparity_mask = (arr_init_disp > 0) * segment_mask * 1 # disparity of the current segment disparity = arr_init_disp * disparity_mask if disparity.sum() == 0: continue segment_regr_weights = np.zeros(disparity.shape, dtype=float) segment_regr_weights = (disparity > 0) * arr_regr_weights_init # get the robust least squares solution for the current segment # and for const, planar, and quadric models # get indices of given disparity values indices = disparity.nonzero() x = np.zeros(len(indices[1])) y = np.zeros(len(indices[0])) x[:] = indices[1] y[:] = indices[0] z_ones = np.ones([x.size, 1]) Z = disparity segment_regr_weights_fl = segment_regr_weights[segment_regr_weights.nonzero()].flatten() segment_regr_weights = np.reshape(segment_regr_weights_fl, ([len(segment_regr_weights_fl), 1])) # constant model X = z_ones a_lsq_const, abs_res_scale_const = robust_lsq.get_dense_disparity_robust_lsq(X, Z, segment_regr_weights) mad_const, w_const, d_const = estimate_weights.estimate_weights( X, Z, a_lsq_const, abs_res_scale_const, segment_regr_weights, disparity.shape ) # planar model X = np.hstack((np.reshape(x, ([len(x), 1])), np.reshape(y, ([len(y), 1])), z_ones)) a_lsq_planar, abs_res_scale_planar = robust_lsq.get_dense_disparity_robust_lsq(X, Z, segment_regr_weights) mad_planar, w_planar, d_planar = estimate_weights.estimate_weights( X, Z, a_lsq_planar, abs_res_scale_planar, segment_regr_weights, disparity.shape ) # quadric model x2 = x ** 2 y2 = y ** 2 xy = x * y # v1 = np.reshape(x2, ([len(x2),1])) # v2 = np.reshape(y2, ([len(y2),1])) # v3 = np.reshape(x, ([len(x),1])) # v4 = np.reshape(y, ([len(y),1])) v1 = np.reshape(x2, ([len(x2), 1])) v2 = np.reshape(y2, ([len(y2), 1])) v3 = np.reshape(xy, ([len(xy), 1])) v4 = np.reshape(x, ([len(x), 1])) v5 = np.reshape(y, ([len(y), 1])) # X = np.hstack((v1, v2, v3, v4, z_ones)) X = np.hstack((v1, v2, v3, v4, v5, z_ones)) a_lsq_quadric, abs_res_scale_quadric = robust_lsq.get_dense_disparity_robust_lsq(X, Z, segment_regr_weights) mad_quadric, w_quadric, d_quadric = estimate_weights.estimate_weights( X, Z, a_lsq_quadric, abs_res_scale_quadric, segment_regr_weights, disparity.shape ) # calculate disparity for the current segment based on the found model segment_indices = segment_mask.nonzero() disparity_init_const[segment_indices] = d_const[segment_indices] disparity_init_planar[segment_indices] = d_planar[segment_indices] disparity_init_quadric[segment_indices] = d_quadric[segment_indices] weights_const[segment_indices] = w_const[segment_indices] weights_planar[segment_indices] = w_planar[segment_indices] weights_quadric[segment_indices] = w_quadric[segment_indices] x_s = np.zeros(len(segment_indices[1])) y_s = np.zeros(len(segment_indices[0])) x_s[:] = segment_indices[1] y_s[:] = segment_indices[0] # calculate disparity values according to found models disparity_const[segment_indices] = a_lsq_const[0] disparity_planar[segment_indices] = a_lsq_planar[0] * x_s + a_lsq_planar[1] * y_s + a_lsq_planar[2] # disparity_quadric[ segment_indices ] = a_lsq_quadric[0]*x_s*x_s + a_lsq_quadric[1]*y_s*y_s + a_lsq_quadric[2]*x_s + a_lsq_quadric[3]*y_s + a_lsq_quadric[4] disparity_quadric[segment_indices] = ( a_lsq_quadric[0] * x_s * x_s + a_lsq_quadric[1] * y_s * y_s + a_lsq_quadric[2] * x_s * y_s + a_lsq_quadric[3] * x_s + a_lsq_quadric[4] * y_s + a_lsq_quadric[5] ) # select the best model winner = np.argmin([mad_const, mad_planar, mad_quadric]) if winner == 0: disparity_final[segment_indices] = disparity_const[segment_indices] elif winner == 1: disparity_final[segment_indices] = disparity_planar[segment_indices] elif winner == 2: disparity_final[segment_indices] = disparity_quadric[segment_indices] # calculated elapsed time t2 = dt.datetime.now() ms = (t2 - t1).microseconds / 1e3 print "Robust LSQ, elapsed time = ", ms, " ms" print "" # replace zeros by NaN values just for displaying disparity_init_const[(disparity_init_const == 0).nonzero()] = "nan" disparity_init_planar[(disparity_init_planar == 0).nonzero()] = "nan" disparity_init_quadric[(disparity_init_quadric == 0).nonzero()] = "nan" arr_regr_weights_init[(arr_regr_weights_init == 0).nonzero()] = "nan" weights_const[(weights_const == 0).nonzero()] = "nan" weights_planar[(weights_planar == 0).nonzero()] = "nan" weights_quadric[(weights_quadric == 0).nonzero()] = "nan" plt.plot_array( "Initial disparity (robust, const)", disparity_init_const, disp_min, disp_max, local_folder + "init-disparity-rob-const", ) plt.plot_array( "Initial disparity (robust, planar)", disparity_init_planar, disp_min, disp_max, local_folder + "init-disparity-rob-planar", ) plt.plot_array( "Initial disparity (robust, quadric)", disparity_init_quadric, disp_min, disp_max, local_folder + "init-disparity-rob-quadric", ) plt.plot_array("Regression weights", arr_regr_weights_init, 0, 1, local_folder + "regression-weight") w_min = 0.0 w_max = 0.37 plt.plot_array("Weights (robust, const)", weights_const, w_min, w_max, local_folder + "weights-rob-const") plt.plot_array("Weights (robust, planar)", weights_planar, w_min, w_max, local_folder + "weights-rob-planar") plt.plot_array("Weights (robust, quadric)", weights_quadric, w_min, w_max, local_folder + "weights-rob-quadric") return disparity_const, disparity_planar, disparity_quadric, disparity_final
def view_disparity(path, disparity_min, disparity_max, occlusion_range): arr_sparse_disparity_incons = abs( np.loadtxt(path + 'sparse_disparity_incons.dat')) arr_sparse_disparity_cons = abs( np.loadtxt(path + 'sparse_disparity_cons.dat')) arr_average_disparity = abs(np.loadtxt(path + 'average-disparity.dat')) arr_average_disparity_lines = abs( np.loadtxt(path + 'average-disparity-lines.dat')) arr_edge_disparity = abs(np.loadtxt(path + 'edge-disparity.dat')) arr_edge_disparity_final = abs( np.loadtxt(path + 'edge-disparity-final.dat')) arr_initial_disparity = abs(np.loadtxt(path + 'initial-disparity.dat')) arr_sigma_map = np.loadtxt(path + 'sigma-map.dat') arr_occlusion_map = np.loadtxt(path + 'occlusion-map.dat') arr_outliers_map = np.loadtxt(path + 'outliers-map.dat') arr_edge_disparity_mask = np.loadtxt(path + 'edge-disparity-mask.dat') arr_sparse_filtered = abs( np.loadtxt(path + 'sparse-disparity-filtered.dat')) # get size height = arr_sparse_disparity_cons.shape[0] width = arr_sparse_disparity_cons.shape[1] print 'height = ', height print 'width = ', width # plot inconsistent sparse disparity map min_val = disparity_min max_val = disparity_max arr_sparse_disparity_incons[0:height, 0:occlusion_range] = 'nan' arr_average_disparity[0:height, 0:occlusion_range] = 'nan' arr_average_disparity_lines[0:height, 0:occlusion_range] = 'nan' arr_edge_disparity[0:height, 0:occlusion_range] = 'nan' arr_edge_disparity_final[0:height, 0:occlusion_range] = 'nan' arr_initial_disparity[0:height, 0:occlusion_range] = 'nan' arr_occlusion_map[0:height, 0:occlusion_range] = 'nan' arr_edge_disparity_mask[0:height, 0:occlusion_range] = 'nan' arr_sigma_map[0:height, 0:occlusion_range] = 'nan' arr_outliers_map[0:height, 0:occlusion_range] = 'nan' plt.plot_array('Inconsistent sparse disparity map (phase-based)', arr_sparse_disparity_incons, min_val, max_val, path + 'sparse-disparity-incons') plt.plot_array('Consistent sparse disparity map (phase-based)', arr_sparse_disparity_cons, min_val, max_val, path + 'sparse-disparity-cons') plt.plot_array('Filtered sparse disparity map', arr_sparse_filtered, min_val, max_val, path + 'sparse-disparity-filtered') plt.plot_array('Average disparity', arr_average_disparity, min_val, max_val, path + 'average-disparity') plt.plot_array('Average disparity (lines)', arr_average_disparity_lines, min_val, max_val, path + 'average-disparity-lines') # make borders thicker extend_border_horizontal(arr_edge_disparity) extend_border_vertical(arr_edge_disparity) extend_border_horizontal(arr_edge_disparity_final) extend_border_vertical(arr_edge_disparity_final) extend_sigma_horizontal(arr_sigma_map, 0.3) extend_sigma_vertical(arr_sigma_map, 0.3) # replace zeros by NaN values for i in range(1, height): for j in range(1, width): if (arr_edge_disparity_final[i, j] != 0): arr_initial_disparity[i, j] = arr_edge_disparity_final[i, j] if (arr_edge_disparity[i, j] == 0): arr_edge_disparity[i, j] = 'nan' if (arr_edge_disparity_final[i, j] == 0): arr_edge_disparity_final[i, j] = 'nan' if (arr_edge_disparity_mask[i, j] == 0): arr_edge_disparity_mask[i, j] = 'nan' if (arr_occlusion_map[i, j] == 0): arr_occlusion_map[i, j] = 'nan' if (arr_outliers_map[i, j] == 0): arr_outliers_map[i, j] = 'nan' plt.plot_array('Edge disparity', arr_edge_disparity, min_val, max_val, path + 'edge-disparity') plt.plot_array('Final edge disparity', arr_edge_disparity_final, min_val, max_val, path + 'edge-disparity-final') plt.plot_array('Initial disparity', arr_initial_disparity, min_val, max_val, path + 'initial-disparity') #plot_array('Sigma map', arr_sigma_map, arr_sigma_map.min(), arr_sigma_map.max(), 'sigma-map') plt.plot_array('Sigma map', arr_sigma_map, 0, 1, path + 'sigma-map') #plot_array('Occlusion map', arr_occlusion_map, arr_occlusion_map.min(), arr_occlusion_map.max(), 'occlusion-map') plt.plot_array('Occlusion map', arr_occlusion_map, 0, 1, path + 'occlusion-map') plt.plot_array('Outliers map', arr_outliers_map, 0, 1, path + 'outliers-map') plt.plot_array('Edge disparity mask', arr_edge_disparity_mask, 0, 1, path + 'edge-disparity-mask')
def surface_fitting_robust_lsq(local_folder, arr_segments, arr_init_disp, arr_edge_disp, disp_min, disp_max): # get segment labels labels = set(np.unique(arr_segments)).difference((0, )) #print 'labels = ', labels # matrix with regression weights arr_regr_weights_init = np.zeros(arr_init_disp.shape, dtype=float) arr_regr_weights_init[arr_init_disp.nonzero()] = 0.3 arr_regr_weights_init[arr_edge_disp.nonzero()] = 1.0 # initial sparse disparity maps without outliers for a specific model disparity_init_const = np.zeros(arr_init_disp.shape, dtype=float) disparity_init_planar = np.zeros(arr_init_disp.shape, dtype=float) disparity_init_quadric = np.zeros(arr_init_disp.shape, dtype=float) # weights for sparse disparity values without outliers weights_const = np.zeros(arr_init_disp.shape, dtype=float) weights_planar = np.zeros(arr_init_disp.shape, dtype=float) weights_quadric = np.zeros(arr_init_disp.shape, dtype=float) # dense disparity maps obtained using a specific model disparity_const = np.zeros(arr_init_disp.shape, dtype=float) disparity_planar = np.zeros(arr_init_disp.shape, dtype=float) disparity_quadric = np.zeros(arr_init_disp.shape, dtype=float) # final dense disparity map after the model selection disparity_final = np.zeros(arr_init_disp.shape, dtype=float) t1 = dt.datetime.now() # go over all segments and fit sparse disparity input to the model for label in labels: #print '' #print 'current label = ', label # masks of the considered segment and its sparse disparity map segment_mask = (arr_segments == label) * 1 disparity_mask = (arr_init_disp > 0) * segment_mask * 1 # disparity of the current segment disparity = arr_init_disp * disparity_mask if disparity.sum() == 0: continue segment_regr_weights = np.zeros(disparity.shape, dtype=float) segment_regr_weights = (disparity > 0) * arr_regr_weights_init # get the robust least squares solution for the current segment # and for const, planar, and quadric models # get indices of given disparity values indices = disparity.nonzero() x = np.zeros(len(indices[1])) y = np.zeros(len(indices[0])) x[:] = indices[1] y[:] = indices[0] z_ones = np.ones([x.size, 1]) Z = disparity segment_regr_weights_fl = segment_regr_weights[ segment_regr_weights.nonzero()].flatten() segment_regr_weights = np.reshape(segment_regr_weights_fl, ([len(segment_regr_weights_fl), 1])) # constant model X = z_ones a_lsq_const, abs_res_scale_const = robust_lsq.get_dense_disparity_robust_lsq( X, Z, segment_regr_weights) mad_const, w_const, d_const = estimate_weights.estimate_weights( X, Z, a_lsq_const, abs_res_scale_const, segment_regr_weights, disparity.shape) # planar model X = np.hstack( (np.reshape(x, ([len(x), 1])), np.reshape(y, ([len(y), 1])), z_ones)) a_lsq_planar, abs_res_scale_planar = robust_lsq.get_dense_disparity_robust_lsq( X, Z, segment_regr_weights) mad_planar, w_planar, d_planar = estimate_weights.estimate_weights( X, Z, a_lsq_planar, abs_res_scale_planar, segment_regr_weights, disparity.shape) # quadric model x2 = x**2 y2 = y**2 xy = x * y #v1 = np.reshape(x2, ([len(x2),1])) #v2 = np.reshape(y2, ([len(y2),1])) #v3 = np.reshape(x, ([len(x),1])) #v4 = np.reshape(y, ([len(y),1])) v1 = np.reshape(x2, ([len(x2), 1])) v2 = np.reshape(y2, ([len(y2), 1])) v3 = np.reshape(xy, ([len(xy), 1])) v4 = np.reshape(x, ([len(x), 1])) v5 = np.reshape(y, ([len(y), 1])) #X = np.hstack((v1, v2, v3, v4, z_ones)) X = np.hstack((v1, v2, v3, v4, v5, z_ones)) a_lsq_quadric, abs_res_scale_quadric = robust_lsq.get_dense_disparity_robust_lsq( X, Z, segment_regr_weights) mad_quadric, w_quadric, d_quadric = estimate_weights.estimate_weights( X, Z, a_lsq_quadric, abs_res_scale_quadric, segment_regr_weights, disparity.shape) # calculate disparity for the current segment based on the found model segment_indices = segment_mask.nonzero() disparity_init_const[segment_indices] = d_const[segment_indices] disparity_init_planar[segment_indices] = d_planar[segment_indices] disparity_init_quadric[segment_indices] = d_quadric[segment_indices] weights_const[segment_indices] = w_const[segment_indices] weights_planar[segment_indices] = w_planar[segment_indices] weights_quadric[segment_indices] = w_quadric[segment_indices] x_s = np.zeros(len(segment_indices[1])) y_s = np.zeros(len(segment_indices[0])) x_s[:] = segment_indices[1] y_s[:] = segment_indices[0] # calculate disparity values according to found models disparity_const[segment_indices] = a_lsq_const[0] disparity_planar[segment_indices] = a_lsq_planar[ 0] * x_s + a_lsq_planar[1] * y_s + a_lsq_planar[2] #disparity_quadric[ segment_indices ] = a_lsq_quadric[0]*x_s*x_s + a_lsq_quadric[1]*y_s*y_s + a_lsq_quadric[2]*x_s + a_lsq_quadric[3]*y_s + a_lsq_quadric[4] disparity_quadric[ segment_indices] = a_lsq_quadric[0] * x_s * x_s + a_lsq_quadric[ 1] * y_s * y_s + a_lsq_quadric[2] * x_s * y_s + a_lsq_quadric[ 3] * x_s + a_lsq_quadric[4] * y_s + a_lsq_quadric[5] # select the best model winner = np.argmin([mad_const, mad_planar, mad_quadric]) if winner == 0: disparity_final[segment_indices] = disparity_const[segment_indices] elif winner == 1: disparity_final[segment_indices] = disparity_planar[ segment_indices] elif winner == 2: disparity_final[segment_indices] = disparity_quadric[ segment_indices] # calculated elapsed time t2 = dt.datetime.now() ms = (t2 - t1).microseconds / 1e3 print 'Robust LSQ, elapsed time = ', ms, ' ms' print '' # replace zeros by NaN values just for displaying disparity_init_const[(disparity_init_const == 0).nonzero()] = 'nan' disparity_init_planar[(disparity_init_planar == 0).nonzero()] = 'nan' disparity_init_quadric[(disparity_init_quadric == 0).nonzero()] = 'nan' arr_regr_weights_init[(arr_regr_weights_init == 0).nonzero()] = 'nan' weights_const[(weights_const == 0).nonzero()] = 'nan' weights_planar[(weights_planar == 0).nonzero()] = 'nan' weights_quadric[(weights_quadric == 0).nonzero()] = 'nan' plt.plot_array('Initial disparity (robust, const)', disparity_init_const, disp_min, disp_max, local_folder + 'init-disparity-rob-const') plt.plot_array('Initial disparity (robust, planar)', disparity_init_planar, disp_min, disp_max, local_folder + 'init-disparity-rob-planar') plt.plot_array('Initial disparity (robust, quadric)', disparity_init_quadric, disp_min, disp_max, local_folder + 'init-disparity-rob-quadric') plt.plot_array('Regression weights', arr_regr_weights_init, 0, 1, local_folder + 'regression-weight') w_min = 0.0 w_max = 0.37 plt.plot_array('Weights (robust, const)', weights_const, w_min, w_max, local_folder + 'weights-rob-const') plt.plot_array('Weights (robust, planar)', weights_planar, w_min, w_max, local_folder + 'weights-rob-planar') plt.plot_array('Weights (robust, quadric)', weights_quadric, w_min, w_max, local_folder + 'weights-rob-quadric') return disparity_const, disparity_planar, disparity_quadric, disparity_final
def surface_fitting(output_path, occlusion_range, disp_min, disp_max): #fname_segments = './segments-left.dat' # input left segments #fname_cons_disp = './sparse_disparity_cons.dat' # consistent sparse disparity estimated by the phase-based method #fname_edge_disp = './edge-disparity-final.dat' # edge disparity map #fname_init_disp = './initial-disparity.dat' # initial disparity map fname_segments = output_path + 'segments-left.dat' # input left segments fname_cons_disp = output_path + 'sparse_disparity_cons.dat' # consistent sparse disparity estimated by the phase-based method fname_edge_disp = output_path + 'edge-disparity-final.dat' # edge disparity map fname_init_disp = output_path + 'initial-disparity.dat' # initial disparity map #local_folder = './tmp/' # folder for final results #occlusion_range = 32 # range of a half-occlusion area (for left segments) # min and max disparity values for color scaling #disp_min = 0 #disp_max = 50#45#48#50 arr_segments = np.loadtxt(fname_segments, dtype=float) arr_cons_disp = abs(np.loadtxt(fname_cons_disp, dtype=float)) arr_edge_disp = np.loadtxt(fname_edge_disp, dtype=float) arr_init_disp = np.loadtxt(fname_init_disp, dtype=float) # replace 'NaN's by zeros arr_init_disp = np.nan_to_num(arr_init_disp) arr_cons_disp = np.nan_to_num(arr_cons_disp) # clean input segments and find regions remained unfound after segmentation arr_segments, arr_segments_gaps, arr_segments_final = find_small_segments.detect_small_segments(arr_segments, occlusion_range) # save clean segments, segmentation gaps and final segments (clean segments + filled gaps) segment_colors = plt.generate_colors() find_small_segments.save_segments(output_path + 'segments-left-original', arr_segments, segment_colors) find_small_segments.save_segments(output_path + 'segments-left-gaps', arr_segments_gaps, segment_colors) find_small_segments.save_segments(output_path + 'segments-left-final', arr_segments_final, segment_colors) # for segments with a few estimations in the initial map use input consistent disparity arr_init_disp = disparity_operations.complement_initial_disparity(arr_segments, arr_init_disp, arr_cons_disp) # CASE 1: regular least squares (for both segment gaps and segments) disparity_gaps_reg_const, disparity_gaps_reg_planar, disparity_gaps_reg_quadric = fitting_regular_lsq.surface_fitting_regular_lsq(arr_segments_gaps, arr_cons_disp) disparity_reg_const, disparity_reg_planar, disparity_reg_quadric = fitting_regular_lsq.surface_fitting_regular_lsq(arr_segments, arr_init_disp) # go over gap labels and fill gaps in all maps by const disparity values for lb in set(np.unique(arr_segments_gaps)).difference((0,)): disparity_reg_const[ (arr_segments_gaps == lb).nonzero() ] = disparity_gaps_reg_const[ (arr_segments_gaps == lb).nonzero() ] disparity_reg_planar[ (arr_segments_gaps == lb).nonzero() ] = disparity_gaps_reg_const[ (arr_segments_gaps == lb).nonzero() ] disparity_reg_quadric[ (arr_segments_gaps == lb).nonzero() ] = disparity_gaps_reg_const[ (arr_segments_gaps == lb).nonzero() ] # CASE 2: robust least squares (for both segment gaps and segments) disparity_gaps_rob_const, disparity_gaps_rob_planar, disparity_gaps_rob_quadric, disparity_gaps_rob_final = fitting_robust_lsq.surface_fitting_robust_lsq(output_path, arr_segments_gaps, arr_cons_disp, arr_edge_disp, disp_min, disp_max) disparity_rob_const, disparity_rob_planar, disparity_rob_quadric, disparity_rob_final = fitting_robust_lsq.surface_fitting_robust_lsq(output_path, arr_segments, arr_init_disp, arr_edge_disp, disp_min, disp_max) # go over gap labels and fill gaps in all maps by const disparity values for lb in set(np.unique(arr_segments_gaps)).difference((0,)): disparity_rob_const[ (arr_segments_gaps == lb).nonzero() ] = disparity_gaps_rob_const[ (arr_segments_gaps == lb).nonzero() ] disparity_rob_planar[ (arr_segments_gaps == lb).nonzero() ] = disparity_gaps_rob_const[ (arr_segments_gaps == lb).nonzero() ] disparity_rob_quadric[ (arr_segments_gaps == lb).nonzero() ] = disparity_gaps_rob_const[ (arr_segments_gaps == lb).nonzero() ] disparity_rob_final[ (arr_segments_gaps == lb).nonzero() ] = disparity_gaps_rob_const[ (arr_segments_gaps == lb).nonzero() ] # perform interpolation of disparity values for empty pixels disparity_reg_const = disparity_operations.disparity_interpolation(disparity_reg_const, arr_segments_final, occlusion_range) disparity_reg_planar = disparity_operations.disparity_interpolation(disparity_reg_planar, arr_segments_final, occlusion_range) disparity_reg_quadric = disparity_operations.disparity_interpolation(disparity_reg_quadric, arr_segments_final, occlusion_range) disparity_rob_const = disparity_operations.disparity_interpolation(disparity_rob_const, arr_segments_final, occlusion_range) disparity_rob_planar = disparity_operations.disparity_interpolation(disparity_rob_planar, arr_segments_final, occlusion_range) disparity_rob_quadric = disparity_operations.disparity_interpolation(disparity_rob_quadric, arr_segments_final, occlusion_range) disparity_rob_final = disparity_operations.disparity_interpolation(disparity_rob_final, arr_segments_final, occlusion_range) # PLOT DATA # replace zeros by NaN to make them white in plots arr_edge_disp[ (arr_edge_disp == 0).nonzero() ] = 'nan' arr_init_disp[ (arr_init_disp == 0).nonzero() ] = 'nan' arr_cons_disp[ (arr_cons_disp == 0).nonzero() ] = 'nan' disparity_reg_const[ (disparity_reg_const == 0).nonzero() ] = 'nan' disparity_reg_planar[ (disparity_reg_planar == 0).nonzero() ] = 'nan' disparity_reg_quadric[ (disparity_reg_quadric == 0).nonzero() ] = 'nan' disparity_gaps_reg_const[ (disparity_gaps_reg_const == 0).nonzero() ] = 'nan' disparity_gaps_reg_planar[ (disparity_gaps_reg_planar == 0).nonzero() ] = 'nan' disparity_gaps_reg_quadric[ (disparity_gaps_reg_quadric == 0).nonzero() ] = 'nan' disparity_gaps_rob_const[ (disparity_gaps_rob_const == 0).nonzero() ] = 'nan' disparity_gaps_rob_planar[ (disparity_gaps_rob_planar == 0).nonzero() ] = 'nan' disparity_gaps_rob_quadric[ (disparity_gaps_rob_quadric == 0).nonzero() ] = 'nan' disparity_rob_const[ (disparity_rob_const == 0).nonzero() ] = 'nan' disparity_rob_planar[ (disparity_rob_planar == 0).nonzero() ] = 'nan' disparity_rob_quadric[ (disparity_rob_quadric == 0).nonzero() ] = 'nan' disparity_rob_final[ (disparity_rob_final == 0).nonzero() ] = 'nan' plt.plot_array('Consistent sparse disparity (phase-based)', arr_cons_disp, disp_min, disp_max, output_path + 'sparse-disparity-consistent') plt.plot_array('Edge Disparity', arr_edge_disp, disp_min, disp_max, output_path + 'edge-disparity') plt.plot_array('Initial Disparity', arr_init_disp, disp_min, disp_max, output_path + 'init-disparity') plt.plot_array('Final Disparity (regular LSQ, const)', disparity_reg_const, disp_min, disp_max, output_path + 'dense-disparity-reg-const') plt.plot_array('Final Disparity (regular LSQ, planar)', disparity_reg_planar, disp_min, disp_max, output_path + 'dense-disparity-reg-planar') plt.plot_array('Final Disparity (regular LSQ, quadric)', disparity_reg_quadric, disp_min, disp_max, output_path + 'dense-disparity-reg-quadric') #plt.plot_array('Gaps Disparity (regular LSQ, const)', disparity_gaps_reg_const, disp_min, disp_max, local_folder + 'dense-disparity-gaps-reg-const') #plt.plot_array('Gaps Disparity (regular LSQ, planar)', disparity_gaps_reg_planar, disp_min, disp_max, local_folder + 'dense-disparity-gaps-reg-planar') #plt.plot_array('Gaps Disparity (regular LSQ, quadric)', disparity_gaps_reg_quadric, disp_min, disp_max, local_folder + 'dense-disparity-gaps-reg-quadric') #plt.plot_array('Gaps Disparity (robust LSQ, const)', disparity_gaps_rob_const, disp_min, disp_max, local_folder + 'dense-disparity-gaps-rob-const') #plt.plot_array('Gaps Disparity (robust LSQ, planar)', disparity_gaps_rob_planar, disp_min, disp_max, local_folder + 'dense-disparity-gaps-rob-planar') #plt.plot_array('Gaps Disparity (robust LSQ, quadric)', disparity_gaps_rob_quadric, disp_min, disp_max, local_folder + 'dense-disparity-gaps-rob-quadric') plt.plot_array('Final Disparity (robust LSQ, const)', disparity_rob_const, disp_min, disp_max, output_path + 'dense-disparity-rob-const') plt.plot_array('Final Disparity (robust LSQ, planar)', disparity_rob_planar, disp_min, disp_max, output_path + 'dense-disparity-rob-planar') plt.plot_array('Final Disparity (robust LSQ, quadric)', disparity_rob_quadric, disp_min, disp_max, output_path + 'dense-disparity-rob-quadric') plt.plot_array('Final Disparity (robust, model selection)', disparity_rob_final, disp_min, disp_max, output_path + 'dense-disparity-rob-final') # save estimated disparities as text files np.savetxt(output_path + 'dense-disparity-reg-const.dat', disparity_reg_const) np.savetxt(output_path + 'dense-disparity-reg-planar.dat', disparity_reg_planar) np.savetxt(output_path + 'dense-disparity-reg-quadric.dat', disparity_reg_quadric) np.savetxt(output_path + 'dense-disparity-rob-const.dat', disparity_rob_const) np.savetxt(output_path + 'dense-disparity-rob-planar.dat', disparity_rob_planar) np.savetxt(output_path + 'dense-disparity-rob-quadric.dat', disparity_rob_quadric) np.savetxt(output_path + 'dense-disparity-rob-final.dat', disparity_rob_final) return disparity_rob_final