Esempio n. 1
0
    def __get_costs(self, iml, imr):
        costcensus = mtc.census(iml, imr, self.d,
                                self.__censw).astype(np.float64)

        costncc = mtc.nccNister(iml, imr, self.d, self.__nccw)
        costncc = fte.swap_axes(costncc)

        sobl = mtc.sobel(iml)
        sobr = mtc.sobel(imr)

        costsob = mtc.sadsob(sobl, sobr, self.d, 5).astype(np.float64)
        costsob = fte.swap_axes(costsob)

        costsad = mtc.zsad(iml, imr, self.d, self.__sadw).astype(np.float64)
        costsad = fte.swap_axes(costsad)

        return costcensus, costncc, costsob, costsad
Esempio n. 2
0
    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