def create_sliceinfo_w(images_fn,
                       labels_fn,
                       regint_fn,
                       kernels,
                       i,
                       slice_no=None):
    # figure out the biggest slice
    if slice_no == None:
        slice_no = find_biggest_slice(path + labels_fn[i])
    print("Creating Weighted Slice Info... {}/{}: Slice {}".format(
        i + 1, len(images_fn), slice_no))

    # get the slice, label, and associated orientations
    slice_im, slice_im_or = get_nifti_slice(path + images_fn[i], slice_no)
    slice_lb, slice_lb_or = get_nifti_slice(path + labels_fn[i], slice_no)
    slice_ro, slice_ro_or = get_nifti_slice(path + regint_fn[i], slice_no)

    # if crop, we crop the image down
    if crop:
        crop_x, crop_y = crop
        start_x = slice_im.shape[0] / 2 - crop_x / 2
        end_x = start_x + crop_x
        start_y = slice_im.shape[1] / 2 - crop_y / 2
        end_y = start_y + crop_y

        slice_im = slice_im[start_x:end_x, start_y:end_y]
        slice_lb = slice_lb[start_x:end_x, start_y:end_y]
        slice_ro = slice_ro[start_x:end_x, start_y:end_y]

    # figure out the principal patches
    pc_payload = (slice_im, slice_lb)
    patches_m_pc, patches_n_pc, vals_m, vals_n = create_pc_patches_w(
        *pc_payload)

    # compute gabor features for the patches
    feats_m = []
    feats_n = []
    intens_m = []
    intens_n = []
    for patch in patches_m_pc:
        feats_m.append(compute_feats(patch, kernels))
        intens_m.append(compute_intens(patch))
    for patch in patches_n_pc:
        feats_n.append(compute_feats(patch, kernels))
        intens_n.append(compute_intens(patch))

    # package it into a SliceInfo object
    si_payload = (images_fn[i], slice_no, slice_im, slice_im_or, slice_lb,
                  slice_lb_or, slice_ro, slice_ro_or, patches_m_pc,
                  patches_n_pc, feats_m, feats_n, intens_m, intens_n, vals_m,
                  vals_n)

    return SliceInfo(*si_payload)
def create_sliceinfo_w(images_fn, labels_fn, regint_fn, kernels, i, slice_no=None):
    # figure out the biggest slice
    if slice_no == None:    
        slice_no = find_biggest_slice(path + labels_fn[i])
    print("Creating Weighted Slice Info... {}/{}: Slice {}".format(i+1, len(images_fn), slice_no))
    
    # get the slice, label, and associated orientations
    slice_im, slice_im_or = get_nifti_slice(path + images_fn[i], slice_no)
    slice_lb, slice_lb_or = get_nifti_slice(path + labels_fn[i], slice_no)
    slice_ro, slice_ro_or = get_nifti_slice(path + regint_fn[i], slice_no)
    
    # if crop, we crop the image down
    if crop:    
        crop_x, crop_y = crop
        start_x = slice_im.shape[0] / 2 - crop_x / 2
        end_x = start_x + crop_x
        start_y = slice_im.shape[1] / 2 - crop_y / 2
        end_y = start_y + crop_y
    
        slice_im = slice_im[start_x:end_x, start_y:end_y]
        slice_lb = slice_lb[start_x:end_x, start_y:end_y]
        slice_ro = slice_ro[start_x:end_x, start_y:end_y]
            
    # figure out the principal patches
    pc_payload = (slice_im, slice_lb)
    patches_m_pc, patches_n_pc, vals_m, vals_n = create_pc_patches_w(*pc_payload)
    
    # compute gabor features for the patches
    feats_m = []
    feats_n = []
    intens_m = []
    intens_n = []
    for patch in patches_m_pc:
        feats_m.append(compute_feats(patch, kernels))
        intens_m.append(compute_intens(patch))
    for patch in patches_n_pc:
        feats_n.append(compute_feats(patch, kernels))
        intens_n.append(compute_intens(patch))
    
    # package it into a SliceInfo object
    si_payload = (images_fn[i], slice_no, 
                  slice_im, slice_im_or,
                  slice_lb, slice_lb_or,
                  slice_ro, slice_ro_or,
                  patches_m_pc, patches_n_pc,
                  feats_m, feats_n,
                  intens_m, intens_n,
                  vals_m, vals_n) 
                  
    return SliceInfo(*si_payload)
