[Insight-users] Problems with image corruption

Radhika Sivaramakrishna Radhika Sivaramakrishna" <radshashi at earthlink . net
Wed, 17 Dec 2003 09:39:53 -0800


This is a multi-part message in MIME format.

------=_NextPart_000_0037_01C3C481.B924D550
Content-Type: multipart/alternative;
	boundary="----=_NextPart_001_0038_01C3C481.B924D550"


------=_NextPart_001_0038_01C3C481.B924D550
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi Luis,
I am having some problems with my program. I have enclosed a portion of =
my code here where the problem is. I am not able to figure it out. =
Basically, "binimg" in my code is the output of the relabel filter. At =
that point when I write it out, it is correct. However, further down the =
line, when I try to write out the output of the relabel filter, it is =
corrupted. In fact, all are corrupted and they all have the same values =
which is that of the result of the mask filter. Even "gs" the output of =
the extractimagefilter is wrong at the point where I am writing it out. =
Could you help me figure out the problem? Maybe there is something wrong =
in the way I am setting the region of iteration for each or is there a =
problem in reusing the extractimagefilter for first extracting a =
greyscale slice and then using the same thing to extract a mask?

Thanks
Radhika

------=_NextPart_001_0038_01C3C481.B924D550
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1276" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi Luis,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I am having some problems with my =
program. I have=20
enclosed a portion of my code here where the problem is. I am not able =
to figure=20
it out. Basically, "binimg" in my code is the output of the relabel =
filter. At=20
that point when I write it out, it is correct. However, further down the =
line,=20
when I try to write out the output of the relabel filter, it is =
corrupted. In=20
fact, all are corrupted and they all have the same&nbsp;values which is =
that of=20
the result of the mask filter. Even "gs" the output of the =
extractimagefilter is=20
wrong at the point where I am writing it out. Could you help me figure =
out the=20
problem? Maybe there is something wrong in the way I am setting the =
region of=20
iteration for each or is there a problem in reusing the =
extractimagefilter for=20
first extracting a greyscale slice and then using the same thing to =
extract a=20
mask?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Thanks</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Radhika</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV></BODY></HTML>

------=_NextPart_001_0038_01C3C481.B924D550--

------=_NextPart_000_0037_01C3C481.B924D550
Content-Type: application/octet-stream;
	name="Segment_regions.cxx"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="Segment_regions.cxx"


// ITK include files

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkBinaryErodeImageFilter.h"
#include "itkBinaryDilateImageFilter.h"
#include "itkBinaryBallStructuringElement.h"=20
#include "itkMinimumImageFilter.h"
#include "itkMaximumImageFilter.h"=20
#include "itkSubtractImageFilter.h"
#include "itkMaskImageFilter.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkConnectedComponentImageFilter.h"
#include "itkRelabelComponentImageFilter.h"
#include "itkConnectedThresholdImageFilter.h"
#include "itkExtractImageFilter.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkImageLinearIteratorWithIndex.h"
#include "itkImageSliceIteratorWithIndex.h"
#include "itkRegionOfInterestImageFilter.h"
#include "vnl/vnl_math.h"

