Ejemplo n.º 1
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
Ejemplo n.º 2
0
Archivo: test.py Proyecto: topriss/CBMV
    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