Пример #1
0
def extract_features_left(census,ncc,sobel,sad, 
        cens_sigma = 128.0,  
        ncc_sigma=0.02, 
        sad_sigma=20000.0, 
        sobel_sigma=20000.0,
        disp_image = None):

        # cost shape : [img_H, img_W, ndisp]
        dims = census.shape
        h,w,ndisp = dims[:]
        ## clip to remove RAND_MAX values + normalize in 0,1
        #cost_tmp = census
        #cost_tmp[np.isclose(cost_tmp, 2147483648.0)] = 1024.0
        census  =  np.reshape(census,   [h*w, ndisp])
        ncc  =  np.reshape(ncc, [h*w,ndisp])
        sobel  =  np.reshape(sobel, [h*w,ndisp])
        sad  =  np.reshape(sad,  [h*w,ndisp])

        """ let channel number = 8, as the first dim, which makes 
            the following assignment and element access 
            in C contiguous arraya way, that is, a chunk of contiguous 
            memory in the C-like index order;
        """
        features = np.empty((8, h, w, ndisp), order= 'C')
        """ matcher cost : census , ncc, sobel, sad """
        features[0, :,:,:]= np.reshape(np.clip(census, 0., 120.)/120. , [h, w, ndisp])
        #features[1, :,:,:]= np.reshape(np.clip(ncc + 1.0, 0., 2.),[h, w, ndisp])
        features[1, :,:,:]= np.reshape((1 + np.clip(ncc, -1., 1.))/2,[h, w, ndisp]) # Updated: change NCC to [0, 1] range!!!
        features[2, :,:,:]= np.reshape(np.clip(sobel, 0., 2**13)/float(2**13), [h, w, ndisp])
        features[3, :,:,:]= np.reshape(np.clip(sad, 0., 2**13) / float(2**13), [h, w, ndisp])
        """ aml for left costs: census , ncc, sobel, sad """
        # X ~ N(u, sigma^2), then aX ~ N(au, a^2*sigma^2);
        # E(aX + b) = aE(x) + b;
        # Var(aX + b) = a^2*Var(x);
        #normlized_cens_var = cens_sigma / (120.0**2 *4)
        #normalized_ncc_var = ncc_sigma/4.0
        #normalized_sob_var = sad_sigma / float(2**26 *4)
        #normalized_sad_var = sad_sigma / float(2**26 *16)
        normlized_cens_var = cens_sigma
        normalized_ncc_var = ncc_sigma
        normalized_sob_var = sad_sigma
        normalized_sad_var = sad_sigma
        
        features[4,:,:,:]=np.reshape(fte.extract_likelihood(census, normlized_cens_var),[h,w,ndisp])
        features[5,:,:,:]=np.reshape(fte.extract_likelihood(ncc, normalized_ncc_var),   [h,w,ndisp])
        features[6,:,:,:]=np.reshape(fte.extract_likelihood(sobel, normalized_sob_var), [h,w,ndisp])
        features[7,:,:,:]=np.reshape(fte.extract_likelihood(sad, normalized_sad_var),   [h,w,ndisp])
        
        # in size [C, D, H, W] 
        features = features.transpose((0,3,1,2))
        return features.astype(np.float32)