int main()
{

	const unsigned int Dimension =3D 3;

	typedef unsigned char   PixelType;
	typedef int IntType;


	typedef itk::Image< PixelType,  Dimension >  ImageType;
	typedef itk::Image<PixelType,2> ImageType2D;
	typedef itk::Image<float, Dimension> FloatImageType;
      typedef itk::Image<IntType,2> IntImageType2D;


	typedef itk::ImageRegionConstIteratorWithIndex< ImageType2D > =
ConstIteratorType;
	typedef itk::ImageRegionIteratorWithIndex< ImageType2D > IteratorType;

	// Software Guide : EndCodeSnippet

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



	typedef itk::BinaryThresholdImageFilter< ImageType2D,ImageType2D >  =
BinaryThresholdFilterType;


 =20
	typedef itk::BinaryBallStructuringElement<PixelType, 2  >         =
StructuringElementType;
 =20
	typedef itk::BinaryErodeImageFilter<
								ImageType2D,=20
								ImageType2D,
								StructuringElementType >  ErodeFilterType;

	typedef itk::BinaryDilateImageFilter<
								ImageType2D,=20
								ImageType2D,=20
								StructuringElementType >  DilateFilterType;


	typedef itk::MinimumImageFilter<
								ImageType,=20
								ImageType,=20
								ImageType >  ANDFilterType;

	typedef itk::MaximumImageFilter<
								ImageType,=20
								ImageType,=20
								ImageType >  ORFilterType;=20

	typedef itk::SubtractImageFilter<
								ImageType,=20
								ImageType,=20
								ImageType >  NOTFilterType;

	typedef itk::MaskImageFilter<
								ImageType,=20
								ImageType,=20
								ImageType >  MASKFilterType;

	typedef itk::ExtractImageFilter< ImageType, ImageType2D > =
ExtractFilterType;
=09
	typedef itk::ConnectedComponentImageFilter<ImageType2D,IntImageType2D> =
ConnectedComponentFilterType;

	typedef itk::RelabelComponentImageFilter<IntImageType2D,ImageType2D> =
RelabelComponentFilterType;

	typedef itk::ConnectedThresholdImageFilter<ImageType2D,ImageType2D> =
ConnectedThresholdImageFilterType;

	typedef itk::ImageLinearIteratorWithIndex< ImageType2D > =
LinearIteratorType;
      typedef itk::ImageSliceIteratorWithIndex< ImageType >  =
SliceIteratorType;
      typedef itk::RegionOfInterestImageFilter< ImageType,ImageType >  =
CopyFilterType;



  // Creation of Reader and Writer filters

  ReaderType::Pointer readervolume =3D ReaderType::New();
  ReaderType::Pointer readermask =3D ReaderType::New();
  WriterType::Pointer writermask =3D WriterType::New();
  WriterType2D::Pointer writerbinimg2D =3D WriterType2D::New();
  WriterType2D::Pointer writernewmask2D =3D WriterType2D::New();
  WriterType2D::Pointer writernbinimg2D =3D WriterType2D::New();
  WriterType2D::Pointer writermask2D =3D WriterType2D::New();
  WriterType2D::Pointer writerthreshold2D =3D WriterType2D::New();
  WriterType2D::Pointer writergs2D =3D WriterType2D::New();
  WriterType2D::Pointer writerresult2D =3D WriterType2D::New();
 =20

 // Creation of pointers to various filters =20

  ErodeFilterType::Pointer  binaryErode  =3D ErodeFilterType::New();
  DilateFilterType::Pointer binaryDilate =3D DilateFilterType::New();
  ANDFilterType::Pointer ANDmasks =3D ANDFilterType::New();
  ORFilterType::Pointer ORmasks =3D ORFilterType::New();=20
  NOTFilterType::Pointer NOTmasks =3D NOTFilterType::New();
  MASKFilterType::Pointer MASKgs1 =3D MASKFilterType::New();
  MASKFilterType::Pointer MASKgs2 =3D MASKFilterType::New();
  BinaryThresholdFilterType::Pointer thresholder =3D =
BinaryThresholdFilterType::New();
  ExtractFilterType::Pointer extracter2D =3D ExtractFilterType::New();
  ConnectedComponentFilterType::Pointer connectedcomponent2D =3D =
ConnectedComponentFilterType::New();
  RelabelComponentFilterType::Pointer relabelcomponent2D =3D =
RelabelComponentFilterType::New();
  ConnectedThresholdImageFilterType::Pointer seedgrower2D =3D =
ConnectedThresholdImageFilterType::New();
  CopyFilterType::Pointer copier =3D CopyFilterType::New();

=20
  // Setting 2D structuring element

  StructuringElementType  structuringElement;

  structuringElement.SetRadius( 1 );  // 3x3 2D structuring element

  structuringElement.CreateStructuringElement();

  binaryErode->SetKernel(  structuringElement );
  binaryDilate->SetKernel( structuringElement );

  // Reading all inputs=20
 =20
 /* readervolume->SetFileName( argv[1] );
  readermask->SetFileName (argv[2]);
  writermask->SetFileName(argv[3]); */

  readervolume->SetFileName( "C:/Synarc/BSI/Data/image1.img");
  readermask->SetFileName ("C:/Synarc/BSI/Data/median1.img");
  writermask->SetFileName("C:/Synarc/BSI/Data/new.img");

  readervolume->Update();
  readermask->Update();
 =20
 // unsigned int sstart =3D ::atoi(argv[4]);
  // unsigned int send =3D ::atoi(argv[5]);

  unsigned int sstart =3D 43;
  unsigned int send =3D 150;


  PixelType background =3D   0;
  PixelType foreground =3D 255;

// Setting constant parameters

  unsigned int GTHRESHOLD =3D 70;
  unsigned int DTHRESHOLD =3D 40;
  unsigned int NPIXELS =3D 2000; // # of pixels overgrowth going from 3D =
to 2D
  unsigned int maxval =3D 255;
  unsigned int direction[2];
  direction[0] =3D 0;
  direction[1] =3D 1;

// 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(); // This is used to =
extract the required slice from the 3D image
  size[2] =3D 0;
  ImageType::IndexType start =3D inputRegion.GetIndex();
  ImageType::RegionType desiredRegion;
  desiredRegion.SetSize(  size  );


  // Setting background, foreground, threshold values for threshold =
filter
  thresholder->SetOutsideValue( background );
  thresholder->SetInsideValue(  foreground );
  thresholder->SetLowerThreshold( GTHRESHOLD );
  thresholder->SetUpperThreshold( maxval );
=20
    unsigned int s =3D 70; // Test slice 70

    start[2] =3D s;   =20
    desiredRegion.SetIndex( start );
	extracter2D->SetExtractionRegion( desiredRegion );
	extracter2D->SetInput(readervolume->GetOutput());
	extracter2D->Update();
    ImageType2D::Pointer gs =3D extracter2D->GetOutput();

	thresholder->SetInput(extracter2D->GetOutput());
	connectedcomponent2D->SetInput(thresholder->GetOutput());
	relabelcomponent2D->SetInput(connectedcomponent2D->GetOutput());
	relabelcomponent2D->Update();
	ImageType2D::Pointer binimg =3D relabelcomponent2D->GetOutput(); =
//Contains labeled components
=09
	writerbinimg2D->SetFileName("C:/Synarc/BSI/Data/binimg2D.img");
    writerbinimg2D->SetInput(binimg);
    writerbinimg2D->Update();=20


	// Setting extracter2D filter to the input 3D mask image
	extracter2D->SetInput(readermask->GetOutput());
	extracter2D->Update();
	ImageType2D::Pointer mask =3D extracter2D->GetOutput();
=09
=09

	// Create an empty image of same size as binimg
	ImageType2D::Pointer newmask =3D ImageType2D::New();
	newmask->SetRegions(binimg->GetRequestedRegion());
	newmask->CopyInformation(binimg);
	newmask->Allocate();

	// Setting pixels that are off in 3D mask slice to off in 2D mask

    ImageType2D::RegionType region =3D =
binimg->GetLargestPossibleRegion();
    ConstIteratorType maskit (mask, region);
    ConstIteratorType binimgit( binimg, region );
    IteratorType newmaskit(newmask,region);

   maskit.GoToBegin();
   binimgit.GoToBegin();
   newmaskit.GoToBegin();
   while( ! maskit.IsAtEnd() && ! binimgit.IsAtEnd() && ! =
newmaskit.IsAtEnd())
     {
	 newmaskit.Set(binimgit.Get());
       if( maskit.Get() =3D=3D 0 )   // if mask pixel is off
         {
		newmaskit.Set(0);
         }
       ++maskit;
	 ++binimgit;
	 ++newmaskit;
     }
  =20
   unsigned int number =3D relabelcomponent2D->GetNumberOfObjects();

   writermask2D->SetFileName("C:/Synarc/BSI/Data/mask2D.img");
   writermask2D->SetInput(mask);
   writermask2D->Update();=20

   writergs2D->SetFileName("C:/Synarc/BSI/Data/gs2D.img");
   writergs2D->SetInput(extracter2D->GetOutput());
   writergs2D->Update();=20

   writernewmask2D->SetFileName("C:/Synarc/BSI/Data/newmask2D.img");
   writernewmask2D->SetInput(newmask);
   writernewmask2D->Update();=20
  =20
   writerthreshold2D->SetFileName("C:/Synarc/BSI/Data/threshold2D.img");
   writerthreshold2D->SetInput(thresholder->GetOutput());
   writerthreshold2D->Update();
  =20

   writerbinimg2D->SetFileName("C:/Synarc/BSI/Data/binimg2D-1.img");
   writerbinimg2D->SetInput(relabelcomponent2D->GetOutput());
   writerbinimg2D->Update();=20

  std::cout << " All done ";

 // return 0;
}


------=_NextPart_000_0037_01C3C481.B924D550--