[ITK-users] Writing from an external buffer to VectorImage

Matt McCormick matt.mccormick at kitware.com
Wed Jun 28 12:25:46 EDT 2017


Hi Gordian,

Examining or using the code in ITKGPUCommon may be helpful. The
methods transfer data from CPU to GPU memory and back.

  https://github.com/InsightSoftwareConsortium/ITK/tree/master/Modules/Core/GPUCommon

Hope this helps,
Matt

On Wed, Jun 28, 2017 at 9:51 AM, Kabelitz, Gordian
<Gordian.Kabelitz at medma.uni-heidelberg.de> wrote:
> Hello,
>
> i computed a gradient with my own function and as a result a pointer to an image buffer is provided. I know the size, origin and spacing of the gradient component image.
> I want to copy the gradient image into an itk::VectorImage with the components for the x,y,z gradients.
>
> The way I copied the image to the GPU is that I retrieved the buffer pointer from my input image and use the pointer to copy the image data to the GPU.
> I used the way proposed in [1]. The computeGradientImage method is listed at the end of this mail.
>
> [...]
> // get float pointer to image data
> ImageType::Pointer image = reader->GetOutput();
> Image->Update();
>
> float* data = image.GetBufferPointer();
> // copy image data to GPU texture memory (this works)
> gpu_dev->setVoxels(dimension, voxelSize, data);
> [...]
> computeGradientImage<<<n,m>>> (dev_gradientImage, dimension);
>
> // copy resulting gradientImage to host variable
> float4* host_gradientImage;
> cudaMemcpy(host_gradient, dev_gradientImage, numberOfVoxels*sizeof(float4));
>
> --> Pseudo Code <--
> // Now I want to reverse the copy process. I have a float4 image and want to copy this into a itk::VectorImage with VariableVectorLength of 3 (skipping the magnitude value).
> [...] -> size, spacing, origin, region definition
> Itk::VectorImageType vecImage = VectorImageType::New();
> vecImage->setRegion(region);
> vecImage ->SetVectorLength(3);
> vecImage->Allocate();
>
> // copy image buffer to vecImage, component by component
> auto vecBuffer = vecImage->getBufferPointer();
> auto j = 0;
> for (i=0; i<totalVoxel; ++i)
> {
>         vecbuffer[j] = host_gradient[i].x; j++;
>         vecbuffer[j] = host_gradient[i].y; j++;
>         vecbuffer[j] = host_gradient[i].z; j++;
> }
>
> // save vecImage as nrrd image
> [...]
>
> I haven't found a way to achieve my idea.
> Are there any suggestions or examples?
> As far I can see I cannot use the itk::ImportImageFilter.
>
> Thank you for any suggestions.
> With kind regards,
> Gordian
>
> [1]: https://itk.org/CourseWare/Training/GettingStarted-V.pdf
>
> void computeGradientImage(float4* gradientImage, int* dimension)
> {
>         // every thread computes the float4 voxel with theta,phi,magnitude from gradient image
>         int idx = blockIdx.x * blockDim.x + threadIdx.x;
>         int idy = blockIdx.y * blockDim.y + threadIdx.y;
>         int idz = blockIdx.z * blockDim.z + threadIdx.z;
>
>         if (idx < dimension[0] && idy < dimension[1] && idz < dimension[2])
>         {
>                 // define sobel filter for each direction
>                 [...]
>
>                 // run sobel on image in texture memory for each direction and put result into a float4 image
>                 gradientImage[idx + dimension[0] * (idy + idz * dimension[1])] = make_float4(sobelX, sobelY, sobelZ, magn);
>         }
> }
>
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> 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://public.kitware.com/mailman/listinfo/insight-users


More information about the Insight-users mailing list