Пример #1
0
def initImage(image=None, image_filename=None, verbose=0):

    assert ((image is not None)
            or (image_filename
                is not None)), "Need an image or an image_filename. Aborting."

    if image is None:
        return myVTK.readImage(filename=image_filename, verbose=verbose)
    else:
        return image
Пример #2
0
def initImage(
        image=None,
        image_filename=None,
        verbose=1):

    assert ((image is not None) or (image_filename is not None)), "Need an image or an image_filename. Aborting."

    if image is None:
        return myVTK.readImage(
            filename=image_filename,
            verbose=verbose)
    else:
        return image
Пример #3
0
def readDataSet(filename, verbose=0):

    mypy.my_print(verbose, "*** readDataSet: " + filename + " ***")

    assert (os.path.isfile(filename)
            ), "Wrong filename (\"" + filename + "\"). Aborting."

    file_ext = filename[-3:]
    if (file_ext == "vtk"):
        for line in open(filename):
            if (line.split()[0] == "DATASET"):
                dataset_type = line.split()[-1]
                break
        assert ("dataset_type" in locals()), "Wrong file format. Aborting."

        if (dataset_type == "STRUCTURED_POINTS"):
            return myvtk.readSPoints(filename=filename, verbose=verbose - 1)
        elif (dataset_type == "STRUCTURED_GRID"):
            return myvtk.readSGrid(filename=filename, verbose=verbose - 1)
        elif (dataset_type == "UNSTRUCTURED_GRID"):
            return myvtk.readUGrid(filename=filename, verbose=verbose - 1)
        elif (dataset_type == "POLYDATA"):
            return myvtk.readPData(filename=filename, verbose=verbose - 1)
        elif (dataset_type == "RECTILINEAR_GRID") or (dataset_type == "FIELD"):
            assert 0, "Not implemented. Aborting."
        else:
            assert 0, "Wrong dataset type (" + dataset_type + "). Aborting."
    elif (file_ext == "vti"):
        return myvtk.readImage(filename=filename, verbose=verbose - 1)
    elif (file_ext == "vtp") or (file_ext == "stl"):
        return myvtk.readPData(filename=filename, verbose=verbose - 1)
    elif (file_ext == "vts"):
        return myvtk.readSGrid(filename=filename, verbose=verbose - 1)
    elif (file_ext == "vtu"):
        return myvtk.readUGrid(filename=filename, verbose=verbose - 1)
    else:
        assert 0, "Wrong extention (" + file_ext + "). Extention must be vtk, vti, vtp, stl, vts, vtu. Aborting."
Пример #4
0
        else:
            image_append_components.AddInput(image_w_hessian_component)

    image_append_components.Update()
    image_w_hessian = image_append_components.GetOutput()

    name = image.GetPointData().GetScalars().GetName()
    image.GetPointData().AddArray(
        image_w_gradient.GetPointData().GetArray(name + "Gradient"))
    image.GetPointData().AddArray(
        image_w_hessian.GetPointData().GetArray(name + "GradientGradient"))
    image.GetPointData().SetActiveScalars(name + "GradientGradient")


########################################################################

if (__name__ == "__main__"):
    parser = argparse.ArgumentParser()
    parser.add_argument("image_filename", type=str)
    parser.add_argument("--verbose", "-v", type=int, default=1)
    args = parser.parse_args()

    image = myvtk.readImage(filename=args.image_filename, verbose=args.verbose)

    myvtk.addImageHessian(image=image, verbose=args.verbose)

    myvtk.writeImage(image=image,
                     filename=args.image_filename.replace(
                         ".vti", "-hessian.vti"),
                     verbose=args.verbose)
