VTK notes - use the vtkImageImport class to convert memory arrays to vtkImageData

class vtkImageImport

vtkImageImportProvides the methods needed to import image data from VTK-independent data sources such as simple C arrays or third-party pipelines. Note that the VTK convention is to set the image voxel index (0,0,0) to be the bottom left corner of the image, whereas most 2D image formats use the top left corner. After the image is loaded into VTK, it can be used vtkImageFlipto correct the orientation.
Note that raw data can also be imported from Python strings instead of C arrays.
This array works only with scalar point data, not cell data.

void SetImportVoidPointer(void* ptr);

Set the pointer of the input image data;
inline the input ptr pointer into vtk, if the ptr is destroyed, the vtkImageData obtained by the GetOutput function will fail and report an error; at
this time, vtk will not make its own copy of the data, it will directly from the provided Array access data. Vtk will not delete data or modify data;
if you need vtk to copy the contents of the ptr array to a copy, you need to use the function CopyImportVoidPointer;

void CopyImportVoidPointer(void* ptr, vtkIdType size);

Imports the data and makes an internal copy of the data;
if you don't want VTK to copy the data, use SetImportVoidPointer instead (don't use both). Gives the size of the data array in bytes.

void SetImportVoidPointer(void* ptr, int save);

Set the pointer of the input image data;
save is used to control the relationship between the lifetime of ptr and the vtkImageImport class object. If you expect to delete and release the ptr memory when the vtkImageImport class object is destroyed, you can set save to 0; when it is set to 1, the vtkImageImport The ptr memory will not be deleted and released when the class object is destroyed;

void* GetImportVoidPointer() {
    
     return this->ImportVoidPointer; }

Set the scalar data type;

vtkSetMacro(DataScalarType, int);
void SetDataScalarTypeToDouble() {
    
     this->SetDataScalarType(VTK_DOUBLE); }
void SetDataScalarTypeToFloat() {
    
     this->SetDataScalarType(VTK_FLOAT); }
void SetDataScalarTypeToInt() {
    
     this->SetDataScalarType(VTK_INT); }
void SetDataScalarTypeToShort() {
    
     this->SetDataScalarType(VTK_SHORT); }
void SetDataScalarTypeToUnsignedShort() {
    
     this->SetDataScalarType(VTK_UNSIGNED_SHORT); }
void SetDataScalarTypeToUnsignedChar() {
    
     this->SetDataScalarType(VTK_UNSIGNED_CHAR); }
vtkGetMacro(DataScalarType, int);
const char* GetDataScalarTypeAsString(){
    
    
     return vtkImageScalarTypeNameMacro(this->DataScalarType);
}

Set the number of constituent channels of scalar data;

vtkSetMacro(NumberOfScalarComponents, int);
vtkGetMacro(NumberOfScalarComponents, int);

Set the three-dimensional size, pixel pitch, and data starting point;

vtkSetVector6Macro(DataExtent, int);
vtkGetVector6Macro(DataExtent, int);
void SetDataExtentToWholeExtent() {
    
     this->SetDataExtent(this->GetWholeExtent()); }
vtkSetVector3Macro(DataSpacing, double);
vtkGetVector3Macro(DataSpacing, double);  
vtkSetVector3Macro(DataOrigin, double);
vtkGetVector3Macro(DataOrigin, double);
vtkSetVector6Macro(WholeExtent, int);
vtkGetVector6Macro(WholeExtent, int);

Set the direction vector of the three-dimensional volume data, the default is a 3*3 identity matrix; (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)

vtkSetVectorMacro(DataDirection, double, 9);
vtkGetVectorMacro(DataDirection, double, 9);

sample code

#include "pch.h"
#include <vtkImageActor.h>
#include <vtkImageData.h>
#include <vtkImageImport.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkXMLImageDataWriter.h>

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
int main(){
    
    
    vtkNew<vtkNamedColors> colors;

    // Create a c-style image
    constexpr int width = 4;
    constexpr int height = 4;

    unsigned char cImage[width * height];
    unsigned char value = 0;
    for (unsigned int row = 0; row < height; ++row) {
    
    
        for (unsigned int col = 0; col < width; ++col) {
    
    
            cImage[row * width + col] = value;
            value += 10;
        }
    }

    // Convert the c-style image to a vtkImageData
    vtkNew<vtkImageImport> imageImport;
    imageImport->SetDataSpacing(1, 1, 1);
    imageImport->SetDataOrigin(0, 0, 0);
    imageImport->SetWholeExtent(0, width - 1, 0, height - 1, 0, 0);
    imageImport->SetDataExtentToWholeExtent();
    imageImport->SetDataScalarTypeToUnsignedChar();
    imageImport->SetNumberOfScalarComponents(1);
    imageImport->SetImportVoidPointer(cImage);
    imageImport->Update();

    // Create an actor
    vtkNew<vtkImageActor> actor;
    actor->SetInputData(imageImport->GetOutput());

    // Setup renderer
    vtkNew<vtkRenderer> renderer;
    renderer->AddActor(actor);
    renderer->ResetCamera();
    renderer->SetBackground(colors->GetColor3d("SaddleBrown").GetData());

    // Setup render window
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    renderWindow->SetWindowName("ImageImport");

    // Setup render window interactor
    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    vtkNew<vtkInteractorStyleImage> style;

    renderWindowInteractor->SetInteractorStyle(style);

    // Render and start interaction
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindow->Render();
    renderWindowInteractor->Initialize();

    renderWindowInteractor->Start();
    return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/liushao1031177/article/details/122224116