Beispiel #1
0
    def test_single_point(self):
        img = np.ones((11, 11))
        img[6, 6] = 0

        dgm = lower_star_img(img)

        assert dgm.shape[0] == 1
        assert tuple(dgm[0]) == (0, np.inf)
def getSegmentation(F):
    ''' 
    This function computes the step in the filtration of binary images that we will use later onto find a suitable mask.

    :type F: numpy.array
    :param F: 3D array representing the filtration of binary images. The dimensions correspond to height x width x step_in_filtration.

    :retrun: numpy.array - Binary image that will be used to compute the mask.3
    '''

    if F.ndim != 3:
        print('ERROR: The parameter F must be a 3D-array.')
        return

    
    prev_num_connected_componnets = img.shape[0]*img.shape[1]
    T2 = 0
    for i in range(F.shape[2]):
        
        # Compute 0-dim persistence
        dgm = lower_star_img(F[:,:,i])
        # We need to clear out results since nany point with persistence (-0,infy) is point that never was part of the diagram.
        # ind_of_zeros = np.where(dgm[:,0] == 0)[0]
        # dgm = np.delete(dgm, ind_of_zeros, axis=0)

        # The stopping condition comes whenever S_{t+1} has more connected components than S_t
        
        if dgm.shape[0] <= prev_num_connected_componnets:
            
            prev_num_connected_componnets = dgm.shape[0]        
        else:
            
            T2 = i-1
            break
            
    T1 = int(1 + np.floor(T2/4))

    selected = F[:,:,T1]

    return selected
Beispiel #3
0
    # plt.savefig('mask.png')

    # Apply mask to image

    masked_img = cv2.bitwise_and(img, img, mask=mask)

    # fig, ax = plt.subplots(1,1, figsize=(10,10))

    # ax.imshow(masked_img, cmap='gray')

    # plt.savefig('masked_img.png')

    # RGB

    dgm_R_rgb.append(lower_star_img(-masked_img[:, :, 0]))
    dgm_G_rgb.append(lower_star_img(-masked_img[:, :, 1]))
    dgm_B_rgb.append(lower_star_img(-masked_img[:, :, 2]))

    # HSV

    masked_img_hsv = skimage.color.rgb2hsv(masked_img)

    dgm_H_hsv.append(lower_star_img(-masked_img_hsv[:, :, 0]))
    dgm_S_hsv.append(lower_star_img(-masked_img_hsv[:, :, 1]))
    dgm_V_hsv.append(lower_star_img(-masked_img_hsv[:, :, 2]))

    # XYZ

    masked_img_xyz = skimage.color.rgb2xyz(masked_img)
Beispiel #4
0
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import ndimage
import PIL

from ripser import ripser, plot_dgms, lower_star_img


ts = np.linspace(-1, 1, 100)
x1 = np.exp(-ts**2/(0.1**2))
ts -= 0.4
x2 = np.exp(-ts**2/(0.1**2))
img = -x1[None, :]*x1[:, None] - 2*x1[None, :]*x2[:, None] - 3*x2[None, :]*x2[:, None]


dgm = lower_star_img(img)

plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.imshow(img)
plt.colorbar()
plt.title("Test Image")
plt.subplot(122)
plot_dgms(dgm)
plt.title("0-D Persistence Diagram")
plt.tight_layout()
plt.show()
Beispiel #5
0
def RipserMat(i,j):
    index=[]
    if j!=99:
        j=s.index(i)
    else:
        j=99          
    mat=np.array(i) #prodce single matrix per letter
    mat=mat.astype(np.float) #convert to float
    co = np.argwhere(mat!=0)# create matrix of coordinates 
    if len(co)==0: #if empty data set then all values are 0
        H1=0;H2=0;H3=0;R1=(0,0);R2=(0,0);RL0=0;RRL0=(0,0);RRL1=(0,0);RRL2=(0,0);LR0=0;RLR0=(0,0);RLR1=(0,0);RLR2=(0,0);TB0=0;RTB0=(0,0);RTB1=(0,0);RTB2=(0,0);
        BT0=0;RBT0=(0,0);RBT1=(0,0);RBT2=(0,0);LL0=0;LL1= 0;LL2=0;LLLS0=0;LLLS1=0;LLLS2=0;CO0=0;CO1= 0;CO2=0;COLS0=0;COLS1=0;COLS2=0;CI0=0;
        CI1= 0;CI2=0;CILS0=0;CILS1=0;CILS2=0;RLLS0= 0;RLLS1=0;RLLS2=0;LRLS0=0;LRLS1=0;LRLS2=0;TBLS0=0;TBLS1=0;TBLS2=0;BTLS0=0;BTLS1=0;BTLS2=0
        ranges= [RLLS0,RLLS1,RLLS2,LRLS0,LRLS1,LRLS2,TBLS0,TBLS1,TBLS2,BTLS0,BTLS1,BTLS2,LLLS0,LLLS1,LLLS2,COLS0,COLS1,COLS2,CILS0,CILS1,CILS2,H1,H2,H3]  

    else:
        
        dgms = ripser(co)['dgms']#analyses the point cloud of coordinates in ripser 
        OneDset=(dgms[1:][0])#defines the range of 1D homology ""

        if len(OneDset)>0: #delete all repeated values
            OneDset=np.unique(OneDset, axis=0)          

        H1=len(OneDset)
        
        for h in np.arange(len(OneDset)):
            index1=(OneDset[h][1])
            index0=(OneDset[h][0])
            feat=np.subtract((OneDset[h][1]),(OneDset[h][0]))
            
            #removes noise in 1D
            if feat < 1:
                index.append(h)
                                    
        OneDset= np.delete(OneDset,index,0)
                      
        H1=(len(OneDset))
        H2=0
        H3=0
        
        if H1 ==1:
            H1=0
            H2=100
            H3=0
        elif H1 ==0 :
            H1=100
        elif H1 ==2:
            H1=0
            H2=0
            H3=100            
    
            #RIGHT TO LEFT scan    
        RLmat=RightToLeft(mat) 
        dgm = lower_star_img(RLmat)
        RL0=len(dgm)
        RLLS0,RLLS1,RLLS2= USDictionaryUpdate(dgm)
        mat=np.array(i) 
        mat=mat.astype(np.float)
            
        LRmat=LeftToRight(mat) 
        dgm = lower_star_img(LRmat)
        LR0=len(dgm)
        LRLS0,LRLS1,LRLS2= USDictionaryUpdate(dgm)
        mat=np.array(i) 
        mat=mat.astype(np.float)
            
        TBmat=TopToBottom(mat) 
            
        dgm = lower_star_img(TBmat)
        TB0=len(dgm)
        TBLS0,TBLS1,TBLS2= USDictionaryUpdate(dgm)
        mat=np.array(i) 
        mat=mat.astype(np.float)
            
        BTmat=BottomToTop(mat) 
        dgm = lower_star_img(BTmat)
        BT0=len(dgm)
        BTLS0,BTLS1,BTLS2= USDictionaryUpdate(dgm)
        mat=np.array(i) 
        mat=mat.astype(np.float)
            
        LLmat= Diag_Array_LL(mat)
        dgm = lower_star_img(LLmat)
        LL0=len(dgm)
        LLLS0,LLLS1,LLLS2= USDictionaryUpdate(dgm)
        mat=np.array(i) 
        mat=mat.astype(np.float)
            
            
        COmat=CenterOutFiltration(mat)
        dgm = lower_star_img(COmat)
        CO0=len(dgm)
        COLS0,COLS1,COLS2= USDictionaryUpdate(dgm)
        mat=np.array(i) 
        mat=mat.astype(np.float)
            
        CImat=CenterInFiltration(mat)
        dgm = lower_star_img(CImat)
        CI0=len(dgm)
        CILS0,CILS1,CILS2= USDictionaryUpdate(dgm)
        mat=np.array(i) 
        mat=mat.astype(np.float)
            
        ranges= [RLLS0,RLLS1,RLLS2,LRLS0,LRLS1,LRLS2,TBLS0,TBLS1,TBLS2,BTLS0,BTLS1,BTLS2,LLLS0,LLLS1,LLLS2,COLS0,COLS1,COLS2,CILS0,CILS1,CILS2,H1,H2,H3]  
    
    for i in range(len(ranges)):
        if i<=2 and ranges[i]!=0.0:
            ClassificationKey.append([j,1,ranges[i]])
        elif 2<i<=5 and ranges[i]!=0.0:
            ClassificationKey.append([j,2,ranges[i]])
        elif 5<i<=8 and ranges[i]!=0.0:
            ClassificationKey.append([j,3,ranges[i]])
        elif 8<i<=11 and ranges[i]!=0.0:
            ClassificationKey.append([j,4,ranges[i]])
        elif 11<i<=14 and ranges[i]!=0.0:
            ClassificationKey.append([j,5,ranges[i]])
        elif 14<i<=17 and ranges[i]!=0.0:
            ClassificationKey.append([j,6,ranges[i]])
        elif 17<i<=20 and ranges[i]!=0.0:
            ClassificationKey.append([j,7,ranges[i]]) 
            
    return ranges,ClassificationKey