def generateImages(
        images,
        structure,
        texture,
        noise,
        deformation,
        evolution,
        verbose=1):

    myVTK.myPrint(verbose, "*** generateImages ***")

    vtk_image = vtk.vtkImageData()

    if   (images["n_dim"] == 1):
        vtk_image.SetExtent([0, images["n_voxels"][0]-1, 0,                       0, 0,                       0])
    elif (images["n_dim"] == 2):
        vtk_image.SetExtent([0, images["n_voxels"][0]-1, 0, images["n_voxels"][1]-1, 0,                       0])
    elif (images["n_dim"] == 3):
        vtk_image.SetExtent([0, images["n_voxels"][0]-1, 0, images["n_voxels"][1]-1, 0, images["n_voxels"][2]-1])
    else:
        assert (0), "n_dim must be \"1\", \"2\" or \"3\". Aborting."

    spacing = numpy.array(images["L"])/numpy.array(images["n_voxels"])
    if (images["n_dim"] == 1):
        spacing = [spacing[0], 1., 1.]
    elif (images["n_dim"] == 2):
        spacing = [spacing[0], spacing[1], 1.]
    vtk_image.SetSpacing(spacing)

    origin = numpy.array(vtk_image.GetSpacing())/2
    if   (images["n_dim"] == 1):
        origin[1] = 0.
        origin[2] = 0.
    elif (images["n_dim"] == 2):
        origin[2] = 0.
    vtk_image.SetOrigin(origin)
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        vtk_image.AllocateScalars(vtk.VTK_FLOAT, 1)
    else:
        vtk_image.SetScalarTypeToFloat();
        vtk_image.SetNumberOfScalarComponents(1);
        vtk_image.AllocateScalars()
    vtk_image_scalars = vtk_image.GetPointData().GetScalars()

    if not os.path.exists(images["images_folder"]):
        os.mkdir(images["images_folder"])

    x0 = numpy.empty(3)
    x  = numpy.empty(3)
    if   (images["n_dim"] == 1):
        dx = spacing[0]/images["n_integration"][0]
    elif (images["n_dim"] == 2):
        dx = spacing[0]/images["n_integration"][0]
        dy = spacing[1]/images["n_integration"][1]
    elif (images["n_dim"] == 3):
        dx = spacing[0]/images["n_integration"][0]
        dy = spacing[1]/images["n_integration"][1]
        dz = spacing[2]/images["n_integration"][2]
    else:
        assert (0), "n_dim must be \"1\", \"2\" or \"3\". Aborting."
    global_min = float("+Inf")
    global_max = float("-Inf")
    image = Image(images, structure, texture, noise)
    mapping = Mapping(images, structure, deformation, evolution)
    for k_frame in xrange(images["n_frames"]):
        t = images["T"]*float(k_frame)/(images["n_frames"]-1) if (images["n_frames"]>1) else 0.
        mapping.init_t(t)
        for k_point in xrange(vtk_image.GetNumberOfPoints()):
            vtk_image.GetPoint(k_point, x0)
            #print "x0 = "+str(x0)
            x[:] = x0[:]
            I = 0.
            if   (images["n_dim"] == 1):
                for k_x in xrange(images["n_integration"][0]):
                    x[0] = x0[0] - dx/2 + (k_x+1./2)*dx/images["n_integration"][0]
                    I += image.I0(mapping.X(x))
                I /= images["n_integration"][0]
            elif (images["n_dim"] == 2):
                for k_y in xrange(images["n_integration"][1]):
                    x[1] = x0[1] - dy/2 + (k_y+1./2)*dy/images["n_integration"][1]
                    for k_x in xrange(images["n_integration"][0]):
                        x[0] = x0[0] - dx/2 + (k_x+1./2)*dx/images["n_integration"][0]
                        I += image.I0(mapping.X(x))
                I /= images["n_integration"][1]*images["n_integration"][0]
            elif (images["n_dim"] == 3):
                for k_z in xrange(images["n_integration"][2]):
                    x[2] = x0[2] - dz/2 + (k_z+1./2)*dz/images["n_integration"][2]
                    for k_y in xrange(images["n_integration"][1]):
                        x[1] = x0[1] - dy/2 + (k_y+1./2)*dy/images["n_integration"][1]
                        for k_x in xrange(images["n_integration"][0]):
                            x[0] = x0[0] - dx/2 + (k_x+1./2)*dx/images["n_integration"][0]
                            #print "x = "+str(x)
                            I += image.I0(mapping.X(x))
                            #print "I = "+str(I)
                I /= images["n_integration"][2]*images["n_integration"][1]*images["n_integration"][0]
            else:
                assert (0), "n_dim must be \"1\", \"2\" or \"3\". Aborting."
            vtk_image_scalars.SetTuple1(k_point, I)
            if (I < global_min): global_min = I
            if (I > global_max): global_max = I
        myVTK.writeImage(
            image=vtk_image,
            filename=images["images_folder"]+"/"+images["images_basename"]+"_"+str(k_frame).zfill(2)+".vti")

    if (images["data_type"] in ("float")):
        pass
    elif (images["data_type"] in ("unsigned char", "unsigned short", "unsigned int", "unsigned long", "unsigned float", "uint8", "uint16", "uint32", "uint64", "ufloat")):
        print "global_min = "+str(global_min)
        print "global_max = "+str(global_max)
        shifter = vtk.vtkImageShiftScale()
        shifter.SetShift(-global_min)
        if   (images["data_type"] in ("unsigned char", "uint8")):
            shifter.SetScale(float(2**8-1)/(global_max-global_min))
            shifter.SetOutputScalarTypeToUnsignedChar()
        elif (images["data_type"] in ("unsigned short", "uint16")):
            shifter.SetScale(float(2**16-1)/(global_max-global_min))
            shifter.SetOutputScalarTypeToUnsignedShort()
        elif (images["data_type"] in ("unsigned int", "uint32")):
            shifter.SetScale(float(2**32-1)/(global_max-global_min))
            shifter.SetOutputScalarTypeToUnsignedInt()
        elif (images["data_type"] in ("unsigned long", "uint64")):
            shifter.SetScale(float(2**64-1)/(global_max-global_min))
            shifter.SetOutputScalarTypeToUnsignedLong()
        elif (images["data_type"] in ("unsigned float", "ufloat")):
            shifter.SetScale(1./(global_max-global_min))
            shifter.SetOutputScalarTypeToFloat()
        for k_frame in xrange(images["n_frames"]):
            vtk_image = myVTK.readImage(
                filename=images["images_folder"]+"/"+images["images_basename"]+"_"+str(k_frame).zfill(2)+".vti")
            if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
                shifter.SetInputData(vtk_image)
            else:
                shifter.SetInput(vtk_image)
            shifter.Update()
            vtk_image = shifter.GetOutput()
            myVTK.writeImage(
                image=vtk_image,
                filename=images["images_folder"]+"/"+images["images_basename"]+"_"+str(k_frame).zfill(2)+".vti")
    else:
        assert (0), "Wrong data type. Aborting."
