VTK系列25_BMP图像像素值的访问与修改
原创
©著作权归作者所有:来自51CTO博客作者亦我飞也的原创作品,请联系作者获取转载授权,否则将追究法律责任
实例25:BMP图像像素值的访问与修改
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkBMPReader.h>
#include <vtkImageData.h>
#include <vtkImageIterator.h>
//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkBMPReader> reader =
vtkSmartPointer<vtkBMPReader>::New();
reader->SetFileName ( "lena.bmp" );
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();
}
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("VisitImagePixelIterativelyExample");
renderWindowInteractor->Start();
return 0;
}
VTK系列目录: