我正在使用可以从dicom文件创建冠状,矢状和轴向图像的代码,但是与此同时,我不需要向用户显示renderWindow,这是我的代码:
int main(int argc, char* argv[])
{
// Verify input arguments
if ( argc != 2 )
{
std::cout << "Usage: " << argv[0]
<< " FolderName" << std::endl;
return EXIT_FAILURE;
}
std::string folder = argv[1];
//std::string folder = "C:\\VTK\\vtkdata-5.8.0\\Data\\DicomTestImages";
// Read all the DICOM files in the specified directory.
vtkSmartPointer<vtkDICOMImageReader> reader =
vtkSmartPointer<vtkDICOMImageReader>::New();
reader->SetDirectoryName(folder.c_str());
reader->Update();
// Visualize
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(reader->GetOutputPort());
imageViewer->SetSlice(0);
imageViewer->Render();
for(int k = 0; k < 3; k++){
cout<< k << endl;
if(k == 0){
imageViewer->SetSliceOrientationToYZ();
}else if(k == 1){
imageViewer->SetSliceOrientationToXZ();
}else{
imageViewer->SetSliceOrientationToXY();
}
int _MinSlice = imageViewer->GetSliceMin();
int _MaxSlice = imageViewer->GetSliceMax();
// Screenshot
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
vtkSmartPointer<vtkWindowToImageFilter>::New();
vtkSmartPointer<vtkPNGWriter> writer =
vtkSmartPointer<vtkPNGWriter>::New();
for (int i = _MinSlice; i < _MaxSlice; i++){
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
vtkSmartPointer<vtkWindowToImageFilter>::New();
vtkSmartPointer<vtkPNGWriter> writer =
vtkSmartPointer<vtkPNGWriter>::New();
windowToImageFilter->SetInput(imageViewer->GetRenderWindow());
windowToImageFilter->ReadFrontBufferOff(); // read from the back buffer
windowToImageFilter->Update();
std::string filename = "img/" + std::to_string(k) + "/" + std::to_string(i) + ".png";
char *y = new char[filename.length() + 1];
std::strcpy(y, filename.c_str());
writer->SetFileName(y);
writer->SetInputConnection(windowToImageFilter->GetOutputPort());
writer->Write();
imageViewer->SetSlice(i);
imageViewer->Render();
}
}
return EXIT_SUCCESS;
}
我的问题是,当尝试
imageViewer->SetRenderWindow(NULL);
或imageViewer->GetRenderWindow()->Delete();
时,它将删除renderWindow实例,并且代码将被破坏,因此如何在不向用户显示的情况下保持renderWindow在后台工作?谢谢
最佳答案
您可以使用SetShowWindow(bool)或ShowWindowOff(),这些方法是从vtkWindow继承的。这样做不应停止渲染管道(您可能仍可以使用windowToImageFilter),但是我没有对其进行测试。让我们知道它是否有效。