[Insight-users] Custom class image serialization?

David Doria daviddoria+itk at gmail.com
Wed Aug 12 11:13:51 EDT 2009


It is easy to construct an itk image of a custom class:

class TestClass
{
    public:
        TestClass(){}
        TestClass(const double d, const std::string &s) : MyDouble(d),
MyString(s) {}

    double MyDouble;
    std::string MyString;
};

    typedef TestClass     PixelType;
    const     unsigned int    Dimension = 2;
    typedef itk::Image< PixelType, Dimension >  ImageType;


But is there a way to read/write to a file? Clearly it would not fit in any
predefined IO class (itkMYCLASSImageIO), but couldn't serialization be used
to convert the contents into an binary array that can be vectorized and
un-vectorized into an N-D image?

When I try to write the TestClass image using the itkRawImageIO

    itk::RawImageIO<PixelType,2>::Pointer io;
    io = itk::RawImageIO<PixelType,2>::New();

    typedef  itk::ImageFileWriter< ImageType  > WriterType;
    WriterType::Pointer writer = WriterType::New();
    writer->SetFileName( "test.raw" );
    writer->SetInput(image);
    writer->SetImageIO(io);

I get:

error: 'Length' is not a member of 'TestClass'
error: no type named 'ValueType' in 'class TestClass'

I added

class TestClass
{
    public:
      typedef TestClass ValueType;

(The error went way, but I'm not sure that this is right?)

It seems like all that is left is defining Length? It seems like read and
write functions were never specified, so I feel like this isn't going to
work anyway?

With VSL from VXL, you simply have to define these functions in your class:

//: Binary save to stream.
void vsl_b_write(vsl_b_ostream & os, TestClass const & obj);
//: Binary load from stream.
void vsl_b_read(vsl_b_istream & is, TestClass & obj);
//: Print human readable summary of object to a stream
void vsl_print_summary(vcl_ostream & os, TestClass const & obj);

These functions are very easy to write as VSL provides the same functions
for all "built in" types (so in this case, we would just have to call
vsl_b_write(os, double) followed by vsl_b_write(os, string) ). Is something
like this possible in ITK?

Some compilable code (with the error) is here:
http://www.rpi.edu/~doriad/ITK_List/CustomImageIO/

Thoughts?

Thanks,

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090812/cfabf16c/attachment.htm>


More information about the Insight-users mailing list