Пример #6
0
    else:
        image_gradient.SetInput(image)
    image_gradient.SetDimensionality(image_dimensionality)
    image_gradient.Update()
    image_w_grad = image_gradient.GetOutput()

    name = image.GetPointData().GetScalars().GetName()
    image.GetPointData().AddArray(image_w_gradient.GetPointData().GetArray(name+"Gradient"))
    image.GetPointData().SetActiveScalars(name+"Gradient")

########################################################################

if (__name__ == "__main__"):
    parser = argparse.ArgumentParser()
    parser.add_argument("image_filename", type=str)
    parser.add_argument("--verbose", "-v", type=int, default=1)
    args = parser.parse_args()

    image = myvtk.readImage(
        filename=args.image_filename,
        verbose=args.verbose)

    myvtk.addImageGradient(
        image=image,
        verbose=args.verbose)

    myvtk.writeImage(
        image=image,
        filename=args.image_filename.replace(".vti", "-gradient.vti"),
        verbose=args.verbose)
def generateUnwarpedImages(
        images_folder,
        images_basename,
        sol_folder,
        sol_basename,
        sol_ext="vtu",
        verbose=0):

    myVTK.myPrint(verbose, "*** generateUnwarpedImages ***")

    ref_image_zfill = len(glob.glob(images_folder+"/"+images_basename+"_*.vti")[0].rsplit("_")[-1].split(".")[0])
    ref_image_filename = images_folder+"/"+images_basename+"_"+str(0).zfill(ref_image_zfill)+".vti"
    ref_image = myVTK.readImage(
        filename=ref_image_filename)

    image = vtk.vtkImageData()
    image.SetOrigin(ref_image.GetOrigin())
    image.SetSpacing(ref_image.GetSpacing())
    image.SetExtent(ref_image.GetExtent())
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        image.AllocateScalars(vtk.VTK_FLOAT, 1)
    else:
        image.SetScalarTypeToFloat()
        image.SetNumberOfScalarComponents(1)
        image.AllocateScalars()
    scalars = image.GetPointData().GetScalars()

    sol_zfill = len(glob.glob(sol_folder+"/"+sol_basename+"_*."+sol_ext)[0].rsplit("_")[-1].split(".")[0])
    n_frames = len(glob.glob(sol_folder+"/"+sol_basename+"_"+"[0-9]"*sol_zfill+"."+sol_ext))
    #n_frames = 1

    X = numpy.empty(3)
    U = numpy.empty(3)
    x = numpy.empty(3)
    I = numpy.empty(1)
    m = numpy.empty(1)
    for k_frame in xrange(n_frames):
        myVTK.myPrint(verbose, "k_frame = "+str(k_frame))

        def_image = myVTK.readImage(
            filename=images_folder+"/"+images_basename+"_"+str(k_frame).zfill(ref_image_zfill)+".vti")

        interpolator = myVTK.createImageInterpolator(
            image=def_image)

        mesh = myVTK.readUGrid(
            filename=sol_folder+"/"+sol_basename+"_"+str(k_frame).zfill(sol_zfill)+"."+sol_ext)

        probe = vtk.vtkProbeFilter()
        if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
            probe.SetInputData(image)
            probe.SetSourceData(mesh)
        else:
            probe.SetInput(image)
            probe.SetSource(mesh)
        probe.Update()
        probed_image = probe.GetOutput()
        scalars_mask = probed_image.GetPointData().GetArray("vtkValidPointMask")
        scalars_U = probed_image.GetPointData().GetArray("displacement")

        for k_point in xrange(image.GetNumberOfPoints()):
            scalars_mask.GetTuple(k_point, m)
            if (m[0] == 0):
                I[0] = 0.
            else:
                image.GetPoint(k_point, X)
                scalars_U.GetTuple(k_point, U)
                x = X + U
                interpolator.Interpolate(x, I)
            scalars.SetTuple(k_point, I)

        myVTK.writeImage(
            image=image,
            filename=sol_folder+"/"+sol_basename+"-unwarped_"+str(k_frame).zfill(sol_zfill)+".vti")
