实例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系列25_BMP图像像素值的访问与修改_图像处理

VTK系列25_BMP图像像素值的访问与修改_VTK_02

VTK系列目录:

1 VTK基本概念

2 VTK图像处理

3 VTK图形处理

4 VTK体绘制