[Insight-users] Unable to display segmented images properly using itk-vtk

Luis Ibanez luis.ibanez at kitware.com
Sun, 22 Feb 2004 19:24:57 -0500


Hi Yasser,

You are attempting to change the origin of the slices
by calling SetOrigin() on the output of the reader.

This violates the ownership of the image by the reader.
You should only invoke const methods on the GetOutput()
of ITK filters.

The correct way of changing the origion and/or spacing of
the image is to use the ChangeInformationImageFilter
http://www.itk.org/Insight/Doxygen/html/classitk_1_1ChangeInformationImageFilter.html

You connect this filter at the output of the reader, and use it
for changing the origin of the images.

---

For the long term, a better alternative is to actually correct
the origin on the images in the files  that you are reading.

You could for example use the MetaImage headers and change the
"Offset" inside the header in order to align the images.
In that way, the data in the files will actually reflect the
results of the alignment that you already computed.

We are just doing a similar process for aligninig images
taken from electron microscopy.


Please let us know if you have further questions.


  Thanks

       Luis


-------------------------
Yasser Bashir wrote:

> Hi all,
>  
> I have the segmented visible human data in Analyze format.  In a 
> separate file type called knowledgeframe file, i have the offsets for 
> each of the segmented volumes.  I am trying to load the data in ITK 
> and use the SetOrigin method to set the offset for each of the 
> segmented volume.  Then i connect ITK and VTK and try to display the 
> images.  Even though each of the segmented images shows up correctly, 
> its world coordinates are not correct which i expect to be correct 
> after i do a setorigin().
>  
> My code follows and is also attached.  If you can find an obvious 
> issue with it, please do let me know:
>  
>
> void visibleHuman()
>
> {
>
> ofstream fout("debug.txt");
>
> const unsigned int dim = 3;
>
> string testdir = "..\\common\\data\\VisibleHuman\\Analyze\\";
>
> string inputFile = "analyze.txt";
>
> itksys::SystemTools::ChangeDirectory(testdir.c_str());
>
> Reader::Pointer analyzeReader = Reader::New();
>
> AnalyzeBinaryBitmapImageIO::Pointer Analyzeio;
>
> Analyzeio = AnalyzeBinaryBitmapImageIO::New();
>
> Analyzeio->SetByteOrder(itk::AnalyzeImageIO::LittleEndian);
>
> analyzeReader->SetImageIO(Analyzeio);
>
> //Load filenames
>
> vector <string> files;
>
> fstream fin(inputFile.c_str());
>
> while(!fin.eof())
>
> {
>
> char str[100];
>
> fin.getline(str, 79, '\n');
>
> if (strlen(str) > 0) {
>
> files.push_back(str);
>
> }
>
> }
>
> fin.close();
>
>  
>
> //Initialize VTK Stuff.
>
> vtkRenderer* ren = vtkRenderer::New();
>
> vtkRenderWindow* renwin = vtkRenderWindow::New();
>
> vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
>
> renwin->AddRenderer( ren );
>
> ren->SetBackground(0.4392,0.5020,0.5647);
>
> vtkProgressCallback * cbc;
>
> cbc = vtkProgressCallback::New();
>
> renwin->SetSize(350,350);
>
> iren->SetRenderWindow(renwin);
>
> iren->GetInteractorStyle()->GetInteractor()->AddObserver(vtkCommand::EndPickEvent,cbc);
>
> cbc->iren = iren;
>
> float factor;
>
> cout << "\nImage Shrinking Factor? ";
>
> factor = 4;
>
> cin >> factor;
>
> cout << factor;
>
> vtkClipVolume* clip[15];
>
> vtkDataSetMapper* mapper1[15];
>
> vtkTransformFilter * transFilter[16];
>
> vtkActor* actors[20];
>
> int k;
>
>  
>
> for(k =0; k < files.size();++k)
>
> {
>
> //Read the segmented image file in analyze format
>
> string filename = "DS101" + files[k] + ".hdr";
>
> analyzeReader->SetFileName(filename.c_str());
>
> //Read additional header info including offsets of the segmented file
>
> string kfffilename = "..\\kff\\" + files[k] + "01697.kff";
>
> KffFileReader kffReader(kfffilename.c_str());
>
> KnowledgeFrame kff = kffReader.GetOutput();
>
> try
>
> {
>
> analyzeReader->Update();
>
> }
>
> catch(itk::ExceptionObject & err)
>
> {
>
> std::cout << "Exception: " << std::endl;
>
> std::cout << err << std::endl;
>
> continue;
>
> }
>
> typedef itk::ShrinkImageFilter <ImageType,RealImageType> ShrinkFilterType;
>
> ShrinkFilterType::Pointer pShrinkFilter = ShrinkFilterType::New();
>
> analyzeReader->GetOutput()->Print(fout);
>
> float spacing[]={0.144,0.144,1};
>
> double origin[ImageType::ImageDimension];
>
> origin[0] = kff.m_volumeOfInterest.m_VoiBoundaryX;
>
> origin[1] = kff.m_volumeOfInterest.m_VoiBoundaryY;
>
> origin[2] = kff.m_volumeOfInterest.m_VoiBoundaryZ;
>
> analyzeReader->GetOutput()->SetOrigin(origin);
>
> analyzeReader->GetOutput()->SetSpacing(spacing);
>
> pShrinkFilter->SetInput(analyzeReader->GetOutput());
>
> pShrinkFilter->SetNumberOfThreads(4);
>
> unsigned int dfactors[3] = { factor, factor, factor };
>
> pShrinkFilter->SetShrinkFactors(dfactors);
>
> pShrinkFilter->UpdateLargestPossibleRegion();
>
> //Give to VTK for rendering
>
> typedef itk::VTKImageExport< RealImageType > VTKExportType;
>
> VTKExportType::Pointer exporter = VTKExportType::New();
>
> exporter->SetInput( pShrinkFilter->GetOutput() );
>
> vtkImageImport* importer = vtkImageImport::New();
>
> ConnectPipelines(exporter, importer);
>
> importer->Update();
>
> vtkImageData* image = importer->GetOutput();
>
>  
>
>  
>
> //VTK convert image to unstructured grid i.e. tetrahedrons.
>
> cout << "\nClipping Volume";
>
> clip[k] = vtkClipVolume::New();
>
> clip[k]->SetInput(image);
>
> clip[k]->SetValue(254);
>
> cout << "\nMapping Data";
>
> mapper1[k] = vtkDataSetMapper::New();
>
> mapper1[k]->SetInput(dynamic_cast<vtkDataSet*>(clip[k]->GetOutput()));
>
> mapper1[k]->ScalarVisibilityOff();
>
> //vtkActor* actor = 
> vtkGenerateActorFromImageData(0,1,importer->GetOutput());
>
> actors[k] = vtkActor::New();
>
> actors[k]->SetMapper(mapper1[k]);
>
> vtkTransform* transform = vtkTransform::New();
>
> cbc->ren = ren;
>
> cbc->renwin = renwin;
>
> actors[k]->GetProperty()->SetRepresentationToWireframe();
>
> ren->AddActor( actors[k]);
>
> pShrinkFilter->Delete();
>
> }
>
> renwin->Render();
>
> iren->Start();
>
> // Clean up after VTK
>
> iren->Delete();
>
> renwin->Delete();
>
> ren->Delete();
>
> fout.close();
>
> }
>
> ------------------------------------------------------------------------
> Do you Yahoo!?
> Yahoo! Mail SpamGuard 
> <http://us.rd.yahoo.com/mailtag_us/*http://antispam.yahoo.com/tools?tool=1> 
> - Read only the mail you want.
>
>------------------------------------------------------------------------
>
>
>
>
>
>void visibleHuman()
>{
>
>	ofstream fout("debug.txt");
>	const unsigned int dim = 3;	
>	string testdir = "..\\common\\data\\VisibleHuman\\Analyze\\";
>	string inputFile = "analyze.txt";
>
>	itksys::SystemTools::ChangeDirectory(testdir.c_str());
>	Reader::Pointer analyzeReader = Reader::New();
>	AnalyzeBinaryBitmapImageIO::Pointer Analyzeio;
>	Analyzeio = AnalyzeBinaryBitmapImageIO::New();
>	Analyzeio->SetByteOrder(itk::AnalyzeImageIO::LittleEndian);
>	analyzeReader->SetImageIO(Analyzeio);		
>	
>	//Load filenames
>	vector <string> files;	
>	fstream fin(inputFile.c_str());
>	while(!fin.eof())
>	{
>		char str[100];
>		fin.getline(str, 79, '\n');
>		if (strlen(str) > 0) {			
>			files.push_back(str);
>		}
>	}
>	fin.close();
>
>
>	//Initialize VTK Stuff.
>	vtkRenderer* ren = vtkRenderer::New();
>	vtkRenderWindow* renwin = vtkRenderWindow::New();
>	vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
>	renwin->AddRenderer( ren );	
>	ren->SetBackground(0.4392,0.5020,0.5647);
>	vtkProgressCallback * cbc;
>	cbc = vtkProgressCallback::New();
>	renwin->SetSize(350,350);
>	iren->SetRenderWindow(renwin);	
>	iren->GetInteractorStyle()->GetInteractor()->AddObserver(vtkCommand::EndPickEvent,cbc);
>	cbc->iren = iren;
>	
>
>	float factor;
>	cout << "\nImage Shrinking Factor? ";	
>	factor = 4;	
>	cin >> factor;
>	cout << factor;
>	vtkClipVolume* clip[15];
>	vtkDataSetMapper* mapper1[15];
>	vtkTransformFilter * transFilter[16];
>	vtkActor* actors[20];
>	int k;
>
>
>	for(k =0; k < files.size();++k)
>	{
>		//Read the segmented image file in analyze format
>		string filename = "DS101" + files[k] + ".hdr";
>		analyzeReader->SetFileName(filename.c_str());			
>
>		//Read additional header info including offsets of the segmented file
>		string kfffilename = "..\\kff\\" + files[k] + "01697.kff";
>		KffFileReader kffReader(kfffilename.c_str());
>		KnowledgeFrame kff = kffReader.GetOutput();
>		try
>		{
>			analyzeReader->Update();					
>		}
>		catch(itk::ExceptionObject & err) 
>		{
>			std::cout << "Exception: " << std::endl;
>			std::cout << err << std::endl;
>			continue;
>		}
>
>		typedef itk::ShrinkImageFilter  <ImageType,RealImageType> ShrinkFilterType;
>		ShrinkFilterType::Pointer pShrinkFilter = ShrinkFilterType::New();
>		analyzeReader->GetOutput()->Print(fout);
>		
>		float spacing[]={0.144,0.144,1};
>		double origin[ImageType::ImageDimension];
>		origin[0] = kff.m_volumeOfInterest.m_VoiBoundaryX;
>		origin[1] = kff.m_volumeOfInterest.m_VoiBoundaryY;
>		origin[2] = kff.m_volumeOfInterest.m_VoiBoundaryZ;
>
>		analyzeReader->GetOutput()->SetOrigin(origin);
>		analyzeReader->GetOutput()->SetSpacing(spacing);
>		pShrinkFilter->SetInput(analyzeReader->GetOutput());
>		pShrinkFilter->SetNumberOfThreads(4);		
>
>		unsigned int dfactors[3] = { factor, factor, factor };
>		pShrinkFilter->SetShrinkFactors(dfactors);
>		pShrinkFilter->UpdateLargestPossibleRegion();
>		
>
>		//Give to VTK for rendering
>		typedef itk::VTKImageExport< RealImageType >  VTKExportType;		
>		VTKExportType::Pointer exporter = VTKExportType::New();
>		exporter->SetInput( pShrinkFilter->GetOutput() );
>
>		vtkImageImport* importer = vtkImageImport::New();
>		ConnectPipelines(exporter, importer);
>		importer->Update();
>
>		vtkImageData* image = importer->GetOutput();
>
>
>
>		//VTK convert image to unstructured grid i.e. tetrahedrons.
>		cout << "\nClipping Volume";		
>		clip[k] = vtkClipVolume::New();
>		clip[k]->SetInput(image);						
>		clip[k]->SetValue(254);
>
>		cout << "\nMapping Data";
>		mapper1[k] = vtkDataSetMapper::New();		
>		mapper1[k]->SetInput(dynamic_cast<vtkDataSet*>(clip[k]->GetOutput()));		
>		mapper1[k]->ScalarVisibilityOff();
>
>		//vtkActor* actor = vtkGenerateActorFromImageData(0,1,importer->GetOutput());
>		actors[k] = vtkActor::New();
>		actors[k]->SetMapper(mapper1[k]);		
>		vtkTransform* transform = vtkTransform::New();
>
>		cbc->ren = ren;
>		cbc->renwin = renwin;
>
>		actors[k]->GetProperty()->SetRepresentationToWireframe();
>
>		ren->AddActor(	actors[k]);		
>		pShrinkFilter->Delete();		
>	}
>	renwin->Render();
>	iren->Start();		
>
>	// Clean up after VTK
>	iren->Delete();
>	renwin->Delete();
>	ren->Delete();	
>	fout.close();
>
>}
>