Ejemplo n.º 1
0
def BscanRegist(LN, radius, verbose):
    [nBscan, H, W] = LN.shape
    nch = radius * 2 + 1
    opt = []
    for i in range(nBscan):
        # get neighboring slices within radius
        if i >= radius and i < nBscan - radius:
            # zero-pad the W
            x = np.zeros([nch, 512, 512], dtype=np.float32)
            im_fix = np.ascontiguousarray(np.float32(LN[i, :, :]))
            x[radius, :, :500] = im_fix

            for j in range(radius):
                dist = j + 1
                mov_pre = np.ascontiguousarray(np.float32(LN[i - dist, :, :]))
                reg_pre = MC.MotionCorrect(im_fix, mov_pre)
                x[radius - dist, :, :500] = reg_pre[:512, :]

                mov_post = np.ascontiguousarray(np.float32(LN[i + dist, :, :]))
                reg_post = MC.MotionCorrect(im_fix, mov_post)
                x[radius + dist, :, :500] = reg_post[:512, :]
            opt.append(x)

        # display a sample
        if verbose == True and i == 200:
            plt.figure(figsize=(12, 4))
            plt.axis('off'), plt.title('Regist Result', fontsize=15)
            plt.imshow(np.concatenate(
                (x[0, :, :], x[radius + 1, :, :], x[-1, :, :]), axis=1),
                       cmap='gray')
            plt.show()
    return opt
def GetPair(Vx, Vy, pair, verbose):
    global n_seq
    nBscan, H, W = Vx.shape
    slc = np.random.randint(n_seq, nBscan - 1, 1)

    # for easy application, first few slices abandoned
    for i in range(n_seq, nBscan):
        x_ipt = Vx[i - n_seq:i + 1, :, :]
        y_ipt = Vy[i - n_seq:i + 1, :, :]
        x = np.zeros([n_seq, H, H], dtype=np.float32)
        y = np.zeros([n_seq, H, H], dtype=np.float32)

        im_fix = np.ascontiguousarray(np.float32(y_ipt[-1, :, :]))
        for j in range(n_seq):
            x_mov = np.ascontiguousarray(np.float32(x_ipt[j, :, :]))
            y_mov = np.ascontiguousarray(np.float32(y_ipt[j, :, :]))
            x[j, :, :W] = MC.MotionCorrect(im_fix, x_mov)
            y[j, :, :W] = MC.MotionCorrect(im_fix, y_mov)

        if verbose == True and i == slc:
            im_x = np.concatenate((x[0, :, :], x[-1, :, :]), axis=1)
            im_y = np.concatenate((y[0, :, :], y[-1, :, :]), axis=1)
            plt.figure(figsize=(12, 12))
            plt.axis('off')
            plt.title('slice {}'.format(slc), fontsize=15)
            plt.imshow(np.concatenate((im_x, im_y), axis=0), cmap='gray')
            plt.show()

        pair = pair + ((x, y), )
    return pair
Ejemplo n.º 3
0
def GetPair(HN, SF, opt):
    global nch
    nBscan, H, W = HN.shape
    radius = int((nch - 1) / 2)
    for i in range(nBscan):
        if i >= radius and i < nBscan - radius:
            x = np.zeros([nch, 512, 512], dtype=np.float32)
            y = SF[i, :, :]

            # eliminate the zero pad for im_fix, mov_pre and mov_post
            im_fix = np.ascontiguousarray(np.float32(HN[i, :, :500]))
            x[radius, :, :500] = im_fix

            for j in range(radius):
                dist = j + 1
                mov_pre = np.ascontiguousarray(
                    np.float32(HN[i - dist, :, :500]))
                reg_pre = MC.MotionCorrect(im_fix, mov_pre)
                x[radius - dist, :, :500] = reg_pre

                mov_post = np.ascontiguousarray(
                    np.float32(HN[i + dist, :, :500]))
                reg_post = MC.MotionCorrect(im_fix, mov_post)
                x[radius + dist, :, :500] = reg_post

            opt.append(np.concatenate((x, np.expand_dims(y, axis=0)), axis=0))

            # display an example
            if i == 200:
                Display(x[radius, :, :], y)
    return opt
Ejemplo n.º 4
0
    def __init__(self, root):
        self.vx = util.nii_loader(root)
        n, h, w = self.vx.shape

        self.data = []
        for i in range(radius, n - radius):
            x = np.zeros([3, 512, 512], dtype=np.float32)

            im_fix = np.ascontiguousarray(np.float32(self.vx[i, :, :]))

            mov_pre = np.ascontiguousarray(np.float32(self.vx[i - 1, :, :]))
            mov_post = np.ascontiguousarray(np.float32(self.vx[i + 1, :, :]))

            x[0, :, :w] = MC.MotionCorrect(im_fix, mov_pre)
            x[2, :, :w] = MC.MotionCorrect(im_fix, mov_post)
            x[1, :, :w] = im_fix

            self.data.append(x)
