Beispiel #1
0
def transform_polydata_from_disk(in_filename, transform_filename, out_filename):
    # Read it in.
    print "<io.py> Transforming ", in_filename, "->", out_filename, "..."

    # Read the transform from disk because we cannot pickle it
    (root, ext) = os.path.splitext(transform_filename)
    print root, ext
    if ext == '.xfm':
        reader = vtk.vtkMNITransformReader()
        reader.SetFileName(transform_filename)
        reader.Update()
        transform = reader.GetTransform()
    elif ext == '.img':
        reader = vtk.vtkImageReader()
        reader.SetFileName(transform_filename)
        reader.Update()
        coeffs = reader.GetOutput()
        transform = vtk.vtkBSplineTransform()
        transform.SetCoefficients(coeffs)
        print coeffs
        print transform
    else:
        f = open(transform_filename, 'r')
        transform = vtk.vtkTransform()
        matrix = vtk.vtkMatrix4x4()
        for i in range(0,4):
            for j in range(0,4):
                matrix_val = float(f.readline())
                matrix.SetElement(i,j, matrix_val)
        transform.SetMatrix(matrix)
        del matrix

    start_time = time.time()
    pd = read_polydata(in_filename)
    elapsed_time = time.time() - start_time
    print "READ:", elapsed_time
    # Transform it.
    start_time = time.time()
    transformer = vtk.vtkTransformPolyDataFilter()
    if (vtk.vtkVersion().GetVTKMajorVersion() >= 6.0):
        transformer.SetInputData(pd)
    else:
        transformer.SetInput(pd)
    transformer.SetTransform(transform)
    transformer.Update()
    elapsed_time = time.time() - start_time
    print "TXFORM:", elapsed_time

    # Write it out.
    start_time = time.time()
    pd2 = transformer.GetOutput()
    write_polydata(pd2, out_filename)
    elapsed_time = time.time() - start_time
    print "WRITE:", elapsed_time

    # Clean up.
    del transformer
    del pd2
    del pd
    del transform
Beispiel #2
0
def transform_polydata_from_disk(in_filename, transform_filename, out_filename):
    # Read it in.
    print "<io.py> Transforming ", in_filename, "->", out_filename, "..."

    # Read the transform from disk because we cannot pickle it
    (root, ext) = os.path.splitext(transform_filename)
    print root, ext
    if ext == '.xfm':
        reader = vtk.vtkMNITransformReader()
        reader.SetFileName(transform_filename)
        reader.Update()
        transform = reader.GetTransform()
    elif ext == '.img':
        reader = vtk.vtkImageReader()
        reader.SetFileName(transform_filename)
        reader.Update()
        coeffs = reader.GetOutput()
        transform = vtk.vtkBSplineTransform()
        transform.SetCoefficients(coeffs)
        print coeffs
        print transform
    else:
        f = open(transform_filename, 'r')
        transform = vtk.vtkTransform()
        matrix = vtk.vtkMatrix4x4()
        for i in range(0,4):
            for j in range(0,4):
                matrix_val = float(f.readline())
                matrix.SetElement(i,j, matrix_val)
        transform.SetMatrix(matrix)
        del matrix

    start_time = time.time()
    pd = read_polydata(in_filename)
    elapsed_time = time.time() - start_time
    print "READ:", elapsed_time
    # Transform it.
    start_time = time.time()
    transformer = vtk.vtkTransformPolyDataFilter()
    if (vtk.vtkVersion().GetVTKMajorVersion() >= 6.0):
        transformer.SetInputData(pd)
    else:
        transformer.SetInput(pd)
    transformer.SetTransform(transform)
    transformer.Update()
    elapsed_time = time.time() - start_time
    print "TXFORM:", elapsed_time

    # Write it out.
    start_time = time.time()
    pd2 = transformer.GetOutput()
    write_polydata(pd2, out_filename)
    elapsed_time = time.time() - start_time
    print "WRITE:", elapsed_time

    # Clean up.
    del transformer
    del pd2
    del pd
    del transform
Beispiel #3
0
def pipeline(inputPolyDatas, sidx, args):
    # get subject identifier from unique input filename
    # -------------------
    #subjectID = os.path.splitext(os.path.basename(inputPolyDatas[sidx]))[0]
    fname = os.path.basename(inputPolyDatas[sidx])
    print("<wm_preprocess.py> ", sidx + 1, "/", len(inputPolyDatas))

    # read input vtk data
    # -------------------
    inpd = wma.io.read_polydata(inputPolyDatas[sidx])

    # Read in the transform
    reader = vtk.vtkMNITransformReader()
    reader.SetFileName(args.transformFile)
    reader.Update()
    transform = reader.GetTransform()

    if args.invert_flag == True:
        transform.Inverse()

    # Apply the transform
    transformer = vtk.vtkTransformPolyDataFilter()
    transformer.SetInput(inpd)
    transformer.SetTransform(transform)
    transformer.Update()
    outpd = transformer.GetOutput()
        
    # outputs
    # -------------------
    fname = os.path.join(args.outputDirectory, fname)
    try:
        print("Writing output polydata", fname, "...")
        wma.io.write_polydata(outpd, fname)
    except:
        print("Unknown exception in IO")
        raise
    del outpd
    del inpd