def generateUndersampledImages(
        images,
        structure,
        texture,
        noise,
        deformation,
        evolution,
        undersampling_level,
        keep_temporary_images=0,
        verbose=0):

    myVTK.myPrint(verbose, "*** generateUndersampledImages ***")

    images_basename = images["basename"]
    images_n_voxels = images["n_voxels"][:]

    images["n_voxels"][1] /= undersampling_level
    if (images["n_dim"] >= 3):
        images["n_voxels"][2] /= undersampling_level
    images["basename"] = images_basename+"-X"
    texture["type"] = "taggX"
    myVTK.generateImages(
        images=images,
        structure=structure,
        texture=texture,
        noise=noise,
        deformation=deformation,
        evolution=evolution,
        verbose=verbose-1)
    images["n_voxels"] = images_n_voxels[:]
    images["basename"] = images_basename

    images["n_voxels"][0] /= undersampling_level
    if (images["n_dim"] >= 3):
        images["n_voxels"][2] /= undersampling_level
    images["basename"] = images_basename+"-Y"
    texture["type"] = "taggY"
    myVTK.generateImages(
        images=images,
        structure=structure,
        texture=texture,
        noise=noise,
        deformation=deformation,
        evolution=evolution,
        verbose=verbose-1)
    images["n_voxels"] = images_n_voxels[:]
    images["basename"] = images_basename

    if (images["n_dim"] >= 3):
        images["n_voxels"][0] /= undersampling_level
        images["n_voxels"][1] /= undersampling_level
        images["basename"] = images_basename+"-Z"
        texture["type"] = "taggZ"
        myVTK.generateImages(
            images=images,
            structure=structure,
            texture=texture,
            noise=noise,
            deformation=deformation,
            evolution=evolution,
            verbose=verbose-1)
        images["n_voxels"] = images_n_voxels
        images["basename"] = images_basename

    if ("zfill" not in images.keys()):
        images["zfill"] = len(str(images["n_frames"]))
    for k_frame in xrange(images["n_frames"]):
        imageX = myVTK.readImage(
            filename=images["folder"]+"/"+images["basename"]+"-X_"+str(k_frame).zfill(images["zfill"])+".vti",
            verbose=verbose-1)
        interpolatorX = myVTK.createImageInterpolator(
            image=imageX,
            verbose=verbose-1)
        iX = numpy.empty(1)

        imageY = myVTK.readImage(
            filename=images["folder"]+"/"+images["basename"]+"-Y_"+str(k_frame).zfill(images["zfill"])+".vti",
            verbose=verbose-1)
        interpolatorY = myVTK.createImageInterpolator(
            image=imageY,
            verbose=verbose-1)
        iY = numpy.empty(1)

        if (images["n_dim"] == 2):
            imageXY = vtk.vtkImageData()
            imageXY.SetExtent([0, images["n_voxels"][0]-1, 0, images["n_voxels"][1]-1, 0, 0])
            imageXY.SetSpacing([images["L"][0]/images["n_voxels"][0], images["L"][1]/images["n_voxels"][1], 1.])
            imageXY.SetOrigin([images["L"][0]/images["n_voxels"][0]/2, images["L"][1]/images["n_voxels"][1]/2, 0.])
            if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
                imageXY.AllocateScalars(vtk.VTK_FLOAT, 1)
            else:
                imageXY.SetScalarTypeToFloat()
                imageXY.SetNumberOfScalarComponents(1)
                imageXY.AllocateScalars()
            scalars = imageXY.GetPointData().GetScalars()
            x = numpy.empty(3)
            for k_point in xrange(imageXY.GetNumberOfPoints()):
                imageXY.GetPoint(k_point, x)
                interpolatorX.Interpolate(x, iX)
                interpolatorY.Interpolate(x, iY)
                scalars.SetTuple(k_point, (iX*iY)**(1./2))
            myVTK.writeImage(
                image=imageXY,
                filename=images["folder"]+"/"+images["basename"]+"_"+str(k_frame).zfill(images["zfill"])+".vti",
                verbose=verbose-1)
            if not (keep_temporary_images):
                os.system("rm "+images["folder"]+"/"+images["basename"]+"-X_"+str(k_frame).zfill(images["zfill"])+".vti")
                os.system("rm "+images["folder"]+"/"+images["basename"]+"-Y_"+str(k_frame).zfill(images["zfill"])+".vti")

        elif (images["n_dim"] == 3):
            imageZ = myVTK.readImage(
                filename=images["folder"]+"/"+images["basename"]+"-Z_"+str(k_frame).zfill(images["zfill"])+".vti",
                verbose=verbose-1)
            interpolatorZ = myVTK.createImageInterpolator(
                image=imageZ,
                verbose=verbose-1)
            iZ = numpy.empty(1)

            imageXYZ = vtk.vtkImageData()
            imageXYZ.SetExtent([0, images["n_voxels"][0]-1, 0, images["n_voxels"][1]-1, 0, images["n_voxels"][2]-1])
            imageXYZ.SetSpacing([images["L"][0]/images["n_voxels"][0], images["L"][1]/images["n_voxels"][1], images["L"][2]/images["n_voxels"][2]])
            imageXYZ.SetOrigin([images["L"][0]/images["n_voxels"][0]/2, images["L"][1]/images["n_voxels"][1]/2, images["L"][2]/images["n_voxels"][2]/2])
            if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
                imageXYZ.AllocateScalars(vtk.VTK_FLOAT, 1)
            else:
                imageXYZ.SetScalarTypeToFloat()
                imageXYZ.SetNumberOfScalarComponents(1)
                imageXYZ.AllocateScalars()
            scalars = imageXYZ.GetPointData().GetScalars()
            x = numpy.empty(3)
            for k_point in xrange(imageXYZ.GetNumberOfPoints()):
                imageXYZ.GetPoint(k_point, x)
                interpolatorX.Interpolate(x, iX)
                interpolatorY.Interpolate(x, iY)
                interpolatorZ.Interpolate(x, iZ)
                scalars.SetTuple(k_point, (iX*iY*iZ)**(1./3))
            myVTK.writeImage(
                image=imageXYZ,
                filename=images["folder"]+"/"+images["basename"]+"_"+str(k_frame).zfill(images["zfill"])+".vti",
                verbose=verbose-1)
            if not (keep_temporary_images):
                os.system("rm "+images["folder"]+"/"+images["basename"]+"-X_"+str(k_frame).zfill(images["zfill"])+".vti")
                os.system("rm "+images["folder"]+"/"+images["basename"]+"-Y_"+str(k_frame).zfill(images["zfill"])+".vti")
                os.system("rm "+images["folder"]+"/"+images["basename"]+"-Z_"+str(k_frame).zfill(images["zfill"])+".vti")
