def b_spline_centerline(x_centerline, y_centerline, z_centerline):
    """Give a better fitting of the centerline than the method 'spline_centerline' using b-splines"""

    points = [[x_centerline[n], y_centerline[n], z_centerline[n]] for n in range(len(x_centerline))]

    nurbs = NURBS(3, len(z_centerline)*3, points, nbControl=None, verbose=2) # for the third argument (number of points), give at least len(z_centerline)
    # (len(z_centerline)+500 or 1000 is ok)
    P = nurbs.getCourbe3D()
    x_centerline_fit=P[0]
    y_centerline_fit=P[1]
    
    return x_centerline_fit, y_centerline_fit
def b_spline_centerline(x_centerline,y_centerline,z_centerline):
                          
    print '\nFitting centerline using B-spline approximation...'
    points = [[x_centerline[n],y_centerline[n],z_centerline[n]] for n in range(len(x_centerline))]
    nurbs = NURBS(3,3000,points)  # BE very careful with the spline order that you choose : if order is too high ( > 4 or 5) you need to set a higher number of Control Points (cf sct_nurbs ). For the third argument (number of points), give at least len(z_centerline)+500 or higher
                          
    P = nurbs.getCourbe3D()
    x_centerline_fit=P[0]
    y_centerline_fit=P[1]
    Q = nurbs.getCourbe3D_deriv()
    x_centerline_deriv=Q[0]
    y_centerline_deriv=Q[1]
    z_centerline_deriv=Q[2]
                          
    return x_centerline_fit, y_centerline_fit,x_centerline_deriv,y_centerline_deriv,z_centerline_deriv
def b_spline_centerline(x_centerline, y_centerline, z_centerline):
    """Give a better fitting of the centerline than the method 'spline_centerline' using b-splines"""

    points = [[x_centerline[n], y_centerline[n], z_centerline[n]]
              for n in range(len(x_centerline))]

    nurbs = NURBS(
        3, len(z_centerline) * 3, points, nbControl=None, verbose=2
    )  # for the third argument (number of points), give at least len(z_centerline)
    # (len(z_centerline)+500 or 1000 is ok)
    P = nurbs.getCourbe3D()
    x_centerline_fit = P[0]
    y_centerline_fit = P[1]

    return x_centerline_fit, y_centerline_fit
Beispiel #4
0
    def writeCenterline(self, output_file_name=None):
        # Compute the centerline and write the float coordinates into a txt file

        nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension(self.list_file[0])

        # Define output image (size matter)
        image_concatenation = self.list_image[0].copy()
        image_concatenation.data *= 0
        image_output = self.list_image[0].copy()
        image_output.data *= 0
        # Concatenate all files by addition
        for i in range(0, len(self.list_image)):
            for s in range(0, nz) :
                image_concatenation.data[:,:,s] = image_concatenation.data[:,:,s] + self.list_image[i].data[:,:,s] #* (1/len(self.list_image))


        # get center of mass of the centerline/segmentation
        sct.printv('\nGet center of mass of the concatenate file...')
        z_centerline = [iz for iz in range(0, nz, 1) if image_concatenation.data[:, :, iz].any()]
        nz_nonz = len(z_centerline)
        x_centerline = [0 for iz in range(0, nz_nonz, 1)]
        y_centerline = [0 for iz in range(0, nz_nonz, 1)]


        # Calculate centerline coordinates and create image of the centerline
        for iz in range(0, nz_nonz, 1):
            x_centerline[iz], y_centerline[iz] = ndimage.measurements.center_of_mass(image_concatenation.data[:, :, z_centerline[iz]])

        #x_centerline_fit, y_centerline_fit,x_centerline_deriv,y_centerline_deriv,z_centerline_deriv = b_spline_centerline(x_centerline,y_centerline,z_centerline)

        points = [[x_centerline[n], y_centerline[n], z_centerline[n]] for n in range(nz_nonz)]
        nurbs = NURBS(3, 1000, points)
        P = nurbs.getCourbe3D()
        x_centerline_fit = P[0]
        y_centerline_fit = P[1]
        z_centerline_fit = P[2]

        # Create output text file
        if output_file_name != None :
            file_name = output_file_name
        else: file_name = 'generated_centerline.txt'

        sct.printv('\nWrite text file...')
        #file_results = open("../"+file_name, 'w')
        file_results = open(file_name, 'w')
        for i in range(0, z_centerline_fit.shape[0], 1):
            file_results.write(str(int(z_centerline_fit[i])) + ' ' + str(x_centerline_fit[i]) + ' ' + str(y_centerline_fit[i]) + '\n')
        file_results.close()