def pipeline(inputPolyDatas, sidx, args):
    # get subject identifier from unique input filename
    # -------------------
    #subjectID = os.path.splitext(os.path.basename(inputPolyDatas[sidx]))[0]
    fname = os.path.basename(inputPolyDatas[sidx])
    print "<wm_preprocess.py> ", sidx + 1, "/", len(inputPolyDatas)

    # read input vtk data
    # -------------------
    inpd = wma.io.read_polydata(inputPolyDatas[sidx])

    # Read in the transform
    reader = vtk.vtkMNITransformReader()
    reader.SetFileName(args.transformFile)
    reader.Update()
    transform = reader.GetTransform()

    if args.invert_flag == True:
        transform.Inverse()

    # Apply the transform
    transformer = vtk.vtkTransformPolyDataFilter()
    transformer.SetInput(inpd)
    transformer.SetTransform(transform)
    transformer.Update()
    outpd = transformer.GetOutput()
        
    # outputs
    # -------------------
    fname = os.path.join(args.outputDirectory, fname)
    try:
        print "Writing output polydata", fname, "..."
        wma.io.write_polydata(outpd, fname)
    except:
        print "Unknown exception in IO"
        raise
    del outpd
    del inpd
    p2.SetPoint(5, 159, 159, 0)
    p2.SetPoint(6, 159, 96, 0)
    p2.SetPoint(7, 96, 96, 0)

    thinPlate0 = vtk.vtkThinPlateSplineTransform()
    thinPlate0.SetSourceLandmarks(p1)
    thinPlate0.SetTargetLandmarks(p2)
    thinPlate0.SetBasisToR2LogR()

    # write the tps to a file
    tpsWriter = vtk.vtkMNITransformWriter()
    tpsWriter.SetFileName(filename)
    tpsWriter.SetTransform(thinPlate0)
    tpsWriter.Write()
    # read it back
    tpsReader = vtk.vtkMNITransformReader()
    if (tpsReader.CanReadFile(filename) != 0):
        tpsReader.SetFileName(filename)

        thinPlate = tpsReader.GetTransform()

        # make a linear transform
        linearTransform = vtk.vtkTransform()
        linearTransform.PostMultiply()
        linearTransform.Translate(-127.5, -127.5, 0)
        linearTransform.RotateZ(30)
        linearTransform.Translate(+127.5, +127.5, 0)

        # remove the linear part of the thin plate
        tpsGeneral = vtk.vtkGeneralTransform()
        tpsGeneral.SetInput(thinPlate)
Beispiel #6
0
    p2.SetPoint(5, 159, 159, 0)
    p2.SetPoint(6, 159, 96, 0)
    p2.SetPoint(7, 96, 96, 0)

    thinPlate0 = vtk.vtkThinPlateSplineTransform()
    thinPlate0.SetSourceLandmarks(p1)
    thinPlate0.SetTargetLandmarks(p2)
    thinPlate0.SetBasisToR2LogR()

    # write the tps to a file
    tpsWriter = vtk.vtkMNITransformWriter()
    tpsWriter.SetFileName(filename)
    tpsWriter.SetTransform(thinPlate0)
    tpsWriter.Write()
    # read it back
    tpsReader = vtk.vtkMNITransformReader()
    if (tpsReader.CanReadFile(filename) != 0):
        tpsReader.SetFileName(filename)

        thinPlate = tpsReader.GetTransform()

        # make a linear transform
        linearTransform = vtk.vtkTransform()
        linearTransform.PostMultiply()
        linearTransform.Translate(-127.5, -127.5, 0)
        linearTransform.RotateZ(30)
        linearTransform.Translate(+127.5, +127.5, 0)

        # remove the linear part of the thin plate
        tpsGeneral = vtk.vtkGeneralTransform()
        tpsGeneral.SetInput(thinPlate)
Beispiel #7
0

print("==========================")

# =======================================================================
# Above this line is argument parsing. Below this line is the pipeline.
# =======================================================================


# Loop over input DWIs
inputPolyDatas = wma.io.list_vtk_files(args.inputDirectory)

print("<wm_preprocess.py> Input number of files: ", len(inputPolyDatas))

# Read in the transform to print its contents for the user
reader = vtk.vtkMNITransformReader()
reader.SetFileName(args.transformFile)
reader.Update()
transform = reader.GetTransform()
print(transform)
    
def pipeline(inputPolyDatas, sidx, args):
    # get subject identifier from unique input filename
    # -------------------
    #subjectID = os.path.splitext(os.path.basename(inputPolyDatas[sidx]))[0]
    fname = os.path.basename(inputPolyDatas[sidx])
    print("<wm_preprocess.py> ", sidx + 1, "/", len(inputPolyDatas))

    # read input vtk data
    # -------------------
    inpd = wma.io.read_polydata(inputPolyDatas[sidx])

print "=========================="

# =======================================================================
# Above this line is argument parsing. Below this line is the pipeline.
# =======================================================================


# Loop over input DWIs
inputPolyDatas = wma.io.list_vtk_files(args.inputDirectory)

print "<wm_preprocess.py> Input number of files: ", len(inputPolyDatas)

# Read in the transform to print its contents for the user
reader = vtk.vtkMNITransformReader()
reader.SetFileName(args.transformFile)
reader.Update()
transform = reader.GetTransform()
print transform
    
def pipeline(inputPolyDatas, sidx, args):
    # get subject identifier from unique input filename
    # -------------------
    #subjectID = os.path.splitext(os.path.basename(inputPolyDatas[sidx]))[0]
    fname = os.path.basename(inputPolyDatas[sidx])
    print "<wm_preprocess.py> ", sidx + 1, "/", len(inputPolyDatas)

    # read input vtk data
    # -------------------
    inpd = wma.io.read_polydata(inputPolyDatas[sidx])