def process(filename, filename_label, slice_no):
    
    # Grab the image
    image_slice, orientation_slice = get_nifti_slice(filename, slice_no)
    if SHOW_IMG:
        plt.imshow(image_slice, cmap = plt.cm.gray)
        plt.show()
    
    # Grab the labels
    label_slice, orientation_label = get_nrrd_data(filename_label, slice_no)
    if SHOW_IMG:
        plt.imshow(label_slice, cmap=plt.cm.gray)
        plt.show()

    # Show the mask
    if SHOW_IMG:
        print("Masked version: ")
        mask = np.where(label_slice == 0, label_slice, image_slice)
        plt.imshow(mask, cmap=plt.cm.gray)
        plt.show()   
    
    # Extract patches in ROI
    patches_mask, patches_nonmask = extract_roi_patches(image_slice, 
                                                        label_slice, 
                                                        PATCH_SIZE)
    
    # Get the decomposed patches
    eigens_mask = get_eigenpatches(patches_mask, PATCH_SIZE, MAX_EIGEN)
    eigens_nonmask = get_eigenpatches(patches_nonmask, PATCH_SIZE, MAX_EIGEN)
    
    # Show the eigens, if you want
    if SHOW_IMG:
        show_eigenpatches(eigens_mask)
        
    # Generate Gabor Kernels
    kernels = generate_kernels()
    
    # Show the Gabors
    if SHOW_IMG:
        plot_gabor(eigens_mask)

    # Store all the features and Gabor responses    
    all_features_mask = []
    all_powers_mask = []
    complete_features_mask = []
    
    all_features_nonmask = []
    all_powers_nonmask = []
    complete_features_nonmask = []
    
    for eigen in eigens_mask:
        all_features_mask.append(compute_feats(eigen, kernels))
        all_powers_mask.append(compute_powers(eigen, kernels))
    
    for eigen in eigens_nonmask:
        all_features_nonmask.append(compute_feats(eigen, kernels))
        all_powers_nonmask.append(compute_powers(eigen, kernels))
        
#    for patch in patches_mask:
#        complete_features_mask.append(compute_feats(patch, kernels))
#    
#    for patch in patches_nonmask:
#        complete_features_nonmask.append(compute_feats(patch, kernels))
        
        
    return SliceInfo(filename, slice_no, image_slice, orientation_slice, 
                     label_slice, orientation_label, kernels,
                     patches_mask, eigens_mask,
                     all_features_mask, all_powers_mask,
                     patches_nonmask, eigens_nonmask,
                     all_features_nonmask, all_powers_nonmask)
Пример #4
0
def process(filename, filename_label, slice_no):

    # Grab the image
    image_slice, orientation_slice = get_nifti_slice(filename, slice_no)
    image_slice = normalise(image_slice)
    if SHOW_IMG:
        plt.imshow(image_slice, cmap=plt.cm.gray)
        plt.show()

    # Grab the labels
    label_slice, orientation_label = get_nrrd_data(filename_label, slice_no)
    #if SHOW_IMG:
    #    plt.imshow(label_slice, cmap=plt.cm.gray)
    #    plt.show()

    # Show the mask
    if SHOW_IMG:
        print("Masked version: ")
        mask = np.where(label_slice == 0, label_slice, image_slice)
        plt.imshow(mask, cmap=plt.cm.gray)
        plt.show()

    # Extract patches in ROI
    patches_mask, patches_nonmask = extract_roi_patches(
        image_slice, label_slice, PATCH_SIZE)

    # Get the decomposed patches
    eigens_mask = get_randoms(patches_mask, MAX_EIGEN)
    eigens_nonmask = get_randoms(patches_nonmask, MAX_EIGEN)

    # Show the eigens, if you want
    if SHOW_IMG:
        show_eigenpatches(eigens_mask)

    # Generate Gabor Kernels
    kernels = generate_kernels()

    # Show the Gabors
    if SHOW_IMG:
        plot_gabor(eigens_mask)

    # Store all the features and Gabor responses
    all_features_mask = []
    all_powers_mask = []

    all_features_nonmask = []
    all_powers_nonmask = []

    for eigen in eigens_mask:
        all_features_mask.append(compute_feats(eigen, kernels))
        all_powers_mask.append(compute_powers(eigen, kernels))

    for eigen in eigens_nonmask:
        all_features_nonmask.append(compute_feats(eigen, kernels))
        all_powers_nonmask.append(compute_powers(eigen, kernels))

    return SliceInfo(filename, slice_no, image_slice, orientation_slice,
                     label_slice, orientation_label, kernels, patches_mask,
                     eigens_mask, all_features_mask, all_powers_mask,
                     patches_nonmask, eigens_nonmask, all_features_nonmask,
                     all_powers_nonmask)