示例#1
0
文件: ibp.py 项目: chieupham/fbrain
def iterativeBackPropagation(hrImage, lrImages, lrMasks, transforms, H,
                             itermax):

    y = []
    for i in range(len(lrImages)):
        y.append(
            convert_image_to_vector(lrImages[i]) *
            convert_image_to_vector(lrMasks[i]))

    x = convert_image_to_vector(hrImage)
    outputImage = nibabel.Nifti1Image(hrImage.get_data(), hrImage.affine)

    hrMaskSum = np.zeros(hrImage.get_data().shape, dtype=np.float32)
    for i in range(len(lrImages)):
        tmp1 = apply_affine_itk_transform_on_image(input_image=lrMasks[i],
                                                   transform=transforms[i][0],
                                                   center=transforms[i][1],
                                                   reference_image=hrImage,
                                                   order=0)
        hrMaskSum += tmp1.get_data()

    index = np.nonzero(hrMaskSum)

    for j in range(itermax):

        #simulation and error computation
        hrError = np.zeros(hrImage.get_data().shape, dtype=np.float32)

        for i in range(len(lrImages)):
            lrError = convert_vector_to_image(H[i].dot(x) - y[i], lrImages[i])
            tmp2 = apply_affine_itk_transform_on_image(
                input_image=lrError,
                transform=transforms[i][0],
                center=transforms[i][1],
                reference_image=hrImage,
                order=1)
            hrError += tmp2.get_data()

        hrError2 = np.zeros(hrImage.get_data().shape, dtype=np.float32)
        hrError2[index] = hrError[index] / hrMaskSum[index]

        #update hr image and x
        outputImage = nibabel.Nifti1Image(outputImage.get_data() - hrError2,
                                          hrImage.affine)
        nibabel.save(outputImage, 'ibp_iter' + str(j) + '.nii.gz')
        x = convert_image_to_vector(outputImage)

    return outputImage
示例#2
0
def ibpComputeError(x, H, y, hrMaskSum, lrImages, transforms, interpOrder):
    hrError = np.zeros(hrMaskSum.get_data().shape, dtype=np.float32)
    #Loop over LR image to compute the sum of errors
    for i in range(len(lrImages)):
        lrError = convert_vector_to_image(H[i].dot(x) - y[i], lrImages[i])
        tmp2 = apply_affine_itk_transform_on_image(input_image=lrError,
                                                   transform=transforms[i][0],
                                                   center=transforms[i][1],
                                                   reference_image=hrMaskSum,
                                                   order=interpOrder)
        hrError += tmp2.get_data()

    #Normalize the error image
    hrErrorNorm = np.zeros(hrMaskSum.get_data().shape, dtype=np.float32)
    index = np.nonzero(hrMaskSum.get_data())
    hrErrorNorm[index] = hrError[index] / hrMaskSum.get_data()[index]

    return hrErrorNorm
示例#3
0
文件: ibp.py 项目: rousseau/fbrain
def ibpComputeError(x, H, y, hrMaskSum, lrImages, transforms, interpOrder):
    hrError = np.zeros(hrMaskSum.get_data().shape, dtype=np.float32)
    # Loop over LR image to compute the sum of errors
    for i in range(len(lrImages)):
        lrError = convert_vector_to_image(H[i].dot(x) - y[i], lrImages[i])
        tmp2 = apply_affine_itk_transform_on_image(
            input_image=lrError,
            transform=transforms[i][0],
            center=transforms[i][1],
            reference_image=hrMaskSum,
            order=interpOrder,
        )
        hrError += tmp2.get_data()

    # Normalize the error image
    hrErrorNorm = np.zeros(hrMaskSum.get_data().shape, dtype=np.float32)
    index = np.nonzero(hrMaskSum.get_data())
    hrErrorNorm[index] = hrError[index] / hrMaskSum.get_data()[index]

    return hrErrorNorm
    for i in range(len(inputImages)):
        HList.append(
            compute_H(inputImages[i], initHRImage, inputTransforms[i],
                      psfList[i], maskImages[i]))

    #Intensity correction To do
    #N4 on initHR
    #local correction
    #New init HR
    if args.bias == True:
        initHRImage_N4 = apply_N4_on_image(initHRImage, shrink_factor=1)

        xN4 = convert_image_to_vector(initHRImage_N4)
        hrN4Data = np.zeros(initHRImage.get_data().shape)
        for i in range(len(inputImages)):
            simu = convert_vector_to_image(HList[i].dot(xN4), inputImages[i])
            im = gaussian_biais_correction(inputImages[i], simu, 5)

            warped = apply_affine_itk_transform_on_image(
                input_image=im,
                transform=inputTransforms[i][0],
                center=inputTransforms[i][1],
                reference_image=initHRImage,
                order=3)
            hrN4Data += (warped.get_data() / np.float32(len(inputImages)))
        initHRImage = nibabel.Nifti1Image(hrN4Data, initHRImage.affine)

    #Compute x
    x = convert_image_to_vector(initHRImage)
    maskX = convert_image_to_vector(maskHRImage)
    #Let mask the HR image