Beispiel #5
0
def b_spline_centerline(x_centerline, y_centerline, z_centerline):

    print '\nFitting centerline using B-spline approximation...'
    points = [[x_centerline[n], y_centerline[n], z_centerline[n]]
              for n in range(len(x_centerline))]
    nurbs = NURBS(
        3, 3000, points
    )  # BE very careful with the spline order that you choose : if order is too high ( > 4 or 5) you need to set a higher number of Control Points (cf sct_nurbs ). For the third argument (number of points), give at least len(z_centerline)+500 or higher

    P = nurbs.getCourbe3D()
    x_centerline_fit = P[0]
    y_centerline_fit = P[1]
    Q = nurbs.getCourbe3D_deriv()
    x_centerline_deriv = Q[0]
    y_centerline_deriv = Q[1]
    z_centerline_deriv = Q[2]

    return x_centerline_fit, y_centerline_fit, x_centerline_deriv, y_centerline_deriv, z_centerline_deriv
Beispiel #6
0
    def compute(self):
        nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension(self.list_file[0])

        # Define output image (size matter)
        image_concatenation = self.list_image[0].copy()
        image_concatenation.data *= 0
        image_output = self.list_image[0].copy()
        image_output.data *= 0
        # Concatenate all files by addition
        for i in range(0, len(self.list_image)):
            for s in range(0, nz) :
                image_concatenation.data[:,:,s] = image_concatenation.data[:,:,s] + self.list_image[i].data[:,:,s] #* (1/len(self.list_image))


        # get center of mass of the centerline/segmentation
        sct.printv('\nGet center of mass of the concatenate file...')
        z_centerline = [iz for iz in range(0, nz, 1) if image_concatenation.data[:, :, iz].any()]

        nz_nonz = len(z_centerline)
        x_centerline = [0 for iz in range(0, nz_nonz, 1)]
        y_centerline = [0 for iz in range(0, nz_nonz, 1)]


        # Calculate centerline coordinates and create image of the centerline
        for iz in range(0, nz_nonz, 1):
            x_centerline[iz], y_centerline[iz] = ndimage.measurements.center_of_mass(image_concatenation.data[:, :, z_centerline[iz]])

        points = [[x_centerline[n],y_centerline[n], z_centerline[n]] for n in range(len(z_centerline))]
        nurbs = NURBS(3, 1000, points)
        P = nurbs.getCourbe3D()
        x_centerline_fit = P[0]
        y_centerline_fit = P[1]
        z_centerline_fit = P[2]

        #x_centerline_fit, y_centerline_fit,x_centerline_deriv,y_centerline_deriv,z_centerline_deriv = b_spline_centerline(x_centerline,y_centerline,z_centerline)

        for iz in range(0, z_centerline_fit.shape[0], 1):
            image_output.data[x_centerline_fit[iz], y_centerline_fit[iz], z_centerline_fit[iz]] = 1


        return image_output
