今天看到有人在vtk maillist中询问一个vtkImageReader的问题,正好拿它的程序过来玩玩,修改了一些地方,现在程序可以打开任意jpeg图片并显示出来了。
代码如下:
#define EXAMPLE_DATA "F:/photo/050529_cats/DSCN9150.JPG"
int main(int argc, char **argv)
{
float origin[3] = {0, 0, 0};
int extent[6] = {0, 900, 0, 900, 0, 2};
vtkJPEGReader *m_reader = vtkJPEGReader::New();
m_reader->SetDataByteOrderToLittleEndian();
m_reader->SetDataScalarTypeToUnsignedChar();
m_reader->SetFileDimensionality(3);
m_reader->SetDataExtent(extent);
m_reader->SetDataOrigin(origin);
m_reader->SetFileName(EXAMPLE_DATA);
m_reader->SetNumberOfScalarComponents(1);
m_reader->SetHeaderSize(0);
m_reader->UpdateWholeExtent();
vtkImageActor *m_Actor = vtkImageActor::New();
m_Actor->SetInput(m_reader->GetOutput());
m_Actor->SetDisplayExtent(extent[0], extent[1], extent[2], extent[3], 1, 1);
vtkRenderer *m_Renderer = vtkRenderer::New();
vtkRenderWindow *m_RenWin = vtkRenderWindow::New();
m_RenWin->AddRenderer(m_Renderer);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(m_RenWin);
vtkInteractorStyleImage *interactor = vtkInteractorStyleImage::New();
iren->SetInteractorStyle(interactor);
m_Renderer->AddActor(m_Actor);
m_Renderer->SetBackground(0.1, 0.2, 0.4);
m_RenWin->SetSize(400, 400);
m_RenWin->Render();
m_reader->Modified();
m_RenWin->Render();
iren->Initialize();
iren->Start();
return 0;
}
按住CTRL键,并按下鼠标左键然后在图上拖动就可以旋转图片了,vtkImageActor似乎只能显示屏面图片,不知道要将图片显示成3d的怎么做,可能还是要用贴图方式的吧。