Beispiel #6
0
    pim = PersImage(spread=pim_sd, pixels=[pim_px,pim_px], verbose=False)

    # generate SSMs for each gesture
    raw_ssm_lst = [np.zeros(shape=(a.shape[0], a.shape[0])) for a in arrays]
    for n, a in enumerate(arrays):
        for i in range(a.shape[0]):
            for j in range(a.shape[0]):
                raw_ssm_lst[n][i,j] = cumulated_ts_2(a[i,:],a[j,:])

    # smooth SSM images
    for r, s in enumerate(raw_ssm_lst):
        raw_ssm_lst[r] = gaussian_filter(s, sigma=1)

    # generate persistence images
    for n, s in enumerate(raw_ssm_lst):
        pd = lower_star_img(s)
        img = pim.transform(pd[:-1,:]) # remove 'inf' persistence
        raw_pim_mat[n,:-2] = img.reshape(1,-1)
        raw_pim_mat[n,-2] = subj_lab[n]
        raw_pim_mat[n,-1] = gest_lab[n]

    # save matrix as DataFrame
    raw_pim_df = DataFrame(raw_pim_mat)
    cnames = ["px"+str(i) for i in raw_pim_df.columns]
    cnames[-2:] = ["subj", "gest"]
    raw_pim_df.columns = cnames
    raw_pim_df.to_csv("./Data/raw_pim_vectors.csv", index=False)

    ############################################################################
    ################################ ISOMAP SSMs ###############################
    ############################################################################
Beispiel #7
0
 def test_returns_dgm(self):
     img = np.random.random((100, 400))
     dgm = lower_star_img(img)
     assert isinstance(dgm, np.ndarray)
     assert dgm.shape[1] == 2
     assert np.all(dgm[:, 1] >= dgm[:, 0])
Beispiel #8
0
def topological_process_img(img, dgm=None, tau=None, window_size=None, border_width=None):
    
    return_modified = False
    if dgm is None:
        if window_size is not None:
            img = smoothen(img, window_size=window_size)
            return_modified = True
            
        if border_width is not None:
            img = add_border(img, border_width=border_width)
            return_modified = True
            
        dgm = lower_star_img(img)
    
    if tau is None:
        dgm_lifetimes, tau = lifetimes_from_dgm(dgm, tau=True)
        
    else:
        dgm_lifetimes = lifetimes_from_dgm(dgm)
        
    idxs = np.where(np.logical_and(tau < dgm_lifetimes[:,1], dgm_lifetimes[:,1] < np.inf))[0]
    idxs = np.flip(idxs[np.argsort(dgm[idxs, 0])])
    didxs = np.zeros(0).astype("int")
    
    img_components = np.zeros_like(img)

    dist = np.zeros([len(idxs), img.shape[0], img.shape[1]])
    nearest_value = np.zeros([len(idxs), img.shape[0], img.shape[1]])

    for i, idx in enumerate(idxs):
        bidx = np.argmin(np.abs(img - dgm[idx, 0]))
        didxs = np.append(didxs, np.argmin(np.abs(img - dgm[idx, 1])))

        img_temp = np.ones_like(img)
        img_temp[np.logical_or(img < dgm[idx, 0] - 0.01, dgm[idx, 1] - 0.01 < img)] = np.nan
        component_at_idx = connected_components_img(img_temp)
        del(img_temp)

        component_at_idx = component_at_idx == component_at_idx[bidx // img.shape[1], bidx % img.shape[1]]
        if i > 0:
            didxs_in_component = idxs[np.where([component_at_idx[didx // img.shape[1], didx % img.shape[1]] 
                                                for didx in didxs])[0]]
            if len(didxs_in_component) > 0:
                component_at_idx[img > np.min(dgm[didxs_in_component, 1]) - 0.1] = False

        img_components[component_at_idx] = 1

        img_temp = np.ones_like(img)
        img_temp[component_at_idx] = 0
        dist[i,:,:], nearest_neighbor_temp = distance_transform_edt(img_temp, return_indices=True)
        nearest_value[i,:,:] = img[nearest_neighbor_temp[0], nearest_neighbor_temp[1]]
        del(img_temp, nearest_neighbor_temp)
        
    img_processed = np.zeros_like(img)
    all_components = img_components > 0
    img_processed[all_components] = img[all_components]
    with np.errstate(divide="ignore"):
        img_processed[~all_components] = np.sum(nearest_value / dist, axis=0)[~all_components] / \
                                            np.sum(1 / dist, axis=0)[~all_components]
     
    if return_modified:
        return {"modified": img, "components": img_components, "processed": img_processed}
    
    return {"components": img_components, "processed": img_processed}