Ejemplo n.º 5
0
def FrameAver(volume, idx):
    nFrame, nBscan, H, W = volume.shape
    opt = np.zeros([nFrame, nBscan, H, W], dtype=np.float32)
    # iter over Bscan
    for i in range(nBscan):
        im_fix = np.ascontiguousarray(np.float32(volume[idx, i, :, :]))
        # iter over frames
        for j in range(nFrame):
            im_mov = np.ascontiguousarray(np.float32(volume[j, i, :, :]))
            opt[j, i, :, :] = MC.MotionCorrect(im_fix, im_mov)
    return np.mean(opt, axis=0)
Ejemplo n.º 6
0
def BscanRegist(Volume_noi, nch):
    [nd, nr, nc] = Volume_noi.shape
    radius = int((nch - 1) / 2)
    opt = []
    for i in range(nd):
        if i >= radius and i < nd - radius:
            x = np.zeros([nch, 512, 512], dtype=np.float32)
            fix = np.ascontiguousarray(np.float32(Volume_noi[i, :, :]))
            x[radius, :, :500] = fix[:512, :]
            for j in range(radius):
                dist = j + 1
                mov_pre = np.ascontiguousarray(
                    np.float32(Volume_noi[i - dist, :, :]))
                reg_pre = MC.MotionCorrect(fix, mov_pre)
                x[radius - dist, :, :500] = reg_pre[:512, :]

                mov_post = np.ascontiguousarray(
                    np.float32(Volume_noi[i + dist, :, :]))
                reg_post = MC.MotionCorrect(fix, mov_post)
                x[radius + dist, :, :500] = reg_post[:512, :]
            opt.append(x)
        if i % 50 == 0:
            print('[%d/%d] complete' % (i, nd))
    return opt
fixedImageFile = root + 'fix_img.nii.gz'
movingImageFile = root + 'mov_img.nii.gz'
outputImageFile = root + 'opt.nii.gz'
pair = ()

t1 = time.time()
dim = data.shape
for i in range(dim[0]):
    if i >= radius and i < dim[0] - radius:
        fix = data[i, :, :]
        save_nii(fix, root, 'fix_img.nii.gz')
        for j in range(radius):
            dist = j + 1
            frame_x = data[i - dist, :, :]
            save_nii(frame_x, root, 'mov_img.nii.gz')
            MotionCorrection.MotionCorrect(fixedImageFile, movingImageFile,
                                           outputImageFile)
            x_pre = np.zeros([1024, 512], dtype=np.float32)
            x_pre[:, :500] = load_nii(outputImageFile)

            frame_x = data[i + dist, :, :]
            save_nii(frame_x, root, 'mov_img.nii.gz')
            MotionCorrection.MotionCorrect(fixedImageFile, movingImageFile,
                                           outputImageFile)
            x_post = np.zeros([1024, 512], dtype=np.float32)
            x_post[:, :500] = load_nii(outputImageFile)

            y = np.zeros([1024, 512], dtype=np.float32)
            y[:, :500] = fix
            pair = pair + (
                (x_pre, y),
                (x_post, y),
Ejemplo n.º 8
0
        fovea_list.append(file)

for vol in range(len(fovea_list)):
    print('volume :{}'.format(fovea_list[vol]))
    v = util.nii_loader(dataroot+fovea_list[vol])
    v = v[0,:,:,:]
    slc, H, W = v.shape
    show = random.randint(0,slc-1)
    
    for i in range(radius,slc-radius):
        stack = v[i-radius:i+radius+1,:,:]
        opt = np.zeros([2*radius+1,H,H],dtype=np.float32)
        
        im_fix = np.ascontiguousarray(v[i,:,:])
        for j in range(2*radius+1):
            im_mov = np.ascontiguousarray(stack[j,:,:])
            opt[j,:,:W] = MC.MotionCorrect(im_fix,im_mov)
        vm_train = vm_train+(opt,)
        
        if i == show:
            top = np.concatenate((stack[0,:,:W],stack[-1,:,:W]),axis=1)
            bot = np.concatenate((opt[0,:,:W],opt[-1,:,:W]),axis=1)
            
            plt.figure(figsize=(12,12))
            plt.axis('off')
            plt.title('slc: {}'.format(show),fontsize=15)
            plt.imshow(np.concatenate((top,bot),axis=0),cmap='gray')
            plt.show()

with open('E:\\vm_train.pickle','wb') as func:
    pickle.dump(vm_train,func)