Beispiel #7
0
def b_spline_nurbs(x,
                   y,
                   z,
                   fname_centerline=None,
                   degree=3,
                   point_number=3000,
                   nbControl=-1,
                   verbose=1,
                   all_slices=True):

    from math import log
    from msct_nurbs import NURBS

    twodim = False
    if z == None:
        twodim = True
    """x.reverse()
    y.reverse()
    z.reverse()"""

    sct.printv('\nFitting centerline using B-spline approximation...', verbose)
    if not twodim:
        data = [[x[n], y[n], z[n]] for n in range(len(x))]
    else:
        data = [[x[n], y[n]] for n in range(len(x))]

    # if control_points == 0:
    #     nurbs = NURBS(degree, point_number, data) # BE very careful with the spline order that you choose : if order is too high ( > 4 or 5) you need to set a higher number of Control Points (cf sct_nurbs ). For the third argument (number of points), give at least len(z_centerline)+500 or higher
    # else:
    #     print 'In b_spline_nurbs we get control_point = ', control_points
    #     nurbs = NURBS(degree, point_number, data, False, control_points)

    if nbControl == -1:
        centerlineSize = getSize(x, y, z, fname_centerline)
        nbControl = 30 * log(centerlineSize, 10) - 42
        nbControl = round(nbControl)

    nurbs = NURBS(degree,
                  point_number,
                  data,
                  False,
                  nbControl,
                  verbose,
                  all_slices=all_slices,
                  twodim=twodim)

    if not twodim:
        P = nurbs.getCourbe3D()
        x_fit = P[0]
        y_fit = P[1]
        z_fit = P[2]
        Q = nurbs.getCourbe3D_deriv()
        x_deriv = Q[0]
        y_deriv = Q[1]
        z_deriv = Q[2]
    else:
        P = nurbs.getCourbe2D()
        x_fit = P[0]
        y_fit = P[1]
        Q = nurbs.getCourbe2D_deriv()
        x_deriv = Q[0]
        y_deriv = Q[1]
    """x_fit = x_fit[::-1]
    y_fit = x_fit[::-1]
    z_fit = x_fit[::-1]
    x_deriv = x_fit[::-1]
    y_deriv = x_fit[::-1]
    z_deriv = x_fit[::-1]"""

    if verbose == 2:
        PC = nurbs.getControle()
        PC_x = [p[0] for p in PC]
        PC_y = [p[1] for p in PC]
        if not twodim:
            PC_z = [p[2] for p in PC]

        import matplotlib.pyplot as plt
        if not twodim:
            plt.figure(1)
            #ax = plt.subplot(211)
            plt.subplot(211)
            plt.plot(z, x, 'r.')
            plt.plot(z_fit, x_fit)
            plt.plot(PC_z, PC_x, 'go')
            plt.title("X")
            # ax.set_aspect('equal')
            plt.xlabel('z')
            plt.ylabel('x')
            #ay = plt.subplot(212)
            plt.subplot(212)
            plt.plot(z, y, 'r.')
            plt.plot(z_fit, y_fit)
            plt.plot(PC_z, PC_y, 'go')
            plt.title("Y")
            # ay.set_aspect('equal')
            plt.xlabel('z')
            plt.ylabel('y')
            plt.show()
        else:
            plt.figure(1)
            plt.plot(y, x, 'r.')
            plt.plot(y_fit, x_fit)
            plt.plot(PC_y, PC_x, 'go')
            # ax.set_aspect('equal')
            plt.xlabel('y')
            plt.ylabel('x')
            plt.show()

    if not twodim:
        return x_fit, y_fit, z_fit, x_deriv, y_deriv, z_deriv, nurbs.error_curve_that_last_worked
    else:
        return x_fit, y_fit, x_deriv, y_deriv, nurbs.error_curve_that_last_worked
def b_spline_nurbs(x, y, z, fname_centerline=None, degree=3, point_number=3000, nbControl=-1, verbose=1):

    from math import log
    from msct_nurbs import NURBS

    """x.reverse()
    y.reverse()
    z.reverse()"""
          
    sct.printv('\nFitting centerline using B-spline approximation...', verbose)
    data = [[x[n], y[n], z[n]] for n in range(len(x))]

    # if control_points == 0:
    #     nurbs = NURBS(degree, point_number, data) # BE very careful with the spline order that you choose : if order is too high ( > 4 or 5) you need to set a higher number of Control Points (cf sct_nurbs ). For the third argument (number of points), give at least len(z_centerline)+500 or higher
    # else:
    #     print 'In b_spline_nurbs we get control_point = ', control_points
    #     nurbs = NURBS(degree, point_number, data, False, control_points)

    if nbControl == -1:
        centerlineSize = getSize(x, y, z, fname_centerline)
        nbControl = 30*log(centerlineSize, 10) - 42
        nbControl = round(nbControl)

    nurbs = NURBS(degree, point_number, data, False, nbControl, verbose)

    P = nurbs.getCourbe3D()
    x_fit=P[0]
    y_fit=P[1]
    z_fit=P[2]
    Q = nurbs.getCourbe3D_deriv()
    x_deriv=Q[0]
    y_deriv=Q[1]
    z_deriv=Q[2]

    """x_fit = x_fit[::-1]
    y_fit = x_fit[::-1]
    z_fit = x_fit[::-1]
    x_deriv = x_fit[::-1]
    y_deriv = x_fit[::-1]
    z_deriv = x_fit[::-1]"""

    PC = nurbs.getControle()
    PC_x = [p[0] for p in PC]
    PC_y = [p[1] for p in PC]
    PC_z = [p[2] for p in PC]

    if verbose == 2:
        import matplotlib.pyplot as plt
        plt.figure(1)
        #ax = plt.subplot(211)
        plt.subplot(211)
        plt.plot(z, x, 'r.')
        plt.plot(z_fit, x_fit)
        plt.plot(PC_z,PC_x,'go')
        plt.title("X")
        #ax.set_aspect('equal')
        plt.xlabel('z')
        plt.ylabel('x')
        #ay = plt.subplot(212)
        plt.subplot(212)
        plt.plot(z, y, 'r.')
        plt.plot(z_fit, y_fit)
        plt.plot(PC_z,PC_y,'go')
        plt.title("Y")
        #ay.set_aspect('equal')
        plt.xlabel('z')
        plt.ylabel('y')
        plt.show()
  
    return x_fit, y_fit, z_fit, x_deriv, y_deriv, z_deriv