Пример #2
0
def extract_features_left_V2(census,ncc,sobel,sad, 
        cens_sigma = 128.0,  
        ncc_sigma=0.02, 
        sad_sigma=20000.0, 
        sobel_sigma=20000.0,
        disp_image = None):

        # cost shape : [img_H, img_W, ndisp]
        dims = census.shape
        h,w,ndisp = dims[:]
        """ aml for left costs: census , ncc, sobel, sad """
        # X ~ N(u, sigma^2), then aX ~ N(au, a^2*sigma^2);
        # E(aX + b) = aE(x) + b;
        # Var(aX + b) = a^2*Var(x);
        #normlized_cens_var = cens_sigma / (120.0**2 *4)
        #normalized_ncc_var = ncc_sigma/4.0
        #normalized_sob_var = sad_sigma / float(2**26 *4)
        #normalized_sad_var = sad_sigma / float(2**26 *16)
        normlized_cens_var = cens_sigma
        normalized_ncc_var = ncc_sigma
        normalized_sob_var = sad_sigma
        normalized_sad_var = sad_sigma
        
        #census
        census_aml = np.expand_dims(np.reshape(fte.extract_likelihood(np.reshape(census, [h*w,ndisp]), normlized_cens_var),[h,w,ndisp]), 0)
        census = np.expand_dims(np.clip(census, 0., 120.)/120., 0)
        # ncc
        ncc_aml = np.expand_dims(np.reshape(fte.extract_likelihood(np.reshape(ncc,[h*w,ndisp]), normalized_ncc_var),[h,w, ndisp]), 0)
        ncc = np.expand_dims(np.clip(ncc + 1.0, 0., 2.), 0)
        # sobel
        sobel_aml = np.expand_dims(np.reshape(fte.extract_likelihood(np.reshape(sobel, [h*w,ndisp]),normalized_sob_var),[h,w,ndisp]), 0)
        sobel = np.expand_dims(np.clip(sobel, 0., 2**13) / float(2**13), 0)
        # sad
        sad_aml = np.expand_dims(np.reshape(fte.extract_likelihood(np.reshape(sad, [h*w, ndisp]), normalized_sad_var),[h,w,ndisp]), 0)
        sad = np.expand_dims(np.clip(sad, 0., 2**13) / float(2**13), 0)
        
        """ let channel number = 8, as the first dim, which makes 
            the following assignment and element access 
            in C contiguous arraya way, that is, a chunk of contiguous 
            memory in the C-like index order;
        """
        features = np.concatenate((census, ncc, sobel, sad, census_aml, ncc_aml, sobel_aml, sad_aml), 0)
        # in size [C, D, H, W] 
        features = features.transpose((0,3,1,2))
        del census_aml
        del ncc_aml
        del sobel_aml
        del sad_aml
        return features.astype(np.float32)