def generateUnwarpedImages(images_folder,
                           images_basename,
                           sol_folder,
                           sol_basename,
                           sol_ext="vtu",
                           verbose=0):

    myVTK.myPrint(verbose, "*** generateUnwarpedImages ***")

    ref_image_zfill = len(
        glob.glob(images_folder + "/" + images_basename +
                  "_*.vti")[0].rsplit("_")[-1].split(".")[0])
    ref_image_filename = images_folder + "/" + images_basename + "_" + str(
        0).zfill(ref_image_zfill) + ".vti"
    ref_image = myVTK.readImage(filename=ref_image_filename)

    image = vtk.vtkImageData()
    image.SetOrigin(ref_image.GetOrigin())
    image.SetSpacing(ref_image.GetSpacing())
    image.SetExtent(ref_image.GetExtent())
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        image.AllocateScalars(vtk.VTK_FLOAT, 1)
    else:
        image.SetScalarTypeToFloat()
        image.SetNumberOfScalarComponents(1)
        image.AllocateScalars()
    scalars = image.GetPointData().GetScalars()

    sol_zfill = len(
        glob.glob(sol_folder + "/" + sol_basename + "_*." +
                  sol_ext)[0].rsplit("_")[-1].split(".")[0])
    n_frames = len(
        glob.glob(sol_folder + "/" + sol_basename + "_" + "[0-9]" * sol_zfill +
                  "." + sol_ext))
    #n_frames = 1

    X = numpy.empty(3)
    U = numpy.empty(3)
    x = numpy.empty(3)
    I = numpy.empty(1)
    m = numpy.empty(1)
    for k_frame in xrange(n_frames):
        myVTK.myPrint(verbose, "k_frame = " + str(k_frame))

        def_image = myVTK.readImage(
            filename=images_folder + "/" + images_basename + "_" +
            str(k_frame).zfill(ref_image_zfill) + ".vti")

        interpolator = myVTK.createImageInterpolator(image=def_image)

        mesh = myVTK.readUGrid(filename=sol_folder + "/" + sol_basename + "_" +
                               str(k_frame).zfill(sol_zfill) + "." + sol_ext)

        probe = vtk.vtkProbeFilter()
        if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
            probe.SetInputData(image)
            probe.SetSourceData(mesh)
        else:
            probe.SetInput(image)
            probe.SetSource(mesh)
        probe.Update()
        probed_image = probe.GetOutput()
        scalars_mask = probed_image.GetPointData().GetArray(
            "vtkValidPointMask")
        scalars_U = probed_image.GetPointData().GetArray("displacement")

        for k_point in xrange(image.GetNumberOfPoints()):
            scalars_mask.GetTuple(k_point, m)
            if (m[0] == 0):
                I[0] = 0.
            else:
                image.GetPoint(k_point, X)
                scalars_U.GetTuple(k_point, U)
                x = X + U
                interpolator.Interpolate(x, I)
            scalars.SetTuple(k_point, I)

        myVTK.writeImage(image=image,
                         filename=sol_folder + "/" + sol_basename +
                         "-unwarped_" + str(k_frame).zfill(sol_zfill) + ".vti")
