2

I am using Kitware.VTK toolkit to show 2d images in 3d.

I have image in byte[]. I want to display it in renderviewcontrol of Kitware.VTK using vtkImageviewer. I dont have much idea about VTK.

Is there anyother way to perform the task? Can anybody help me for this?

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Finisher001
  • 455
  • 1
  • 5
  • 18

1 Answers1

3

You can copy your byte array into the VTK world and create a vktImageData object to use it as InputConnection.

vtkUnsignedCharArray myCharArray = vtkUnsignedCharArray.New();
GCHandle myPinnedArray = GCHandle.Alloc(myByteArray, GCHandleType.Pinned);
myCharArray.SetArray(myPinnedArray.AddrOfPinnedObject(), myData.Width * myData.Height * myData.Depth, 1);

vtkImageData myVtkImageData = vtkImageData.New();
myVtkImageData.GetPointData().SetScalars(myCharArray);
myVtkImageData.SetScalarTypeToUnsignedChar();

myVtkImageData.SetExtent(0, myData.Width - 1, 0, myData.Height - 1, myData.Depth -1);

myVtkImageData.Update();

The myData is just a information about the image dimension of your byte array. You should also make sure that the GCHandle and vtkImageData object is not cleared by the GC.

Depending on your data it's probably necessary to set Origin and Spacing.

JohnnyQ
  • 1,591
  • 1
  • 16
  • 25
  • thanks JohnnyQ! It helped. I need some more help here. I have byte array of multiple images and i want to convert them into 3d vtkimage. It can be done by storing byte data as jpg or bmp images and them load these series of images as 3d in vtk but i dont want to store images instead just want to direct convert them to 3d vtkimage from arrays. – Finisher001 Aug 12 '15 at 07:03
  • You could merge your arrays into one and use it as input array, so you get a 3D volume as output. – JohnnyQ Aug 12 '15 at 08:36
  • Hi JohnnyQ, I am using kitware vtk version 5.6. SetScalarTypeToUnsignedByte() method is not available in it. By using other similar methods, I am not able to create image. I also not get, how t merge image data arrays. – Finisher001 Aug 12 '15 at 10:59
  • Corrected the code i meant `SetScalarTypeToUnsignedChar`. See [http://stackoverflow.com/questions/59217/merging-two-arrays-in-net](http://stackoverflow.com/questions/59217/merging-two-arrays-in-net) – JohnnyQ Aug 12 '15 at 11:23
  • Its still not working. Can you give full code to show image in vtk image viewer. – Finisher001 Aug 12 '15 at 11:54
  • The general gist of it. This answer should guide people to the right solution. For me, it helped to set the number of scalar components on the scalar array (not vtkImageData). – TernaryTopiary May 05 '17 at 10:59