Beispiel #1
0
def get_cnn_centers(names, labels_names, balanced=True, neigh_width=15):
    rois = list(load_masks(names))
    rois_p = list(load_masks(labels_names))
    rois_p_neigh = [log_and(log_and(imdilate(roi_p, iterations=neigh_width), log_not(roi_p)), roi)
                    for roi, roi_p in izip(rois, rois_p)]
    rois_n_global = [log_and(roi, log_not(log_or(roi_pn, roi_p)))
                     for roi, roi_pn, roi_p in izip(rois, rois_p_neigh, rois_p)]
    rois = list()
    for roi_pn, roi_ng, roi_p in izip(rois_p_neigh, rois_n_global, rois_p):
        # The goal of this for is to randomly select the same number of nonlesion and lesion samples for each image.
        # We also want to make sure that we select the same number of boundary negatives and general negatives to
        # try to account for the variability in the brain.
        n_positive = np.count_nonzero(roi_p)
        if balanced:
            roi_pn[roi_pn] = np.random.permutation(xrange(np.count_nonzero(roi_pn))) < n_positive / 2
        roi_ng[roi_ng] = np.random.permutation(xrange(np.count_nonzero(roi_ng))) < n_positive / 2
        rois.append(log_or(log_or(roi_ng, roi_pn), roi_p))

    # In order to be able to permute the centers to randomly select them, or just shuffle them for training, we need
    # to keep the image reference with the center. That's why we are doing the next following lines of code.
    centers_list = [get_mask_voxels(roi) for roi in rois]
    idx_lesion_centers = np.concatenate([np.array([(i, c) for c in centers], dtype=object)
                                         for i, centers in enumerate(centers_list)])

    return idx_lesion_centers
Beispiel #2
0
def get_mask(mask_name, dilate=0, dtype=np.uint8):
    # Lesion mask
    mask_image = load_nii(mask_name).get_data().astype(dtype)
    if dilate > 0:
        mask_d = imdilate(
            mask_image,
            iterations=dilate
        )
        mask_e = imerode(
            mask_image,
            iterations=dilate
        )
        mask_image = np.logical_and(mask_d, np.logical_not(mask_e)).astype(dtype)

    return mask_image
def get_mask(mask_name, dilate=0, dtype=np.uint8):
    """
    Function to load a mask image
    :param mask_name: Path to the mask image file
    :param dilate: Dilation radius
    :param dtype: Data type for the final mask
    :return:
    """
    # Lesion mask
    mask_image = (load_nii(mask_name).get_fdata() > 0.5).astype(dtype)
    if dilate > 0:
        mask_d = imdilate(mask_image, iterations=dilate)
        mask_e = imerode(mask_image, iterations=dilate)
        mask_image = np.logical_and(mask_d,
                                    np.logical_not(mask_e)).astype(dtype)

    return mask_image
Beispiel #4
0
def contrast_features(img,region=None,neihbors=2):
    img = img*255
    if region is None:
        region = np.ones_like(img)
    
    R = region==1
    Rn = R
    for i in range(neihbors):
        Rn = imdilate(Rn)

    Rn = np.bitwise_and(Rn,~R)

    if np.max(Rn)==1:
        Ir = img*region
        In = img*Rn
        MeanGr = np.average(Ir)
        MeanGn = np.average(In)
        K1 = (MeanGr-MeanGn)/MeanGn            # contrast after Kamm, 1999
        K2 = (MeanGr-MeanGn)/(MeanGr+MeanGn)   # modulation after Kamm, 1999
        K3 = np.log(MeanGr/MeanGn)                # film-contrast after Kamm, 1999
    else:
        K1 = -1        
        K2 = -1        
        K3 = -1

    (nI,mI) = img.shape

    n1 = int(round(nI/2)+1)
    m1 = int(round(mI/2)+1)

    P1 = img[n1,:]    # Profile in i-Direction
    P2 = img[:,m1]    # Profile in j-Direction
    Q1 = rampefree(P1)
    Q2 = rampefree(P2)
    Q = np.concatenate((Q1,Q2))

    Ks = np.std(Q)
    K  = np.log(np.max(Q)-np.min(Q)+1)
        
    features = [K1,K2,K3,Ks,K]
    return features
Beispiel #5
0
def ellipsoid_simulation(I,K,SSe,f,abc,var_mu,xmax):

    J = I.copy()
    (N,M) = I.shape
    

    R = np.zeros((N,M)) # ROI of simulated defect

    invK  = np.linalg.inv(K)

    if len(abc)==3: # elliposoid
        (a,b,c) = abc 
    else: # sphere
        a = abc 
        b = a
        c = a 

    # Computation of the 3 x 3 matrices Phi and L
    H     = np.linalg.inv(SSe)
    h0    = H[0,:]/a
    h1    = H[1,:]/b
    h2    = H[2,:]/c
    Hs    = np.zeros((3,3))
    Hs[:,0] = h0[0:3]
    Hs[:,1] = h1[0:3]
    Hs[:,2] = h2[0:3]
    hd      = np.zeros((3,1))
    hd[0] = h0[3]
    hd[1] = h1[3]
    hd[2] = h2[3]
    Phi = np.matmul(Hs,Hs.T)
    hhd = np.matmul(hd,hd.T)
    hhd1 = 1-np.matmul(hd.T,hd)
    L = np.matmul(np.matmul(Hs,hhd),Hs.T) + hhd1*Phi

    # Location of the superimposed area
    A     = L[0:2,0:2]
    mc    = np.array(-f*np.matmul(np.linalg.inv(A),L[0:2,2]))
    x     = np.linalg.eig(A)[0]
    C     = np.array([x[1],x[0]])
    la    = C
    a00   = np.linalg.det(L)/np.linalg.det(A)
    ae    = f*np.sqrt(-a00/la[0])
    be    = f*np.sqrt(-a00/la[1])
    al    = np.arctan2(C[1],C[0])+np.pi
    ra    = np.array( [ae*np.cos(al), ae*np.sin(al)] )
    rb    = np.array( [be*np.cos(al+np.pi/2), be*np.sin(al+np.pi/2)] )
    u1    = funcf(mc+ra,K)
    u2    = funcf(mc+rb,K)
    u3    = funcf(mc-ra,K)
    u4    = funcf(mc-rb,K)
    uc    = funcf(mc,K)
    e1    = u1+u2-uc
    e2    = u1+u4-uc
    e3    = u3+u2-uc
    e4    = u3+u4-uc
    E     = np.concatenate((e1,e2,e3,e4),axis=1)
    Emax  = np.max(E,axis=1)
    Emin  = np.min(E,axis=1)
    umin  = int(max(np.fix(Emin[0]), 1))
    vmin  = int(max(np.fix(Emin[1]), 1))
    umax  = int(min(np.fix(Emax[0]+1), N))
    vmax  = int(min(np.fix(Emax[1]+1), M))
    q  = 255/(1-np.exp(var_mu*xmax))
    R[umin:umax,vmin:vmax] = 1
    R = imdilate(R)
    z = np.zeros((2,1))
    for u in range(umin,umax):
        z[0] = u
        for v in range(vmin,vmax):
            z[1] = v
            m = funcf(z,invK)
            m[0:2] = m[0:2]/f
            p = np.matmul(np.matmul(m.T,L),m)
            if p>0:
                x = np.matmul(np.matmul(m.T,Phi),m)
                d = 2*np.sqrt(p)*np.linalg.norm(m)/x
                J[u,v] = np.exp(var_mu*d)*(I[u,v]-q)+q

    return J
def get_mask_blocks(mask, dilation=2):
    return get_mask_voxels(imdilate(mask, iterations=dilation))