[Insight-users] Unable to display segmented images properly using itk-vtk
Yasser Bashir
itk_at_stanford at yahoo.com
Sun, 22 Feb 2004 15:49:57 -0800 (PST)
--0-1244371321-1077493797=:58942
Content-Type: multipart/alternative; boundary="0-2088191815-1077493797=:58942"
--0-2088191815-1077493797=:58942
Content-Type: text/plain; charset=us-ascii
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 - Read only the mail you want.
--0-2088191815-1077493797=:58942
Content-Type: text/html; charset=us-ascii
<DIV>
<DIV>Hi all,</DIV>
<DIV> </DIV>
<DIV>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(). </DIV>
<DIV> </DIV>
<DIV>My code follows and is also attached. If you can find an obvious issue with it, please do let me know:</DIV>
<DIV> </DIV>
<DIV><FONT color=#0000ff size=2>
<P>void</FONT><FONT size=2> visibleHuman()</P>
<P>{</P>
<P>ofstream fout("debug.txt");</P>
<P></FONT><FONT color=#0000ff size=2>const</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>unsigned</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> dim = 3; </P>
<P>string testdir = "..\\common\\data\\VisibleHuman\\Analyze\\";</P>
<P>string inputFile = "analyze.txt";</P>
<P>itksys::SystemTools::ChangeDirectory(testdir.c_str());</P>
<P>Reader::Pointer analyzeReader = Reader::New();</P>
<P>AnalyzeBinaryBitmapImageIO::Pointer Analyzeio;</P>
<P>Analyzeio = AnalyzeBinaryBitmapImageIO::New();</P>
<P>Analyzeio->SetByteOrder(itk::AnalyzeImageIO::LittleEndian);</P>
<P>analyzeReader->SetImageIO(Analyzeio); </P>
<P></P>
<P></FONT><FONT color=#008000 size=2>//Load filenames</P></FONT><FONT size=2>
<P>vector <string> files; </P>
<P>fstream fin(inputFile.c_str());</P>
<P></FONT><FONT color=#0000ff size=2>while</FONT><FONT size=2>(!fin.eof())</P>
<P>{</P>
<P></FONT><FONT color=#0000ff size=2>char</FONT><FONT size=2> str[100];</P>
<P>fin.getline(str, 79, '\n');</P>
<P></FONT><FONT color=#0000ff size=2>if</FONT><FONT size=2> (strlen(str) > 0) { </P>
<P>files.push_back(str);</P>
<P>}</P>
<P>}</P>
<P>fin.close();</P>
<P> </P>
<P></FONT><FONT color=#008000 size=2>//Initialize VTK Stuff.</P></FONT><FONT size=2>
<P>vtkRenderer* ren = vtkRenderer::New();</P>
<P>vtkRenderWindow* renwin = vtkRenderWindow::New();</P>
<P>vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();</P>
<P>renwin->AddRenderer( ren ); </P>
<P>ren->SetBackground(0.4392,0.5020,0.5647);</P>
<P>vtkProgressCallback * cbc;</P>
<P>cbc = vtkProgressCallback::New();</P>
<P>renwin->SetSize(350,350);</P>
<P>iren->SetRenderWindow(renwin); </P>
<P>iren->GetInteractorStyle()->GetInteractor()->AddObserver(vtkCommand::EndPickEvent,cbc);</P>
<P>cbc->iren = iren;</P>
<P></P>
<P></FONT><FONT color=#0000ff size=2>float</FONT><FONT size=2> factor;</P>
<P>cout << "\nImage Shrinking Factor? "; </P>
<P>factor = 4; </P>
<P>cin >> factor;</P>
<P>cout << factor;</P>
<P>vtkClipVolume* clip[15];</P>
<P>vtkDataSetMapper* mapper1[15];</P>
<P>vtkTransformFilter * transFilter[16];</P>
<P>vtkActor* actors[20];</P>
<P></FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> k;</P>
<P> </P>
<P></FONT><FONT color=#0000ff size=2>for</FONT><FONT size=2>(k =0; k < files.size();++k)</P>
<P>{</P>
<P></FONT><FONT color=#008000 size=2>//Read the segmented image file in analyze format</P></FONT><FONT size=2>
<P>string filename = "DS101" + files[k] + ".hdr";</P>
<P>analyzeReader->SetFileName(filename.c_str()); </P>
<P></FONT><FONT color=#008000 size=2>//Read additional header info including offsets of the segmented file</P></FONT><FONT size=2>
<P>string kfffilename = "..\\kff\\" + files[k] + "01697.kff";</P>
<P>KffFileReader kffReader(kfffilename.c_str());</P>
<P>KnowledgeFrame kff = kffReader.GetOutput();</P>
<P></FONT><FONT color=#0000ff size=2>try</P></FONT><FONT size=2>
<P>{</P>
<P>analyzeReader->Update(); </P>
<P>}</P>
<P></FONT><FONT color=#0000ff size=2>catch</FONT><FONT size=2>(itk::ExceptionObject & err) </P>
<P>{</P>
<P>std::cout << "Exception: " << std::endl;</P>
<P>std::cout << err << std::endl;</P>
<P></FONT><FONT color=#0000ff size=2>continue</FONT><FONT size=2>;</P>
<P>}</P>
<P></FONT><FONT color=#0000ff size=2>typedef</FONT><FONT size=2> itk::ShrinkImageFilter <ImageType,RealImageType> ShrinkFilterType;</P>
<P>ShrinkFilterType::Pointer pShrinkFilter = ShrinkFilterType::New();</P>
<P>analyzeReader->GetOutput()->Print(fout);</P>
<P></P>
<P></FONT><FONT color=#0000ff size=2>float</FONT><FONT size=2> spacing[]={0.144,0.144,1};</P>
<P></FONT><FONT color=#0000ff size=2>double</FONT><FONT size=2> origin[ImageType::ImageDimension];</P>
<P>origin[0] = kff.m_volumeOfInterest.m_VoiBoundaryX;</P>
<P>origin[1] = kff.m_volumeOfInterest.m_VoiBoundaryY;</P>
<P>origin[2] = kff.m_volumeOfInterest.m_VoiBoundaryZ;</P>
<P>analyzeReader->GetOutput()->SetOrigin(origin);</P>
<P>analyzeReader->GetOutput()->SetSpacing(spacing);</P>
<P>pShrinkFilter->SetInput(analyzeReader->GetOutput());</P>
<P>pShrinkFilter->SetNumberOfThreads(4); </P>
<P></FONT><FONT color=#0000ff size=2>unsigned</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> dfactors[3] = { factor, factor, factor };</P>
<P>pShrinkFilter->SetShrinkFactors(dfactors);</P>
<P>pShrinkFilter->UpdateLargestPossibleRegion();</P>
<P></P>
<P></FONT><FONT color=#008000 size=2>//Give to VTK for rendering</P></FONT><FONT size=2>
<P></FONT><FONT color=#0000ff size=2>typedef</FONT><FONT size=2> itk::VTKImageExport< RealImageType > VTKExportType; </P>
<P>VTKExportType::Pointer exporter = VTKExportType::New();</P>
<P>exporter->SetInput( pShrinkFilter->GetOutput() );</P>
<P>vtkImageImport* importer = vtkImageImport::New();</P>
<P>ConnectPipelines(exporter, importer);</P>
<P>importer->Update();</P>
<P>vtkImageData* image = importer->GetOutput();</P>
<P> </P>
<P> </P>
<P></FONT><FONT color=#008000 size=2>//VTK convert image to unstructured grid i.e. tetrahedrons.</P></FONT><FONT size=2>
<P>cout << "\nClipping Volume"; </P>
<P>clip[k] = vtkClipVolume::New();</P>
<P>clip[k]->SetInput(image); </P>
<P>clip[k]->SetValue(254);</P>
<P>cout << "\nMapping Data";</P>
<P>mapper1[k] = vtkDataSetMapper::New(); </P>
<P>mapper1[k]->SetInput(</FONT><FONT color=#0000ff size=2>dynamic_cast</FONT><FONT size=2><vtkDataSet*>(clip[k]->GetOutput())); </P>
<P>mapper1[k]->ScalarVisibilityOff();</P>
<P></FONT><FONT color=#008000 size=2>//vtkActor* actor = vtkGenerateActorFromImageData(0,1,importer->GetOutput());</P></FONT><FONT size=2>
<P>actors[k] = vtkActor::New();</P>
<P>actors[k]->SetMapper(mapper1[k]); </P>
<P>vtkTransform* transform = vtkTransform::New();</P>
<P>cbc->ren = ren;</P>
<P>cbc->renwin = renwin;</P>
<P>actors[k]->GetProperty()->SetRepresentationToWireframe();</P>
<P>ren->AddActor( actors[k]); </P>
<P>pShrinkFilter->Delete(); </P>
<P>}</P>
<P>renwin->Render();</P>
<P>iren->Start(); </P>
<P></FONT><FONT color=#008000 size=2>// Clean up after VTK</P></FONT><FONT size=2>
<P>iren->Delete();</P>
<P>renwin->Delete();</P>
<P>ren->Delete(); </P>
<P>fout.close();</P>
<P>}</P></FONT></DIV></DIV><p><hr SIZE=1>
Do you Yahoo!?<br>
<a href="http://us.rd.yahoo.com/mailtag_us/*http://antispam.yahoo.com/tools?tool=1">Yahoo! Mail SpamGuard</a> - Read only the mail you want.</a>
--0-2088191815-1077493797=:58942--
--0-1244371321-1077493797=:58942
Content-Type: text/plain; name="source1.cpp"
Content-Description: source1.cpp
Content-Disposition: inline; filename="source1.cpp"
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();
}
--0-1244371321-1077493797=:58942--