[Insight-users] Code to write back 2D image to 3D: Didnt work

Radhika Sivaramakrishna radhika . sivaramakrishna at synarc . com
Thu, 11 Dec 2003 13:01:02 -0800


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01C3C029.71736B4C
Content-Type: multipart/alternative;
	boundary="----_=_NextPart_001_01C3C029.71736B4C"


------_=_NextPart_001_01C3C029.71736B4C
Content-Type: text/plain

Hi Luiz,
Well, I decided to try the iterator approach to write back the 2D slice into
the 3D image into a user-defined slice location.
It didn't work. I have enclosed the code with this. Could somebody take a
look and let me know where I am going wrong.
Basically, I am writing back the 2D slice (which gets extracted correctly).
I am also writing the final 3D image which should contain
the 2D slice at the correct location. What I get back is a blank 3D image.
 
Radhika
 
                                                     
-----------------------------------------------------  
Confidentiality Notice. 
This email message is for the sole use of the intended recipient(s) and may
contain confidential and privileged information. Any unauthorized review,
use, disclosure or distribution is prohibited. If you are not the intended
recipient, please contact the sender by reply email and destroy all copies
of the original message. If you are the intended recipient, please be
advised that the content of this message is subject to access, review and
disclosure by the sender's Email System Administrator.
  

------_=_NextPart_001_01C3C029.71736B4C
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable

<html xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns=3D"http://www . w3 . org/TR/REC-html40">

<head>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3DUS-ASCII">


<meta name=3DProgId content=3DWord.Document>
<meta name=3DGenerator content=3D"Microsoft Word 10">
<meta name=3DOriginator content=3D"Microsoft Word 10">
<link rel=3DFile-List href=3D"cid:filelist . xml at 01C3BFE7 . 39F95CE0">
<!--[if gte mso 9]><xml>
 <o:OfficeDocumentSettings>
  <o:DoNotRelyOnCSS/>
 </o:OfficeDocumentSettings>
</xml><![endif]--><!--[if gte mso 9]><xml>
 <w:WordDocument>
  <w:SpellingState>Clean</w:SpellingState>
  <w:GrammarState>Clean</w:GrammarState>
  <w:DocumentKind>DocumentEmail</w:DocumentKind>
  <w:EnvelopeVis/>
  <w:Compatibility>
   <w:BreakWrappedTables/>
   <w:SnapToGridInCell/>
   <w:WrapTextWithPunct/>
   <w:UseAsianBreakRules/>
  </w:Compatibility>
  <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
 </w:WordDocument>
</xml><![endif]-->
<style>
<!--
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{mso-style-parent:"";
	margin:0in;
	margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:12.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:"Times New Roman";}
a:link, span.MsoHyperlink
	{color:blue;
	text-decoration:underline;
	text-underline:single;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;
	text-decoration:underline;
	text-underline:single;}
span.EmailStyle17
	{mso-style-type:personal-compose;
	mso-style-noshow:yes;
	mso-ansi-font-size:10.0pt;
	mso-bidi-font-size:10.0pt;
	font-family:Arial;
	mso-ascii-font-family:Arial;
	mso-hansi-font-family:Arial;
	mso-bidi-font-family:Arial;
	color:windowtext;}
span.SpellE
	{mso-style-name:"";
	mso-spl-e:yes;}
span.GramE
	{mso-style-name:"";
	mso-gram-e:yes;}
@page Section1
	{size:8.5in 11.0in;
	margin:1.0in 1.25in 1.0in 1.25in;
	mso-header-margin:.5in;
	mso-footer-margin:.5in;
	mso-paper-source:0;}
