正常的png读取和显示:

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkActor2D.h>
#include <vtkPNGReader.h>
#include <vtkImageActor.h>
#include <vtkImageMapper3D.h>

using namespace std;

int main()
{
    vtkSmartPointer<vtkPNGReader> reader =
        vtkSmartPointer<vtkPNGReader>::New();
    reader->SetFileName ( "/Users/weiyang/Desktop/Untitled.png" );
    reader->Update();

    vtkSmartPointer<vtkImageActor> actor =
            vtkSmartPointer<vtkImageActor>::New();
    actor->GetMapper()->SetInputConnection( reader->GetOutputPort() );

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor( actor );
    renderer->SetBackground( 0, 0, 0 );

    vtkSmartPointer<vtkRenderWindow> renderWindow =
            vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer( renderer );

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow( renderWindow );

    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();
    return 0;
}

现在使用vtkImageIterator在数据流传递给vtkImageActor之前对局部的像素值重新设定。

    // The extent of pic: 0, 495, 0, 572, 0, 0
    int subRegion[6] = { 0, 295, 0, 372, 0, 0 };    //extend, x, y, z
    vtkImageIterator< unsigned char > it( reader->GetOutput(), subRegion );
    while( !it.IsAtEnd() )
    {
        unsigned char *inSI = it.BeginSpan();
        unsigned char *inSIEnd = it.EndSpan();
        while (inSI != inSIEnd)
        {
            if( (inSIEnd - inSI)&1 )
            {
                *inSI = 255;
            }
            ++inSI;
        }
        it.NextSpan();
    }

    vtkSmartPointer<vtkImageActor> actor =
            vtkSmartPointer<vtkImageActor>::New();
    actor->GetMapper()->SetInputConnection( reader->GetOutputPort() );

得到变化后的图像:


 

分类: C plus plus

发表评论

电子邮件地址不会被公开。 必填项已用*标注