Michailina
Michailina

Reputation: 15

How to efficient create SimpleITK image?

I have two really long std::vector m1 and m2. To create two SimpleITK images i currently do this:

sitk::PixelIDValueEnum pixelType = sitk::sitkUInt8;
sitk::PixelIDValueEnum pixelTypeFloat = sitk::sitkFloat32;

std::vector<unsigned int> imageSize{d0, d1, l_size};
sitk::Image sitk_m1( imageSize, pixelType );
sitk::Image sitk_m2( imageSize, pixelTypeFloat );

for(unsigned int i=0;i<l_size;i++)
{
    for(unsigned int j=0;j<d1;j++)
    {
        for(unsigned int k=0;k<d0;k++)
        {
             sitk_m1.SetPixelAsUInt8({j,k,i}, m1[d0*d1*i + (j*d1+k)]);
             sitk_m2.SetPixelAsFloat({j,k,i}, m2[d0*d1*i + (j*d1+k)]);
        }
    }
}

How can i do this more efficiently? This works, but takes about 10 seconds with approach.

Upvotes: 0

Views: 222

Answers (1)

Dave Chen
Dave Chen

Reputation: 2085

Use SimpleITK's ImageImportFilter.

Here's a simple example that shows how it work:

std::vector<unsigned int> imageDims{4, 4000, 4000};

std::vector<unsigned char> m1(4000*4000*4, 0);

sitk::ImportImageFilter importer;

importer.SetSize( imageDims );
importer.SetBufferAsUInt8( m1.data() );
sitk::Image sitk_m1 = importer.Execute();

Looking at your code, it looks like you're re-ordering the dimensions in your loop. My example does not do that. It simple copies the std::vector data into the Image's pixel buffer. To swap the image axes, use SimpleITK's FlipImageFilter.

Upvotes: 1

Related Questions