Beispiel #9
0
    def getCenterline(self, type='', output_file_name=None, verbose=0):
        # Compute the centerline and save it into a image file of type "type"

        nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension(self.list_file[0])

        # Define output image (size matter)
        image_concatenation = self.list_image[0].copy()
        image_concatenation.data *= 0
        image_output = self.list_image[0].copy()
        image_output.data *= 0
        # Concatenate all files by addition
        for i in range(0, len(self.list_image)):
            for s in range(0, nz) :
                image_concatenation.data[:,:,s] = image_concatenation.data[:,:,s] + self.list_image[i].data[:,:,s] #* (1/len(self.list_image))
        print image_concatenation.data[:,:,414]

        # get center of mass of the centerline/segmentation
        sct.printv('\nGet center of mass of the concatenate file...')
        z_centerline = [iz for iz in range(0, nz, 1) if image_concatenation.data[:, :, iz].any()]
        nz_nonz = len(z_centerline)
        x_centerline = [0 for iz in range(0, nz_nonz, 1)]
        y_centerline = [0 for iz in range(0, nz_nonz, 1)]


        # Calculate centerline coordinates and create image of the centerline
        for iz in range(0, nz_nonz, 1):
            x_centerline[iz], y_centerline[iz] = ndimage.measurements.center_of_mass(image_concatenation.data[:, :, z_centerline[iz]])
        #x_centerline_fit, y_centerline_fit,x_centerline_deriv,y_centerline_deriv,z_centerline_deriv = b_spline_centerline(x_centerline,y_centerline,z_centerline)


        points = [[x_centerline[n], y_centerline[n], z_centerline[n]] for n in range(nz_nonz)]
        nurbs = NURBS(3, 1000, points, nbControl=None)
        P = nurbs.getCourbe3D()
        x_centerline_fit = P[0]
        y_centerline_fit = P[1]
        z_centerline_fit = P[2]

        if verbose==1 :
                import matplotlib.pyplot as plt

                #Creation of a vector x that takes into account the distance between the labels
                x_display = [0 for i in range(x_centerline_fit.shape[0])]
                y_display = [0 for i in range(y_centerline_fit.shape[0])]
                for i in range(0, nz_nonz, 1):
                    x_display[z_centerline[i]-z_centerline[0]] = x_centerline[i]
                    y_display[z_centerline[i]-z_centerline[0]] = y_centerline[i]

                plt.figure(1)
                plt.subplot(2,1,1)
                #plt.plot(z_centerline,x_centerline, 'ro')
                plt.plot(z_centerline_fit, x_display, 'ro')
                plt.plot(z_centerline_fit, x_centerline_fit)
                plt.xlabel("Z")
                plt.ylabel("X")
                plt.title("x and x_fit coordinates")

                plt.subplot(2,1,2)
                #plt.plot(z_centerline,y_centerline, 'ro')
                plt.plot(z_centerline_fit, y_display, 'ro')
                plt.plot(z_centerline_fit, y_centerline_fit)
                plt.xlabel("Z")
                plt.ylabel("Y")
                plt.title("y and y_fit coordinates")
                plt.show()


        for iz in range(0, z_centerline_fit.shape[0], 1):
            image_output.data[int(round(x_centerline_fit[iz])), int(round(y_centerline_fit[iz])), z_centerline_fit[iz]] = 1

        #image_output.save(type)
        file_load = nibabel.load(self.list_file[0])
        data = file_load.get_data()
        hdr = file_load.get_header()

        print '\nWrite NIFTI volumes...'
        img = nibabel.Nifti1Image(image_output.data, None, hdr)
        if output_file_name != None :
            file_name = output_file_name
        else: file_name = 'generated_centerline.nii.gz'
        nibabel.save(img,file_name)


        # to view results
        print '\nDone !'
        print '\nTo view results, type:'
        print 'fslview '+file_name+' &\n'