[Insight-users] ConnectedThresholdImageFilter doestakeintoaccopunt the configuration

Michael Jackson mike.jackson at bluequartz.net
Tue Oct 13 12:22:31 EDT 2009


why are you passing the ImportFilter by reference?

static bool Gmcao2Itk(typename TItkImportFilter::Pointer &p_pImporter,
  			       const TImage &p_Image)

Shouldn't that be:

static bool Gmcao2Itk(typename TItkImportFilter::Pointer p_pImporter,
  			       const TImage &p_Image)
  With smart pointers it is better to not pass by reference, in fact  
it can have bad side effects.

I have the following code and it works in debug and release modes:

//-- ITK Typedefs
namespace aim
{
namespace Gaussian
{
const unsigned int Dimension = 2;
}
}
//typedef unsigned char PixelType;
typedef float                                               
FloatPixelType;
typedef itk::Image<FloatPixelType,
			aim::Gaussian::Dimension>          FloatImageType;
typedef itk::ImportImageFilter<FloatPixelType,
				aim::Gaussian::Dimension>  ImportFilterType;

//  
-----------------------------------------------------------------------------
//
//  
-----------------------------------------------------------------------------
void  
ITKGaussianFilter::initializeImportFilter(ImportFilterType::Pointer  
importFilter,
                                                FloatPixelType*  
imageData)
{
   ImportFilterType::SizeType size;
   size[0] = m_ZArray->getImagePixelWidth(); // size along X
   size[1] = m_ZArray->getImagePixelHeight(); // size along Y

   ImportFilterType::IndexType start;
   start.Fill(0);
   ImportFilterType::RegionType region;
   region.SetIndex(start);
   region.SetSize(size);
   importFilter->SetRegion(region);

   double origin[aim::Gaussian::Dimension] = {0.0, 0.0};
   importFilter->SetOrigin(origin);

   double spacing[aim::Gaussian::Dimension] = {1.0, 1.0};
   importFilter->SetSpacing(spacing);

   const bool importImageFilterWillOwnTheBuffer = false;
   importFilter->SetImportPointer(imageData, size[0] * size[1],  
importImageFilterWillOwnTheBuffer);
   importFilter->Update();
}

//  
-----------------------------------------------------------------------------
//
//  
-----------------------------------------------------------------------------
int32 ITKGaussianFilter::execute()
{
//m_ZArray->getArrayData() will return a pointer to float.
   int32 err = 0;
   ImportFilterType::Pointer importFilter = ImportFilterType::New();
   initializeImportFilter(importFilter, m_ZArray->getArrayData() );

   GaussianType::Pointer gaussianFilter = GaussianType::New();
   gaussianFilter->SetInput(importFilter->GetOutput());
   gaussianFilter->SetVariance(m_Sigma);
   gaussianFilter->SetUseImageSpacingOn();
   gaussianFilter->SetMaximumKernelWidth(m_MaximumKernelWidth);
   //gaussianFilter->Update();

   MinMaxFilterType::Pointer minMaxFilter = MinMaxFilterType::New();
   minMaxFilter->SetInput(gaussianFilter->GetOutput());
   minMaxFilter->Update();

   FloatImageType* outputImage = minMaxFilter->GetOutput();
   // Copy the data from 'outputImage' into the m_ZArray
   err = m_ZArray->initializeImageWithSourceData(m_ZArray- 
 >getImagePixelWidth(),
                                                 m_ZArray- 
 >getImagePixelHeight(),
                                                 outputImage- 
 >GetBufferPointer());

   m_Limits[0] = minMaxFilter->GetMinimum();
   m_Limits[1] = minMaxFilter->GetMaximum();

   return err;
}

_________________________________________________________
Mike Jackson                  mike.jackson at bluequartz.net
BlueQuartz Software                    www.bluequartz.net
Principal Software Engineer                  Dayton, Ohio

On Oct 13, 2009, at 12:06 PM, Daanen Vincent wrote:

