[Insight-users] Custom class image serialization?

Luis Ibanez luis.ibanez at kitware.com
Sat Aug 15 20:55:02 EDT 2009


Hi David,

The approach that you are taking for serialization will not be
very portable. It probably will only work for reading images
that you save in your exact same computer.


Your pixel type "TestClass" is particularly difficult for serializing
an image, given that your string component will probably have
a different size on every pixel ....   (does that happen ?).


You may have to write your own customized ImageIO class.


   Regards,


         Luis


-------------------------------------------------------------------------------------
On Wed, Aug 12, 2009 at 11:13 AM, David Doria
<daviddoria+itk at gmail.com<daviddoria%2Bitk at gmail.com>
> wrote:

> 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/<http://www.rpi.edu/%7Edoriad/ITK_List/CustomImageIO/>
>
> Thoughts?
>
> Thanks,
>
> David
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090815/08ac7a51/attachment.htm>


More information about the Insight-users mailing list