# VTK volume from std::vector and opencv images

**URL:** https://discourse.vtk.org/t/vtk-volume-from-std-vector-and-opencv-images/9369
**Category:** Support
**Created:** [September 16, 2022, 1:25pm UTC](https://discourse.vtk.org/t/vtk-volume-from-std-vector-and-opencv-images/9369 "2022-09-16T13:25:38Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![alex1610](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/e47774/32.png) [@alex1610](https://discourse.vtk.org/u/alex1610)
#### Post date: [September 16, 2022, 1:25pm UTC](https://discourse.vtk.org/t/vtk-volume-from-std-vector-and-opencv-images/9369/1 "2022-09-16T13:25:38Z")

</div>

I would like to create a volume from an std::vector of cv::Mat. I save all values in an std::vector in a linear way at first and then I import the pointer to data with imageimport. My problem is that my images are truncated. Here is my code:

//std::vectorcv::Mat volume; // was allocated with some data  
const int width = volume[0].cols;  
const int height = volume[0].rows;  
const int depth = (int)volume.size();  
std::vector volume\_cv\_mat;

for (unsigned int z = 0; z \< depth; ++z)  
{  
for (unsigned int row = 0; row \< height; ++row)  
{  
for (unsigned int col = 0; col \<width ; ++col)  
{  
volume\_cv\_mat.push\_back(volume[z].at(height-row, col));  
}  
}  
}  
vtkSmartPointer imageImport = vtkSmartPointer::New();  
imageImport-\>SetImportVoidPointer(volume\_cv\_mat.data(),volume\_cv\_mat.size());  
imageImport-\>SetDataScalarTypeToUnsignedChar();  
imageImport-\>SetNumberOfScalarComponents(1);  
imageImport-\>SetDataSpacing(0.12, 0.15, 1);  
imageImport-\>SetDataOrigin(0, 0, 0);  
imageImport-\>SetWholeExtent(0, width - 1, 0, height - 1, 0, depth - 1);  
imageImport-\>SetDataExtentToWholeExtent();  
imageImport-\>Update();  
vtkRenderer \*ren = vtkRenderer::New();  
vtkRenderWindow \*renWin = vtkRenderWindow::New();  
renWin-\>AddRenderer(ren);  
vtkRenderWindowInteractor \*iren = vtkRenderWindowInteractor::New();  
iren-\>SetRenderWindow(renWin);  
// Create transfer mapping scalar value to opacity  
vtkPiecewiseFunction \*opacityTransferFunction = vtkPiecewiseFunction::New();  
opacityTransferFunction-\>AddPoint(0.0, 1.0);  
opacityTransferFunction-\>AddPoint(255.0, 1.0);

// Create transfer mapping scalar value to color  
vtkColorTransferFunction \*colorTransferFunction = vtkColorTransferFunction::New();  
colorTransferFunction-\>AddRGBPoint(0.0, 0.0, 0.0, 0.0);  
colorTransferFunction-\>AddRGBPoint(255.0, 1.0, 1.0, 1.0);

// The property describes how the data will look  
vtkNew volumeProperty;  
volumeProperty-\>SetColor(colorTransferFunction);  
volumeProperty-\>SetScalarOpacity(opacityTransferFunction);  
volumeProperty-\>SetInterpolationTypeToLinear();  
volumeProperty-\>ShadeOn();  
volumeProperty-\>SetAmbient(0.4);  
volumeProperty-\>SetDiffuse(0.6);  
volumeProperty-\>SetSpecular(0.2);

// The mapper / ray cast function know how to render the data  
vtkGPUVolumeRayCastMapper \*volumeMapper = vtkGPUVolumeRayCastMapper::New();  
volumeMapper-\>SetInputConnection(imageImport-\>GetOutputPort());

// The volume holds the mapper and the property and  
// can be used to position/orient the volume  
vtkVolume \*volumeVTK = vtkVolume::New();  
volumeVTK-\>SetMapper(volumeMapper);  
volumeVTK-\>SetProperty(volumeProperty);

ren-\>AddViewProp(volumeVTK);  
ren-\>SetBackground(1, 1, 1);  
renWin-\>SetSize(1000, 1000);  
iren-\>Initialize();  
renWin-\>Render();  
iren-\>Start();

Here you can see my question in stackoverflow [stdvector - VTK volume from std::vector and opencv images - Stack Overflow](https://stackoverflow.com/questions/73745122/vtk-volume-from-stdvector-and-opencv-images)  
My problem is, why is my image truncated in my volume, but all voxel values are given?  
Thank you

---

<div class="post-metadata">

### Author: ![alex1610](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/e47774/32.png) [@alex1610](https://discourse.vtk.org/u/alex1610)
#### Post date: [September 20, 2022, 1:41pm UTC](https://discourse.vtk.org/t/vtk-volume-from-std-vector-and-opencv-images/9369/2 "2022-09-20T13:41:15Z")

</div>

Ok the problem was with the opencv mat that has 3 channels, which means, each pixel value was tripled. That is why my picture was not entirely shown.