>> Try this:
>>
>> TItkImage* ptr = l_pImportFilter->GetOutput();
>> std::cout << "ref Count: " << ptr->GetReferenceCount() << std::endl;
>> ptr->Register(); // will increment the reference count
>> std::cout << "ref Count: " << ptr->GetReferenceCount() << std::endl;
>>
>> You should see the value incremented if _I_ understand the code
>> correctly..
>
> Yes but the appli still crashes .....
>
> I am more and more convinced that there is something unclear in the  
> behavior
> of the smart pointer ...
>
> I just change the conversion method into the one below.
>
> I think you will agree with me that the scope of the importer does  
> not end
> at the end of the method ( in fact, this method just parameterize the
> importer).
> Well, the content of the importer and thus of the output image is  
> correct in
> debug but the appli still crashes in release (I just try to write  
> the image
> of disk). Even, just a call to importer->GetOutput()->something  
> crashes ...
>
> Even if the debugger performs some initialization as you saisd (and  
> you're
> right), in general, there are some values that show that the  
> component is
> badly initialized and not in my case :(
>
> //-----
> static bool Gmcao2Itk(typename TItkImportFilter::Pointer  
> &p_pImporter, const
> TImage &p_Image)
> {
> 	typedef TItkInterface::TItkImportFilter TItkImportFilter;
> 	if (p_pImporter.IsNull())	
> 	{
> 		p_pImporter = TItkImportFilter::New();
> 		if (p_pImporter.IsNull())	return false;
> 	}
> 	// image Size
> 	{
> 		TItkImportFilter::SizeType l_ImgSize;
> 		{
> 			TImage::TIndex l_ImgWidths = p_Image.GetWidths();
> 			l_ImgSize[0] = l_ImgWidths[0];
> 			l_ImgSize[1] = l_ImgWidths[1];
> 			l_ImgSize[2] = l_ImgWidths[2];
> 		}
>
> 		TItkImportFilter::IndexType l_RegionStart;
> 		l_RegionStart.Fill( 0 );
>
> 		TItkImportFilter::RegionType l_Region;
> 		l_Region.SetIndex( l_RegionStart );
> 		l_Region.SetSize( l_ImgSize );
> 		p_pImporter->SetRegion( l_Region );
> 	}
>
> 	// Image Origin
> 	{
> 		double l_ImgOrigin[DIM];
> 		l_ImgOrigin[0] = p_Image.GetImage2Global().GetOrigin().X();
> 		l_ImgOrigin[1] = p_Image.GetImage2Global().GetOrigin().Y();
> 		l_ImgOrigin[2] = p_Image.GetImage2Global().GetOrigin().Z();
> 		p_pImporter->SetOrigin( l_ImgOrigin );
> 	}
> 		// image scale
> 	{
> 		CVector3D l_ImgScale;
> 		p_Image.GetImage2Global().GetScale(l_ImgScale);
> 		double l_Scale[DIM];
> 		l_Scale[0] = l_ImgScale.X();
> 		l_Scale[1] = l_ImgScale.Y();
> 		l_Scale[2] = l_ImgScale.Z();
> 		p_pImporter->SetSpacing( l_Scale );
> 	}
> 		// image direction
> 	{
> 		CAffineTransform3D l_T = p_Image.GetImage2Global();
> 		l_T.Normalize();
> 		TItkImage::DirectionType l_ImgDir;
> 		l_ImgDir(0,0) = l_T.GetI().X();
> 		l_ImgDir(1,0) = l_T.GetI().Y();
> 		l_ImgDir(2,0) = l_T.GetI().Z();
> 		l_ImgDir(0,1) = l_T.GetJ().X();
> 		l_ImgDir(1,1) = l_T.GetJ().Y();
> 		l_ImgDir(2,1) = l_T.GetJ().Z();
> 		l_ImgDir(0,2) = l_T.GetK().X();
> 		l_ImgDir(1,2) = l_T.GetK().Y();
> 		l_ImgDir(2,2) = l_T.GetK().Z();
> 		p_pImporter->SetDirection(l_ImgDir);
> 	}
>
> 	const bool l_bImportImageFilterWillOwnTheBuffer = false;
> 	TYPE *l_pDataBuff = (TYPE *)(p_Image.Begin());
> 	p_pImporter->SetImportPointer( l_pDataBuff,
> p_Image 
> .GetWidths().GetElementProduct(),l_bImportImageFilterWillOwnTheBuffer
> );
> 	p_pImporter->Update();
> 	return true;
> };
>



More information about the Insight-users mailing list