Пример #3
0
def extract_features_lr(
    census,ncc,sobel,sad, 
    cens_sigma = 128.0,  
    ncc_sigma=0.02, 
    sad_sigma=20000.0, 
    sobel_sigma=20000.0,
    disp_image = None):

        # cost shape : [img_H, img_W, ndisp]
        h,w,ndisp = census.shape[:]
        censusR =  fte.get_right_cost(census)
        #pfm.show(np.argmin(census, axis = 2).astype(np.float32), title='disp_census')
        #pfm.show(np.argmin(censusR, axis = 2).astype(np.float32), title='R_disp_census')

        nccR = fte.get_right_cost(ncc)
        #pfm.show(np.argmin(ncc, axis = 2).astype(np.float32), title='disp_ncc')
        #pfm.show(np.argmin(nccR, axis = 2).astype(np.float32), title='R_disp_ncc')

        sadR = fte.get_right_cost(sad)
        #pfm.show(np.argmin(sad, axis = 2).astype(np.float32), title='disp_sad')
        #pfm.show(np.argmin(sadR, axis = 2).astype(np.float32), title='R_disp_sad')
        
        sobelR = fte.get_right_cost(sobel)
        #pfm.show(np.argmin(sobel, axis = 2).astype(np.float32), title='disp_sob')
        #pfm.show(np.argmin(sobelR, axis = 2).astype(np.float32), title='R_disp_sob')
        
        #cost_tmp = census
        #cost_tmp[np.isclose(cost_tmp, 2147483648.0)] = 1024.0
        census  =  np.reshape(census,   [h*w, ndisp])
        ncc  =  np.reshape(ncc, [h*w,ndisp])
        sobel  =  np.reshape(sobel, [h*w,ndisp])
        sad  =  np.reshape(sad,  [h*w,ndisp])
        
        censusR =  np.reshape(censusR,  [h*w, ndisp])
        nccR =  np.reshape(nccR, [h*w, ndisp])
        sobelR =  np.reshape(sobelR,[h*w,ndisp])
        sadR =  np.reshape(sadR, [h*w,ndisp])
        
        """
        # just for debugging!!! 
        # left ones!
        imgs_to_plot = []
        d0 = 25
        tmp_disp1 = disp_image.copy(order = 'C')
        tmp_disp2 = disp_image.copy(order = 'C')
        tmp_disp3 = disp_image.copy(order = 'C')
        tmp_disp1[np.abs(tmp_disp1 - d0) > 3] = 0
        tmp_disp2[np.abs(tmp_disp2 - d0) > 2] = 0
        tmp_disp3[np.abs(tmp_disp3 - d0) > 1] = 0
        # 0, 1, 2, 3;
        imgs_to_plot.append(disp_image)
        imgs_to_plot.append(tmp_disp1)
        imgs_to_plot.append(tmp_disp2)
        imgs_to_plot.append(tmp_disp3)
        # 4, 5, 6, 7;
        imgs_to_plot.append(np.reshape(np.reshape(fte.extract_likelihood(census, cens_sigma/ (120.0**2 *4)),[h,w,ndisp])[:,:,d0], [h,w]))
        imgs_to_plot.append(np.reshape(np.reshape(fte.extract_likelihood(ncc,    ncc_sigma / 4.0),[h,w,ndisp])[:,:,d0], [h,w]))
        imgs_to_plot.append(np.reshape(np.reshape(fte.extract_likelihood(sobel,  sobel_sigma / float (2**26*4)),[h,w,ndisp])[:,:,d0], [h,w]))
        imgs_to_plot.append(np.reshape(np.reshape(fte.extract_likelihood(sad,    sad_sigma / float (2**26*16)),[h,w,ndisp])[:,:,d0], [h,w]))
        
        # 8, 9, 10, 11;
        imgs_to_plot.append(np.reshape(np.reshape(fte.extract_likelihood(census, cens_sigma/ (120.0**2)),[h,w,ndisp])[:,:,d0], [h,w]))
        imgs_to_plot.append(np.reshape(np.reshape(fte.extract_likelihood(ncc,    ncc_sigma/ 1.0),[h,w,ndisp])[:,:,d0], [h,w]))
        imgs_to_plot.append(np.reshape(np.reshape(fte.extract_likelihood(sobel,  sobel_sigma/ float(2**26)),[h,w,ndisp])[:,:,d0], [h,w]))
        imgs_to_plot.append(np.reshape(np.reshape(fte.extract_likelihood(sad,    sad_sigma/ float(2**26)),[h,w,ndisp])[:,:,d0], [h,w]))

        features_name = [
                'censusL', 'nccL', 'sobelL', 'sadL',
                'ratio_cenL', 'ratio_nccL', 'ratio_sobL', 'ratio_sadL',
                'likly_cenL', 'likly_nccL', 'likly_sobL', 'likly_sadL',
                'ratio_cenR', 'ratio_nccR', 'ratio_sobR', 'ratio_sadR',
                'likly_cenR', 'likly_nccR', 'likly_sobR', 'likly_sadR',
                ]
        show_4_imgs_3_row(imgs = imgs_to_plot, 
                img_names = ['slice_{}_d{}'.format(i, d0) for i in features_name[0:12]],
                #cmap = ['inferno']*8 + ['gray'] * 4
                #cmap = ['inferno']*12
                cmap = ['gray']*12
                )
        
        # right ones
        imgs_to_plot = []
        features_name = [
                'disp_censusL', 'disp_nccL', 'disp_sobelL', 'disp_sadL',
                'disp_likly_cenL', 'disp_likly_nccL', 'disp_likly_sobL', 'disp_likly_sadL',
                'disp_censusR', 'disp_nccR', 'disp_sobelR', 'disp_sadR',
                'disp_likly_cenR', 'disp_likly_nccR', 'disp_likly_sobR', 'disp_likly_sadR',
                ]
        imgs_to_plot.append(np.argmin(np.reshape(census, [h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmin(np.reshape(ncc, [h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmin(np.reshape(sobel, [h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmin(np.reshape(sad, [h,w,ndisp]), axis=2))

        imgs_to_plot.append(np.argmax(np.reshape(fte.extract_likelihood(census, cens_sigma),[h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmax(np.reshape(fte.extract_likelihood(ncc, cens_sigma),[h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmax(np.reshape(fte.extract_likelihood(sobel, cens_sigma),[h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmax(np.reshape(fte.extract_likelihood(sad, cens_sigma),[h,w,ndisp]), axis=2))


        imgs_to_plot.append(np.argmin(np.reshape(censusR, [h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmin(np.reshape(nccR, [h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmin(np.reshape(sobelR, [h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmin(np.reshape(sadR, [h,w,ndisp]), axis=2))
        
        imgs_to_plot.append(np.argmax(np.reshape(fte.extract_likelihood(censusR, cens_sigma),[h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmax(np.reshape(fte.extract_likelihood(nccR, cens_sigma),[h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmax(np.reshape(fte.extract_likelihood(sobelR, cens_sigma),[h,w,ndisp]), axis=2))
        imgs_to_plot.append(np.argmax(np.reshape(fte.extract_likelihood(sadR, cens_sigma),[h,w,ndisp]), axis=2))
        
        show_4_imgs_4_row(imgs = imgs_to_plot, 
                img_names = features_name,
                cmap = ['gray']*16
                )
        sys.exit()
        """         



        """ let channel number = 8, as the first dim, which makes 
            the following assignment and element access 
            in C contiguous arraya way, that is, a chunk of contiguous 
            memory in the C-like index order;
        """
        features = np.empty((16, h, w, ndisp), order= 'C')
        """ matcher cost : census , ncc, sobel, sad """
        ## clip to remove RAND_MAX values, and normalized to [0,1]
        features[0, :,:,:]= np.reshape(np.clip(census, 0., 120.)/120. , [h, w, ndisp])
        features[1, :,:,:]= np.reshape((1 + np.clip(ncc, -1., 1.))/2,[h, w, ndisp]) # Updated: change NCC to [0, 1] range!!!
        features[2, :,:,:]= np.reshape(np.clip(sobel, 0., 2**13)/float(2**13), [h, w, ndisp])
        features[3, :,:,:]= np.reshape(np.clip(sad, 0., 2**13) / float(2**13), [h, w, ndisp])
        """ aml for left costs: census , ncc, sobel, sad """
        # X ~ N(u, sigma^2), then aX ~ N(au, a^2*sigma^2);
        # E(aX + b) = aE(x) + b;
        # Var(aX + b) = a^2*Var(x);
        #normlized_cens_var = cens_sigma / (120.0**2 *4)
        #normalized_ncc_var = ncc_sigma/4.0
        #normalized_sob_var = sad_sigma / float(2**26 *4)
        #normalized_sad_var = sad_sigma / float(2**26 *16)
        normlized_cens_var = cens_sigma
        normalized_ncc_var = ncc_sigma
        normalized_sob_var = sad_sigma
        normalized_sad_var = sad_sigma
        
        features[4,:,:,:]=np.reshape(fte.extract_likelihood(census, normlized_cens_var),[h,w,ndisp])
        features[5,:,:,:]=np.reshape(fte.extract_likelihood(ncc, normalized_ncc_var),   [h,w,ndisp])
        features[6,:,:,:]=np.reshape(fte.extract_likelihood(sobel, normalized_sob_var), [h,w,ndisp])
        features[7,:,:,:]=np.reshape(fte.extract_likelihood(sad, normalized_sad_var),   [h,w,ndisp])


        ## Right features
        features[8, :,:,:]= np.reshape(np.clip(censusR, 0., 120.)/120. , [h, w, ndisp])
        features[9, :,:,:]= np.reshape( (1+np.clip(nccR, -1., 1.))/2,[h, w, ndisp]) # Updated: change NCC to [0, 1] range!!!
        features[10, :,:,:]= np.reshape(np.clip(sobelR, 0., 2**13)/float(2**13), [h, w, ndisp])
        features[11, :,:,:]= np.reshape(np.clip(sadR, 0., 2**13) / float(2**13), [h, w, ndisp])
        features[12,:,:,:]=np.reshape(fte.extract_likelihood(censusR, normlized_cens_var),[h,w,ndisp])
        features[13,:,:,:]=np.reshape(fte.extract_likelihood(nccR, normalized_ncc_var),   [h,w,ndisp])
        features[14,:,:,:]=np.reshape(fte.extract_likelihood(sobelR, normalized_sob_var), [h,w,ndisp])
        features[15,:,:,:]=np.reshape(fte.extract_likelihood(sadR, normalized_sad_var),   [h,w,ndisp])

        #del census
        #del ncc
        #del sobel
        #del sad
        del censusR
        del nccR
        del sobelR
        del sadR

        # in size [C, D, H, W] 
        features = features.transpose((0,3,1,2))
        return features.astype(np.float32)