示例#5
0
  #Compute H
  HList = []
  for i in range(len(inputImages)):
    HList.append( compute_H(inputImages[i], initHRImage, inputTransforms[i], psfList[i], maskImages[i]) )
   
  #Intensity correction To do
  #N4 on initHR
  #local correction
  #New init HR
  if args.bias == True:
    initHRImage_N4 = apply_N4_on_image(initHRImage, shrink_factor=1)
      
    xN4 = convert_image_to_vector(initHRImage_N4)
    hrN4Data = np.zeros(initHRImage.get_data().shape)
    for i in range(len(inputImages)):
      simu = convert_vector_to_image(HList[i].dot(xN4),inputImages[i])
      im = gaussian_biais_correction(inputImages[i],simu, 5)
        
      warped = apply_affine_itk_transform_on_image(input_image=im,transform=inputTransforms[i][0], center=inputTransforms[i][1], reference_image=initHRImage, order=3)
      hrN4Data += (warped.get_data() / np.float32(len(inputImages)) )
    initHRImage = nibabel.Nifti1Image(hrN4Data, initHRImage.affine)  
             
  #Compute x
  x = convert_image_to_vector(initHRImage)
  maskX = convert_image_to_vector(maskHRImage)
  #Let mask the HR image
  x = x*maskX

  #loop over LR images and stack y and masks
  maskList = []
  yList = []
示例#6
0
        m = np.identity(4)
        c = np.array([0, 0, 0, 1])
        inputTransform = (m, c)

    print('Creating mask image using the following padding value:' +
          str(args.padding))
    data = np.zeros(HRimage.get_data().shape)
    data[HRimage.get_data() > args.padding] = 1
    maskHRImage = nibabel.Nifti1Image(data, HRimage.affine)
    print('Percentage of HR masked values : %.2f ' %
          (np.size(np.nonzero(
              (data))) / (1.0 * np.size(data.shape)) * 100.0 / np.size(data)))
    data = np.zeros(LRimage.get_data().shape)
    data[LRimage.get_data() > args.padding] = 1
    maskLRImage = nibabel.Nifti1Image(data, LRimage.affine)
    print('Percentage of LR masked values : %.2f ' %
          (np.size(np.nonzero(
              (data))) / (1.0 * np.size(data.shape)) * 100.0 / np.size(data)))

    HRSpacing = np.float32(np.array(HRimage.header['pixdim'][1:4]))
    LRSpacing = np.float32(np.array(LRimage.header['pixdim'][1:4]))
    psf = compute_psf(LRSpacing, HRSpacing, args.psf)
    H = compute_H(LRimage, HRimage, inputTransform, psf, maskLRImage)

    x = convert_image_to_vector(HRimage)
    maskX = convert_image_to_vector(maskHRImage)
    #Let mask the HR image
    x = x * maskX

    nibabel.save(convert_vector_to_image(H.dot(x), LRimage), args.output)
  else:
    #no transform provided : use identity as transform and zero as center
    m = np.identity(4)
    c = np.array([0, 0, 0, 1])
    inputTransform = (m,c) 

  print('Creating mask image using the following padding value:'+str(args.padding))
  data = np.zeros(HRimage.get_data().shape)
  data[HRimage.get_data() > args.padding] = 1
  maskHRImage = nibabel.Nifti1Image(data, HRimage.affine)
  print('Percentage of HR masked values : %.2f '%( np.size(np.nonzero((data))) / (1.0*np.size(data.shape)) * 100.0 / np.size(data) ) )
  data = np.zeros(LRimage.get_data().shape)
  data[LRimage.get_data() > args.padding] = 1
  maskLRImage = nibabel.Nifti1Image(data, LRimage.affine)
  print('Percentage of LR masked values : %.2f '%( np.size(np.nonzero((data))) / (1.0*np.size(data.shape)) * 100.0 / np.size(data) ) )
  
  HRSpacing = np.float32(np.array(HRimage.header['pixdim'][1:4]))  
  LRSpacing = np.float32(np.array(LRimage.header['pixdim'][1:4]))  
  psf = compute_psf(LRSpacing, HRSpacing, args.psf)
  H = compute_H(LRimage, HRimage, inputTransform, psf, maskLRImage)    
  
  x = convert_image_to_vector(HRimage)
  maskX = convert_image_to_vector(maskHRImage)
  #Let mask the HR image
  x = x*maskX

  nibabel.save(convert_vector_to_image(H.dot(x),LRimage),args.output)

  

示例#8
0
    # Compute H
    HList = []
    for i in range(len(inputImages)):
        HList.append(compute_H(inputImages[i], initHRImage, inputTransforms[i], psfList[i], maskImages[i]))

    # Intensity correction To do
    # N4 on initHR
    # local correction
    # New init HR
    if args.bias == True:
        initHRImage_N4 = apply_N4_on_image(initHRImage, shrink_factor=1)

        xN4 = convert_image_to_vector(initHRImage_N4)
        hrN4Data = np.zeros(initHRImage.get_data().shape)
        for i in range(len(inputImages)):
            simu = convert_vector_to_image(HList[i].dot(xN4), inputImages[i])
            im = gaussian_biais_correction(inputImages[i], simu, 5)

            warped = apply_affine_itk_transform_on_image(
                input_image=im,
                transform=inputTransforms[i][0],
                center=inputTransforms[i][1],
                reference_image=initHRImage,
                order=3,
            )
            hrN4Data += warped.get_data() / np.float32(len(inputImages))
        initHRImage = nibabel.Nifti1Image(hrN4Data, initHRImage.affine)

    # Compute x
    x = convert_image_to_vector(initHRImage)
    maskX = convert_image_to_vector(maskHRImage)