div.Section1
	{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
 /* Style Definitions */=20
 table.MsoNormalTable
	{mso-style-name:"Table Normal";
	mso-tstyle-rowband-size:0;
	mso-tstyle-colband-size:0;
	mso-style-noshow:yes;
	mso-style-parent:"";
	mso-padding-alt:0in 5.4pt 0in 5.4pt;
	mso-para-margin:0in;
	mso-para-margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:10.0pt;
	font-family:"Times New Roman";}
</style>
<![endif]-->
</head>

<body lang=3DEN-US link=3Dblue vlink=3Dpurple =
style=3D'tab-interval:.5in'>

<div class=3DSection1>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>Hi <span =
class=3DSpellE>Luiz</span>,<o:p></o:p></span></font></p>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>Well, I decided to try the <span =
class=3DSpellE>iterator</span>
approach to write back the 2D slice into the 3D image into a =
user-defined slice
location.<o:p></o:p></span></font></p>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>It didn't work. I have enclosed the code with this. =
Could
somebody take a look and let me know where I am going =
wrong.<o:p></o:p></span></font></p>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>Basically, I am writing back the 2D slice (which =
gets
extracted correctly). I am also writing the final 3D image which should =
contain<o:p></o:p></span></font></p>

<p class=3DMsoNormal><span class=3DGramE><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>the</span></font></span><fo=
nt
size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;font-family:Arial'> 2D slice at
the correct location. What I get back is a blank 3D image.<o:p></o:p></s=
pan></font></p>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>Radhika<o:p></o:p></span></font></p>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

</div>

</body>

</html>

<P><FONT SIZE=3D2 FACE=3D"Arial">                                       =
              </FONT></P>

<P><FONT SIZE=3D2 =
FACE=3D"Arial">-----------------------------------------------------  =
</FONT></P>

<P><FONT SIZE=3D2 FACE=3D"Arial">Confidentiality Notice. </FONT></P>

<P><FONT SIZE=3D2 FACE=3D"Arial">This email message is for the sole use =
of the intended recipient(s) and may contain confidential and =
privileged information. Any unauthorized review, use, disclosure or =
distribution is prohibited. If you are not the intended recipient, =
please contact the sender by reply email and destroy all copies of the =
original message. If you are the intended recipient, please be advised =
that the content of this message is subject to access, review and =
disclosure by the sender's Email System Administrator.</FONT></P>

<P><FONT SIZE=3D2 FACE=3D"Arial"> </FONT> </P>

------_=_NextPart_001_01C3C029.71736B4C--

------_=_NextPart_000_01C3C029.71736B4C
Content-Type: application/octet-stream;
	name="3D2D3D.cxx"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="3D2D3D.cxx"



// ITK include files

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkExtractImageFilter.h"
#include "itkPasteImageFilter.h"

int main( int argc, char * argv[] )
{
    if( argc < 4 )
	{
		std::cerr << "Usage: " << std::endl;
		std::cerr << argv[0] << "  inputImageFile  ";
		std::cerr << " outputImageFileErosion  outputImageFileDilation";
		std::cerr << " lowerThreshold upperThreshold " << std::endl;
		return -1;
	}


	const unsigned int Dimension =3D 3;

	typedef unsigned char   PixelType;


	typedef itk::Image< PixelType,  Dimension >  ImageType;
	typedef itk::Image<PixelType,2> ImageType2D;
=09
	typedef itk::ImageRegionConstIteratorWithIndex< ImageType2D > =
ConstIteratorType2D;
	typedef itk::ImageRegionIteratorWithIndex< ImageType > IteratorType;
//	typedef itk::ImageRegionIteratorWithIndex< ImageType2D > =
IteratorType;

	// Software Guide : EndCodeSnippet

	typedef itk::ImageFileReader< ImageType  >  ReaderType;
	typedef itk::ImageFileWriter< ImageType >  WriterType;
	typedef itk::ImageFileWriter< ImageType2D >  WriterType2D;

	typedef itk::ExtractImageFilter< ImageType, ImageType2D> =
ExtractFilterType;
//	typedef itk::PasteImageFilter<ImageType,ImageType2D> =
PasteFilterType;
	 =20


  // Creation of Reader and Writer filters

  ReaderType::Pointer readervolume =3D ReaderType::New();
  WriterType::Pointer writervolume =3D WriterType::New();
  WriterType2D::Pointer writerimage =3D WriterType2D::New();




 // Creation of pointers to various filters =20

    ExtractFilterType::Pointer extracter2D =3D =
ExtractFilterType::New();
 //   PasteFilterType::Pointer paster2D =3D PasteFilterType::New();
  =20
// Reading all inputs=20
 =20
  readervolume->SetFileName( argv[1] );
  writervolume->SetFileName(argv[2]);
  readervolume->Update();
  writerimage->SetFileName(argv[3]);
  unsigned int s =3D ::atoi(argv[4]); // Reading in which slice to =
extract

// Setting an empty 3D image of same size, spacing and origin as input =
image=20

  ImageType::Pointer binimg3D =3D ImageType::New();
  =
binimg3D->SetRegions((readervolume->GetOutput())->GetRequestedRegion());=

  binimg3D->CopyInformation(readervolume->GetOutput());
  binimg3D->Allocate();

  // Setting up region in 3D image and setting size[2] =3D 0, so that a =
2D slice can be extracted

  ImageType::RegionType inputRegion =3D =
readervolume->GetOutput()->GetLargestPossibleRegion();
  ImageType::SizeType size =3D inputRegion.GetSize();
  size[2] =3D 0;
  ImageType::IndexType start =3D inputRegion.GetIndex();
  ImageType::RegionType desiredRegion;
  desiredRegion.SetSize(  size  );

=20
 // Extracting a 2D slice from 3D image and pasting back into a 3D =
image at the same slice location

    start[2] =3D s;   =20
    desiredRegion.SetIndex( start );
    extracter2D->SetExtractionRegion( desiredRegion );
    extracter2D->SetInput(readervolume->GetOutput());
    extracter2D->Update();
    ImageType2D::Pointer gs =3D extracter2D->GetOutput();
 /* paster2D->SetSourceImage(gs);
    paster2D->SetDestinationImage(binimg3D);
    paster2D->SetDestinationIndex(start);
    paster2D->SetSourceRegion(desiredRegion);=20
    writervolume->SetInput(paster2D->GetOutput()); */
    ImageType2D::RegionType newRegion=3Dgs->GetLargestPossibleRegion();
    ImageType2D::SizeType size2D;
    ImageType2D::IndexType index2D;
    size2D[0] =3D size[0];
    size2D[1] =3D size[1];
    index2D[0] =3D 0;
    index2D[1] =3D 0;
    ConstIteratorType2D it2D(gs,newRegion);
    inputRegion.SetSize(size);
    inputRegion.SetIndex(start);
    IteratorType it3D(binimg3D,desiredRegion);
    it3D.GoToBegin();
    it2D.GoToBegin();
    while (!it3D.IsAtEnd() && !it2D.IsAtEnd())
    {
	it3D.Set(it2D.Get());
	++it3D;
      ++it2D;
    }=20
  writervolume->SetInput(binimg3D);
  try=20
    {=20
    writervolume->Update();=20
    }=20
  catch( itk::ExceptionObject & err )=20
    {=20
    std::cout << "ExceptionObject caught !" << std::endl;=20
    std::cout << err << std::endl;=20
  return -1;
    }=20
 writerimage->SetInput(gs);
  try=20
    {=20
    writerimage->Update();=20
    }=20
  catch( itk::ExceptionObject & err )=20
    {=20
    std::cout << "ExceptionObject caught !" << std::endl;=20
    std::cout << err << std::endl;=20
  return -1;
    }=20

 =20

  return 0;
}


------_=_NextPart_000_01C3C029.71736B4C--