I have trouble solving the following task with VTK:
I have a couple of 2D images I obtain from OpenCV that represent slices of a 3D object. I want to stack these 2D images (in z direction) to reconstruct the 3D structure. I use vtkImageAppend for this task. I succeed with adding data to vtkAppend as input. However, instead of adding all the differing slices to my structure my output just consist of the same image stacked multiple times. For clarification, here ist my code and an example output picture. Please note that I stacked the images in Y-axis here just because it is easier to debug like this. Eventually they should be stacked in Z-axis.
vtkSmartPointer<vtkImageData> outputVtkImage =
vtkSmartPointer<vtkImageAppend> volume =
for (int a = 0; a < count; a++)
Mat img = imread(name[a]); //opencv-data
//Convert OpenCV data to vtkImageData
//Add the vtkImageData as input
int render_2d_slices(vtkSmartPointer<vtkImageAppend> volume)
vtkRenderWindowInteractor* iren =
// Create a viewer for the image
vtkImageViewer2* viewer =
viewer->SetSlice(1); //Eventually I want to scroll through slices in Z-direction here
// Initialize the event loop and then start it.
The rendered output looks like this (the program appended the same image 3 times). The photo of a arduino is just an example here.
I can confirm that outputVtkImage contains a different image every time the loop runs through. However, in the end I have the same image (it is always the last image I pass) stacked multiple times instead of the differing images I pass to AddInputData.
Thank you so much, that was a very quick solution for a problem that plagued me for days.
Just so I understand it: I was under the impression that because convertCVMatToVtkImageData changes every iteration and then the adresses to it get copied into outputVtkImage the latter would also change every iteration. Is that not how ShallowCopy works?
Now that appending worked I want to render the stack in 3D. Please tell me when I should open a new topic for this. It would be awesome if you or someone else can help me on that, too. (Note that I only set the opacity in order to see if the stack rendering worked correctly).
int render_3d(vtkSmartPointer<vtkImageAppend> volume)
// Create a mapper and actor
vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkActor> actor =
// Renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderWindow> renderWindow =
// An interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
// Begin mouse interaction
Using render_2d_slices I can see that ‘volume’ indeed has all 6 slices appended. However when I render it in 3D I can indeed see the stacked slices. However, there are only the first (the one with the ‘0’ on it) and last slice (‘6’). Again, note that I set an opacity so that all slices can be seen here.
I am not sure whether I am rendering it with the wrong functions or if there is some error in my code.
Again, thank you very much for your help.
Yes, that’s how ShallowCopy works. I think your misunderstanding is with how AddInputData() and Update() work. When you call AddInputData(), it stores the address of the container (in this case, the address of the outputVtkImage object), not the address of the contents of the container (which is what ShallowCopy() copies).
The reason you only see the first and last slice is that vtkActor renders polygons: the vtkDataSetMapper converts your image volume into polygons by extracting its surface and converting the surface to polygons. Hence, only the slices on the surface are visible.
If you want to display your volume with proper transparency, the volume mappers are probably the best way (but I’ve never used the volume mappers with RGB data, so I’m not sure which one is the best to use).