← Back to team overview

dolfin team mailing list archive

Re: Image to Function data structure conversion

 

>
>I agree with Anders.
>
>The dof_map does of course have a very simple structure in this case, which
>can be written as an explicit formula. This is an advantage we usually don't
>have when working on unstructured meshes. Still, I think the cpu time and
>memory
>will not be dominated by the dof_map so it might be smart to wait about
>taking
>advantage of this until after profiling.
>
>Can you store the images eg as a numpy array?

Do you want to perform the profiling with numpy as a bridge between
itk and dolfin? There is an external project in wrapitk called
PyBuffer which generates the numpy interface. You must have wrappring
for double type on. I don't have access to python itk binaries right
now, but I guess the following untested script should allow you to
start such profiling. This is written for 2D images and generalisation
to nD is trivial.


-Ali


-------------------------------
from dolfin import *
from numpy import *
import itk

dim = 2
inType = itk.Image[itk.D, dim]

reader = itk.ImageFileReader[inType].New(FileName="/path/to/file.bmp")
reader.Update()

itk2numpy = itkPyBufferD2_New()
numpy_arr = itk2numpy.GetArrayFromImage( reader.GetOutput() )
shape = numpy_arr.shape

mesh = UnitSquare(shape[0]-1, shape[1]-1)

class ImageFunction(Function):
    def eval(self, value, x):
        i = int((self.A.shape[0]-1)*x[0])
        j = int((self.A.shape[1]-1)*x[1])
        print i,j
        value[0] = self.A[(i,j)]

V = FunctionSpace(mesh, "CG", 1)
f = ImageFunction(V)
f.A = numpy_arr

plot(f) # can viper plot 2d images better than this?
interactive()
--------------------------------


Follow ups