def eval_prob(self, prob, disp_save_path, display=False, interpolate=False): prob = np.reshape(prob, [self.h, self.w, self.d, 2]) prob = fte.get_cost(prob) probr = fte.get_right_cost(prob) self.do_post(prob, probr, disp_save_path, display, interpolate)
def eval_prob(self, prob, disp_save_path, display=False, interpolate=False, isLocalExp=True): prob = np.reshape(prob, [self.h, self.w, self.d, 2]) prob = fte.get_cost(prob) # now in shape [imgH, imgW, ndisp] probr = fte.get_right_cost(prob) # now in shape [imgH, imgW, ndisp] #print (prob.shape, probr.shape) self.do_post(prob, probr, disp_save_path, display, interpolate, isLocalExp)
def __extract_features_lr(self, census, ncc, sobel, sad): dims = census.shape censusr = fte.get_right_cost(census) census = np.reshape(census, [dims[0] * dims[1], dims[2]]) censusr = np.reshape(censusr, [dims[0] * dims[1], dims[2]]) nccr = fte.get_right_cost(ncc) ncc = np.reshape(ncc, [dims[0] * dims[1], dims[2]]) nccr = np.reshape(nccr, [dims[0] * dims[1], dims[2]]) sobelr = fte.get_right_cost(sobel) sobel = np.reshape(sobel, [dims[0] * dims[1], dims[2]]) sobelr = np.reshape(sobelr, [dims[0] * dims[1], dims[2]]) sadr = fte.get_right_cost(sad) sad = np.reshape(sad, [dims[0] * dims[1], dims[2]]) sadr = np.reshape(sadr, [dims[0] * dims[1], dims[2]]) features = np.empty((dims[0] * dims[1] * dims[2], 20)) features[:, 0] = np.reshape(census, [dims[0] * dims[1] * dims[2]]) features[:, 1] = np.reshape(ncc, [dims[0] * dims[1] * dims[2]]) features[:, 2] = np.reshape(sobel, [dims[0] * dims[1] * dims[2]]) features[:, 3] = np.reshape(sad, [dims[0] * dims[1] * dims[2]]) features[:, 4] = np.reshape(fte.extract_ratio(census, .01), [dims[0] * dims[1] * dims[2]]) features[:, 5] = np.reshape(fte.extract_ratio(ncc, 1.01), [dims[0] * dims[1] * dims[2]]) features[:, 6] = np.reshape(fte.extract_ratio(sobel, .01), [dims[0] * dims[1] * dims[2]]) features[:, 7] = np.reshape(fte.extract_ratio(sad, .01), [dims[0] * dims[1] * dims[2]]) features[:, 8] = np.reshape( fte.extract_likelihood(census, self.__cens_sigma), [dims[0] * dims[1] * dims[2]]) features[:, 9] = np.reshape(fte.extract_likelihood(ncc, self.__ncc_sigma), [dims[0] * dims[1] * dims[2]]) features[:, 10] = np.reshape( fte.extract_likelihood(sobel, self.__sad_sigma), [dims[0] * dims[1] * dims[2]]) features[:, 11] = np.reshape( fte.extract_likelihood(sad, self.__sad_sigma), [dims[0] * dims[1] * dims[2]]) r_pkrn = fte.extract_ratio(censusr, .01) r_pkrn = np.reshape(r_pkrn, [dims[0], dims[1], dims[2]]) features[:, 12] = np.reshape(fte.get_left_cost(r_pkrn), [dims[0] * dims[1] * dims[2]]) r_aml = fte.extract_likelihood(censusr, self.__cens_sigma) r_aml = np.reshape(r_aml, [dims[0], dims[1], dims[2]]) features[:, 16] = np.reshape(fte.get_left_cost(r_aml), [dims[0] * dims[1] * dims[2]]) del censusr r_pkrn = fte.extract_ratio(nccr, 1.01) r_pkrn = np.reshape(r_pkrn, [dims[0], dims[1], dims[2]]) features[:, 13] = np.reshape(fte.get_left_cost(r_pkrn), [dims[0] * dims[1] * dims[2]]) r_aml = fte.extract_likelihood(nccr, self.__ncc_sigma) r_aml = np.reshape(r_aml, [dims[0], dims[1], dims[2]]) features[:, 17] = np.reshape(fte.get_left_cost(r_aml), [dims[0] * dims[1] * dims[2]]) del nccr r_pkrn = fte.extract_ratio(sobelr, .01) r_pkrn = np.reshape(r_pkrn, [dims[0], dims[1], dims[2]]) features[:, 14] = np.reshape(fte.get_left_cost(r_pkrn), [dims[0] * dims[1] * dims[2]]) r_aml = fte.extract_likelihood(sobelr, self.__sad_sigma) r_aml = np.reshape(r_aml, [dims[0], dims[1], dims[2]]) features[:, 18] = np.reshape(fte.get_left_cost(r_aml), [dims[0] * dims[1] * dims[2]]) del sobelr r_pkrn = fte.extract_ratio(sadr, .01) r_pkrn = np.reshape(r_pkrn, [dims[0], dims[1], dims[2]]) features[:, 15] = np.reshape(fte.get_left_cost(r_pkrn), [dims[0] * dims[1] * dims[2]]) r_aml = fte.extract_likelihood(sadr, self.__sad_sigma) r_aml = np.reshape(r_aml, [dims[0], dims[1], dims[2]]) features[:, 19] = np.reshape(fte.get_left_cost(r_aml), [dims[0] * dims[1] * dims[2]]) del sadr return features
def __postprocessing_mem_interp(self, imgl, imgr, cost, direction, display=False): r_cost = fte.get_right_cost(cost) post.InitCUDA() start = time.time() cross_array_init = np.zeros( (4, imgl.shape[0], imgl.shape[1])).astype(np.float32) imgl_d = post.CUDA_float_array(imgl) imgr_d = post.CUDA_float_array(imgr) rightc = np.zeros((4, imgr.shape[0], imgr.shape[1])).astype(np.float32) leftc_d = post.CUDA_float_array(cross_array_init) rightc_d = post.CUDA_float_array(cross_array_init) post.cross(imgl_d, leftc_d, self.__L1, self.__tau1) post.cross(imgr_d, rightc_d, self.__L1, self.__tau1) ###compute right disparity r_cost_d = post.CUDA_double_array(r_cost) temp_cost_d = post.CUDA_double_array(cost) tmp = np.zeros((cost.shape[1], cost.shape[2])) tmp_d = post.CUDA_double_array(tmp) post.swap_axis_back(r_cost_d, temp_cost_d) cc = r_cost_d.get_array() post.CUDA_copy_double(temp_cost_d, r_cost_d) for i in range(0, self.__cbca_i1): post.cbca(rightc_d, leftc_d, r_cost_d, temp_cost_d, -1 * direction) post.CUDA_copy_double(temp_cost_d, r_cost_d) if display: print "CBCA 1 right" cc = r_cost_d.get_array() ddisp = np.argmin(cc, axis=0) pfm.show(ddisp) post.swap_axis(r_cost_d, temp_cost_d) post.CUDA_copy_double(temp_cost_d, r_cost_d) for i in range(0, self.__sgm_i): temp_cost_d.set_to_zero() tmp_d.set_to_zero() post.sgm(imgl_d, imgr_d, r_cost_d, temp_cost_d, tmp_d, self.__pi1, self.__pi2, self.__tau_so, self.__alpha1, self.__sgm_q1, self.__sgm_q2, -1 * direction) post.CUDA_divide_double(temp_cost_d, 4) post.CUDA_copy_double(temp_cost_d, r_cost_d) post.swap_axis_back(r_cost_d, temp_cost_d) post.CUDA_copy_double(temp_cost_d, r_cost_d) if display: print "SGM right" cc = r_cost_d.get_array() ddisp = np.argmin(cc, axis=0) pfm.show(ddisp) for i in range(0, self.__cbca_i2): post.cbca(rightc_d, leftc_d, r_cost_d, temp_cost_d, -1 * direction) post.CUDA_copy_double(temp_cost_d, r_cost_d) if display: print "CBCA2 right" cc = r_cost_d.get_array() ddisp = np.argmin(cc, axis=0) pfm.show(ddisp) del temp_cost_d cc = r_cost_d.get_array() r_disp = np.argmin(cc, axis=0).astype(np.float32) del cc del r_cost_d r_disp_d = post.CUDA_float_array(r_disp) cost_d = post.CUDA_double_array(cost) temp_cost_d = post.CUDA_double_array(cost) post.swap_axis_back(cost_d, temp_cost_d) post.CUDA_copy_double(temp_cost_d, cost_d) for i in range(0, self.__cbca_i1): post.cbca(leftc_d, rightc_d, cost_d, temp_cost_d, direction) post.CUDA_copy_double(temp_cost_d, cost_d) if display: print "CBCA 1" cc = cost_d.get_array() ddisp = np.argmin(cc, axis=0) pfm.show(ddisp) post.swap_axis(cost_d, temp_cost_d) post.CUDA_copy_double(temp_cost_d, cost_d) tmp = np.zeros((cost.shape[1], cost.shape[2])) tmp_d = post.CUDA_double_array(tmp) for i in range(0, self.__sgm_i): temp_cost_d.set_to_zero() tmp_d.set_to_zero() post.sgm(imgl_d, imgr_d, cost_d, temp_cost_d, tmp_d, self.__pi1, self.__pi2, self.__tau_so, self.__alpha1, self.__sgm_q1, self.__sgm_q2, direction) post.CUDA_divide_double(temp_cost_d, 4) post.CUDA_copy_double(temp_cost_d, cost_d) post.swap_axis_back(cost_d, temp_cost_d) post.CUDA_copy_double(temp_cost_d, cost_d) if display: print "SGM" cc = cost_d.get_array() ddisp = np.argmin(cc, axis=0) pfm.show(ddisp) for i in range(0, self.__cbca_i2): post.cbca(leftc_d, rightc_d, cost_d, temp_cost_d, direction) post.CUDA_copy_double(temp_cost_d, cost_d) if display: print "CBCA2" cc = cost_d.get_array() ddisp = np.argmin(cc, axis=0) pfm.show(ddisp) cc = cost_d.get_array() disp_l = np.argmin(cc, axis=0).astype(np.float32) out_s = cc.shape[0] del cc disp_d = post.CUDA_float_array(disp_l) disp_temp = post.CUDA_float_array(disp_l) post.subpixel_enchancement(disp_d, cost_d, disp_temp) outlier = np.zeros( (r_disp.shape[0], r_disp.shape[1])).astype(np.float32) outlier_d = post.CUDA_float_array(outlier) post.outlier_detection(disp_d, r_disp_d, outlier_d, out_s) post.interpolate_mismatch(disp_d, outlier_d, disp_temp) post.CUDA_copy_float(disp_temp, disp_d) post.interpolate_occlusion(disp_d, outlier_d, disp_temp) post.CUDA_copy_float(disp_temp, disp_d) post.CUDA_copy_float(disp_temp, disp_d) if display: disp = disp_d.get_array() print "Subpixel" pfm.show(disp) for i in range(0, self.__median_i): disp = post.median2d(disp_d, disp_temp, self.__median_w) post.CUDA_copy_float(disp_temp, disp_d) if display: disp = disp_d.get_array() print "Median" pfm.show(disp) post.mean2d(disp_d, disp_temp, self.__gaussian(self.__blur_sigma), self.__blur_t) post.CUDA_copy_float(disp_temp, disp_d) if display: disp = disp_d.get_array() print "Bilateral" pfm.show(disp) disp = disp_d.get_array() end = time.time() print(end - start) return disp
def __create_samples_mem(self, iml, imr, index): w, h, ndisp = self.__read_calib(index) gt = pfm.load(self.__data_path + self.__trainset[index] + "/disp0GT.pfm")[0] gt = np.reshape(gt, [gt.shape[0] * gt.shape[1], 1]) infs = np.concatenate((np.argwhere(gt == np.inf), np.argwhere(gt < 0)), axis=0) # infs = np.empty((0,2)) gt = np.delete(gt, infs[:, 0], axis=0) gt = np.round(gt) gt = gt.astype(np.int32) #print ("loading gt ... gt shape = {}".format(gt.shape)) random_samples = fte.generate_d_indices(gt, ndisp, 1) assert random_samples.shape[ 1] == 3 # here : 3 means 1 positive sample + 2 negative ones; samples = np.empty( (random_samples.shape[0] * random_samples.shape[1], 21)) #print ("samples shape = {}".format(samples.shape)) #print ("staring census ...") ################## Census compute ########################################################## #print ('w = {}, h = {}, ndisp = {}, censW = {}'.format(w, h, ndisp, self.__censw)) #print ('last iml = {}, last imr = {}'.format(iml[h-1,w-1], imr[h-1,w-1])) costcensus = mtc.census(iml, imr, ndisp, self.__censw).astype(np.float64) #print ('costcensus shape = {}'.format(costcensus.shape)) costcensusR = fte.get_right_cost(costcensus) costcensus = np.reshape( costcensus, [costcensus.shape[0] * costcensus.shape[1], costcensus.shape[2]]) costcensusR = np.reshape(costcensusR, [ costcensusR.shape[0] * costcensusR.shape[1], costcensusR.shape[2] ]) costcensus = np.delete(costcensus, infs[:, 0], axis=0) samples[:, 0] = fte.get_samples(costcensus, random_samples) samples[:, 4] = fte.extract_ratio(costcensus, random_samples, .01) samples[:, 8] = fte.extract_likelihood(costcensus, random_samples, self.__cens_sigma) del costcensus #print ("census done!") r_pkrn = fte.extract_ratio(costcensusR, .01) r_pkrn = np.reshape(r_pkrn, [h, w, ndisp]) r_pkrn = fte.get_left_cost(r_pkrn) r_pkrn = np.reshape( r_pkrn, [r_pkrn.shape[0] * r_pkrn.shape[1], r_pkrn.shape[2]]) r_pkrn = np.delete(r_pkrn, infs[:, 0], axis=0) samples[:, 12] = fte.get_samples(r_pkrn, random_samples) del r_pkrn r_aml = fte.extract_likelihood(costcensusR, self.__cens_sigma) r_aml = np.reshape(r_aml, [h, w, ndisp]) r_aml = fte.get_left_cost(r_aml) r_aml = np.reshape(r_aml, [r_aml.shape[0] * r_aml.shape[1], r_aml.shape[2]]) r_aml = np.delete(r_aml, infs[:, 0], axis=0) samples[:, 16] = fte.get_samples(r_aml, random_samples) del r_aml del costcensusR ###################################################################################### ############################### NCC compute ########################################## costncc = mtc.nccNister(iml, imr, ndisp, self.__nccw) costncc = fte.swap_axes(costncc) costnccR = fte.get_right_cost(costncc) costncc = np.reshape( costncc, [costncc.shape[0] * costncc.shape[1], costncc.shape[2]]) costnccR = np.reshape( costnccR, [costnccR.shape[0] * costnccR.shape[1], costnccR.shape[2]]) costncc = np.delete(costncc, infs[:, 0], axis=0) samples[:, 1] = fte.get_samples(costncc, random_samples) samples[:, 5] = fte.extract_ratio(costncc, random_samples, 1.01) samples[:, 9] = fte.extract_likelihood(costncc, random_samples, self.__ncc_sigma) del costncc r_pkrn = fte.extract_ratio(costnccR, 1.01) r_pkrn = np.reshape(r_pkrn, [h, w, ndisp]) r_pkrn = fte.get_left_cost(r_pkrn) r_pkrn = np.reshape( r_pkrn, [r_pkrn.shape[0] * r_pkrn.shape[1], r_pkrn.shape[2]]) r_pkrn = np.delete(r_pkrn, infs[:, 0], axis=0) samples[:, 13] = fte.get_samples(r_pkrn, random_samples) del r_pkrn r_aml = fte.extract_likelihood(costnccR, self.__ncc_sigma) r_aml = np.reshape(r_aml, [h, w, ndisp]) r_aml = fte.get_left_cost(r_aml) r_aml = np.reshape(r_aml, [r_aml.shape[0] * r_aml.shape[1], r_aml.shape[2]]) r_aml = np.delete(r_aml, infs[:, 0], axis=0) samples[:, 17] = fte.get_samples(r_aml, random_samples) del r_aml del costnccR ###################################################################################### ############################### Sob compute ########################################## sobl = mtc.sobel(iml) sobr = mtc.sobel(imr) costsob = mtc.sadsob(sobl, sobr, ndisp, 5).astype(np.float64) costsob = fte.swap_axes(costsob) costsobR = fte.get_right_cost(costsob) costsob = np.reshape( costsob, [costsob.shape[0] * costsob.shape[1], costsob.shape[2]]) costsobR = np.reshape( costsobR, [costsobR.shape[0] * costsobR.shape[1], costsobR.shape[2]]) costsob = np.delete(costsob, infs[:, 0], axis=0) samples[:, 2] = fte.get_samples(costsob, random_samples) samples[:, 6] = fte.extract_ratio(costsob, random_samples, .01) samples[:, 10] = fte.extract_likelihood(costsob, random_samples, self.__sad_sigma) del costsob r_pkrn = fte.extract_ratio(costsobR, .01) r_pkrn = np.reshape(r_pkrn, [h, w, ndisp]) r_pkrn = fte.get_left_cost(r_pkrn) r_pkrn = np.reshape( r_pkrn, [r_pkrn.shape[0] * r_pkrn.shape[1], r_pkrn.shape[2]]) r_pkrn = np.delete(r_pkrn, infs[:, 0], axis=0) samples[:, 14] = fte.get_samples(r_pkrn, random_samples) del r_pkrn r_aml = fte.extract_likelihood(costsobR, self.__sad_sigma) r_aml = np.reshape(r_aml, [h, w, ndisp]) r_aml = fte.get_left_cost(r_aml) r_aml = np.reshape(r_aml, [r_aml.shape[0] * r_aml.shape[1], r_aml.shape[2]]) r_aml = np.delete(r_aml, infs[:, 0], axis=0) samples[:, 18] = fte.get_samples(r_aml, random_samples) del r_aml del costsobR ###################################################################################### ############################### Sad compute ########################################## costsad = mtc.zsad(iml, imr, ndisp, self.__sadw).astype(np.float64) costsad = fte.swap_axes(costsad) costsadR = fte.get_right_cost(costsad) costsad = np.reshape( costsad, [costsad.shape[0] * costsad.shape[1], costsad.shape[2]]) costsadR = np.reshape( costsadR, [costsadR.shape[0] * costsadR.shape[1], costsadR.shape[2]]) costsad = np.delete(costsad, infs[:, 0], axis=0) samples[:, 3] = fte.get_samples(costsad, random_samples) samples[:, 7] = fte.extract_ratio(costsad, random_samples, .01) samples[:, 11] = fte.extract_likelihood(costsad, random_samples, self.__sad_sigma) del costsad r_pkrn = fte.extract_ratio(costsadR, .01) r_pkrn = np.reshape(r_pkrn, [h, w, ndisp]) r_pkrn = fte.get_left_cost(r_pkrn) r_pkrn = np.reshape( r_pkrn, [r_pkrn.shape[0] * r_pkrn.shape[1], r_pkrn.shape[2]]) r_pkrn = np.delete(r_pkrn, infs[:, 0], axis=0) samples[:, 15] = fte.get_samples(r_pkrn, random_samples) del r_pkrn r_aml = fte.extract_likelihood(costsadR, self.__sad_sigma) r_aml = np.reshape(r_aml, [h, w, ndisp]) r_aml = fte.get_left_cost(r_aml) r_aml = np.reshape(r_aml, [r_aml.shape[0] * r_aml.shape[1], r_aml.shape[2]]) r_aml = np.delete(r_aml, infs[:, 0], axis=0) samples[:, 19] = fte.get_samples(r_aml, random_samples) del r_aml del costsadR samples[:, 20] = fte.generate_labels(random_samples) return samples