Пример #10
0
def generateImages(
        images,
        structure,
        texture,
        noise,
        deformation,
        evolution,
        generate_image_gradient=False,
        verbose=0):

    myVTK.myPrint(verbose, "*** generateImages ***")

    vtk_image = vtk.vtkImageData()

    if   (images["n_dim"] == 1):
        vtk_image.SetExtent([0, images["n_voxels"][0]-1, 0,                       0, 0,                       0])
    elif (images["n_dim"] == 2):
        vtk_image.SetExtent([0, images["n_voxels"][0]-1, 0, images["n_voxels"][1]-1, 0,                       0])
    elif (images["n_dim"] == 3):
        vtk_image.SetExtent([0, images["n_voxels"][0]-1, 0, images["n_voxels"][1]-1, 0, images["n_voxels"][2]-1])
    else:
        assert (0), "n_dim must be \"1\", \"2\" or \"3\". Aborting."

    spacing = numpy.array(images["L"])/numpy.array(images["n_voxels"])
    if   (images["n_dim"] == 1):
        spacing = numpy.array([images["L"][0]/images["n_voxels"][0], 1., 1.])
    elif (images["n_dim"] == 2):
        spacing = numpy.array([images["L"][0]/images["n_voxels"][0], images["L"][1]/images["n_voxels"][1], 1.])
    elif (images["n_dim"] == 2):
        spacing = numpy.array([images["L"][0]/images["n_voxels"][0], images["L"][1]/images["n_voxels"][1], images["L"][2]/images["n_voxels"][2]])
    vtk_image.SetSpacing(spacing)

    origin = numpy.array(vtk_image.GetSpacing())/2
    if   (images["n_dim"] == 1):
        origin[1] = 0.
        origin[2] = 0.
    elif (images["n_dim"] == 2):
        origin[2] = 0.
    vtk_image.SetOrigin(origin)

    n_points = vtk_image.GetNumberOfPoints()
    vtk_image_scalars = myVTK.createFloatArray(
        name="ImageScalars",
        n_components=1,
        n_tuples=n_points,
        verbose=verbose-1)
    vtk_image.GetPointData().SetScalars(vtk_image_scalars)
    if (generate_image_gradient):
        vtk_image_gradient = myVTK.createFloatArray(
            name="ImageScalarsGradient",
            n_components=images["n_dim"],
            n_tuples=n_points,
            verbose=verbose-1)
        vtk_image.GetPointData().SetVectors(vtk_image_gradient)

    if not os.path.exists(images["folder"]):
        os.mkdir(images["folder"])

    x0   = numpy.empty(3)
    x    = numpy.empty(3)
    X    = numpy.empty(3)
    if (generate_image_gradient):
        F    = numpy.empty((3,3))
        Finv = numpy.empty((3,3))
    else:
        F    = None
        Finv = None
    dx   = spacing[0:images["n_dim"]]/images["n_integration"][0:images["n_dim"]]
    global_min = float("+Inf")
    global_max = float("-Inf")
    I = numpy.empty(1)
    i = numpy.empty(1)
    if (generate_image_gradient):
        G = numpy.empty(images["n_dim"])
        g = numpy.empty(images["n_dim"])
    else:
        G = None
        g = None
    image = Image(images, structure, texture, noise)
    mapping = Mapping(images, structure, deformation, evolution)
    if ("zfill" not in images.keys()):
        images["zfill"] = len(str(images["n_frames"]))
    for k_frame in xrange(images["n_frames"]):
        t = images["T"]*float(k_frame)/(images["n_frames"]-1) if (images["n_frames"]>1) else 0.
        print "t = "+str(t)
        mapping.init_t(t)
        for k_point in xrange(n_points):
            vtk_image.GetPoint(k_point, x0)
            #print "x0 = "+str(x0)
            x[:] = x0[:]
            #print "x = "+str(x)
            I[0] = 0.
            #print "I = "+str(I)
            #if (generate_image_gradient): G[:] = 0.
                #print "G = "+str(G)
            if   (images["n_dim"] == 1):
                for k_x in xrange(images["n_integration"][0]):
                    x[0] = x0[0] - dx[0]/2 + (k_x+1./2)*dx[0]/images["n_integration"][0]
                    mapping.X(x, X, Finv)
                    image.I0(X, i, g)
                    I += i
                    #if (generate_image_gradient): G += numpy.dot(g, Finv)
                I /= images["n_integration"][0]
                #if (generate_image_gradient): G /= images["n_integration"][0]
            elif (images["n_dim"] == 2):
                for k_y in xrange(images["n_integration"][1]):
                    x[1] = x0[1] - dx[1]/2 + (k_y+1./2)*dx[1]/images["n_integration"][1]
                    for k_x in xrange(images["n_integration"][0]):
                        x[0] = x0[0] - dx[0]/2 + (k_x+1./2)*dx[0]/images["n_integration"][0]
                        #print "x = "+str(x)
                        mapping.X(x, X, Finv)
                        #print "X = "+str(X)
                        #print "Finv = "+str(Finv)
                        image.I0(X, i, g)
                        #print "i = "+str(i)
                        #print "g = "+str(g)
                        I += i
                        #if (generate_image_gradient): G += numpy.dot(g, Finv)
                I /= images["n_integration"][1]*images["n_integration"][0]
                #if (generate_image_gradient):G /= images["n_integration"][1]*images["n_integration"][0]
            elif (images["n_dim"] == 3):
                for k_z in xrange(images["n_integration"][2]):
                    x[2] = x0[2] - dx[2]/2 + (k_z+1./2)*dx[2]/images["n_integration"][2]
                    for k_y in xrange(images["n_integration"][1]):
                        x[1] = x0[1] - dx[1]/2 + (k_y+1./2)*dx[1]/images["n_integration"][1]
                        for k_x in xrange(images["n_integration"][0]):
                            x[0] = x0[0] - dx[0]/2 + (k_x+1./2)*dx[0]/images["n_integration"][0]
                            mapping.X(x, X, Finv)
                            image.I0(X, i, g)
                            I += i
                            #if (generate_image_gradient): G += numpy.dot(g, Finv)
                I /= images["n_integration"][2]*images["n_integration"][1]*images["n_integration"][0]
                #if (generate_image_gradient): G /= images["n_integration"][2]*images["n_integration"][1]*images["n_integration"][0]
            else:
                assert (0), "n_dim must be \"1\", \"2\" or \"3\". Aborting."
            vtk_image_scalars.SetTuple(k_point, I)
            #if (generate_image_gradient): vtk_image_gradient.SetTuple(k_point, G)
            if (I[0] < global_min): global_min = I[0]
            if (I[0] > global_max): global_max = I[0]
        myVTK.writeImage(
            image=vtk_image,
            filename=images["folder"]+"/"+images["basename"]+"_"+str(k_frame).zfill(images["zfill"])+".vti",
            verbose=verbose-1)

    if (images["data_type"] in ("float")):
        pass
    elif (images["data_type"] in ("unsigned char", "unsigned short", "unsigned int", "unsigned long", "unsigned float", "uint8", "uint16", "uint32", "uint64", "ufloat")):
        #print "global_min = "+str(global_min)
        #print "global_max = "+str(global_max)
        shifter = vtk.vtkImageShiftScale()
        shifter.SetShift(-global_min)
        if   (images["data_type"] in ("unsigned char", "uint8")):
            shifter.SetScale(float(2**8-1)/(global_max-global_min))
            shifter.SetOutputScalarTypeToUnsignedChar()
        elif (images["data_type"] in ("unsigned short", "uint16")):
            shifter.SetScale(float(2**16-1)/(global_max-global_min))
            shifter.SetOutputScalarTypeToUnsignedShort()
        elif (images["data_type"] in ("unsigned int", "uint32")):
            shifter.SetScale(float(2**32-1)/(global_max-global_min))
            shifter.SetOutputScalarTypeToUnsignedInt()
        elif (images["data_type"] in ("unsigned long", "uint64")):
            shifter.SetScale(float(2**64-1)/(global_max-global_min))
            shifter.SetOutputScalarTypeToUnsignedLong()
        elif (images["data_type"] in ("unsigned float", "ufloat")):
            shifter.SetScale(1./(global_max-global_min))
            shifter.SetOutputScalarTypeToFloat()
        for k_frame in xrange(images["n_frames"]):
            vtk_image = myVTK.readImage(
                filename=images["folder"]+"/"+images["basename"]+"_"+str(k_frame).zfill(images["zfill"])+".vti",
                verbose=verbose-1)
            if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
                shifter.SetInputData(vtk_image)
            else:
                shifter.SetInput(vtk_image)
            shifter.Update()
            vtk_image = shifter.GetOutput()
            myVTK.writeImage(
                image=vtk_image,
                filename=images["folder"]+"/"+images["basename"]+"_"+str(k_frame).zfill(images["zfill"])+".vti",
                verbose=verbose-1)
    else:
        assert (0), "Wrong data type. Aborting."