#include <vtkSmartPointer.h>
#include <vtkPNGReader.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkBMPReader.h>
#include <vtkImageData.h>
#include <vtkImageIterator.h>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
//测试图像:../data/lena.bmp
int main()
{
vtkSmartPointer<vtkPNGReader> reader =
vtkSmartPointer<vtkPNGReader>::New();
reader->SetFileName("test1.png");
reader->Update();
使用迭代器
//int subRegion[6] = { 0,300, 0, 300, 0, 0 };
//vtkImageIterator<unsigned char> it(reader->GetOutput(), subRegion);
//while (!it.IsAtEnd())
//{
// unsigned char *inSI = it.BeginSpan();
// unsigned char *inSIEnd = it.EndSpan();
// while (inSI != inSIEnd)
// {
// *inSI = 255 - *inSI;
// ++inSI;
// }
// it.NextSpan();
//}
//使用指针
int dims[3];
reader->GetOutput()->GetDimensions(dims);
int nbOfComp;
nbOfComp = reader->GetOutput()->GetNumberOfScalarComponents();
for (int k = 0; k<dims[2]; k++)
{
for (int j = 0; j<dims[1]; j++)
{
for (int i = 0; i<dims[0]; i++)
{
unsigned char * pixel =
(unsigned char *)(reader->GetOutput()->GetScalarPointer(i, j, k));
*pixel = 0;
*(pixel + 1) = 0;
*(pixel + 2) = 0;
}
}
}
unsigned char * pixel =
(unsigned char *)(reader->GetOutput()->GetScalarPointer(200, 200, 0));
*pixel = 255;
*(pixel + 1) = 255;
*(pixel + 2) = 255;
//显示
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->Render();
imageViewer->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
imageViewer->SetSize(640, 480);
imageViewer->GetRenderWindow()->SetWindowName("VisitImagePixelDirectlyExample");
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}