Fwd: Re: [Insight-users] Inhomogenity correction using MRIBiasField Correction

Luis Ibanez luis . ibanez at kitware . com
Mon, 05 Aug 2002 10:02:55 -0400


Hi cspl,

As Jisung just said, the itk::Array objects require
to have a size at construction time, like

    itk::Array<double> myArray(100);

or change it by assignment (which is a slow operation):

    itk::Array<double> myArray;
    myArray = itk::Array<double>( 100 );


Notice that this is not using the "new" operator explicitly.

In your code, you are dynamically allocating the Array
and it seems that you are accidentally missusing the
pointer:

    itk::Array<double> *classMeans = new itk::Array<double>;
    classMeans[0] = 1500;
    classMeans[1] = 570;

Here, the 1500 is being writen on top of the actual
address of the itk::Array<double> instead of being stored
in the first element of the array. The 570 value is being
writen on whatever is following the address of the array
class and not into the second element of the array...  :-/


If you want to use a dynamically allocated array,
the assignment should be something like:

    (*classMeans)[0] = 1500;

However, You probably don't get any advantage with the
dynamic allocation, since the Array can take care of most
of this for you. The code will be cleaner if you allocate
the array statically as:

    typedef itk::Array<double>  ArrayType;
    ArrayType  classMeans(2);
    classMeans[0] = 1500;
    classMeans[1] = 570;

and then pass it to the filter as:

    filter->SetTissueClassStatistics( classMeans, classSigmas );

The SetTissueClassStatistics() method is conveniently
expecting a const reference to a itk::Array<>.


Hope that helps,


    Luis


========================================================

Jisung Kim wrote:
> --- Jisung Kim <bahrahm@yahoo.com> wrote:
> 
>>Date: Mon, 5 Aug 2002 06:35:24 -0700 (PDT)
>>From: Jisung Kim <bahrahm@yahoo.com>
>>Subject: Re: [Insight-users] Inhomogenity correction
>>using MRIBiasField Correction
>>To: cspl <affable@hd2.dot.net.in>
>>
>>Hello.
>>
>>I think you should create the mean and sigma array
>>with specific array size. In you case, The mean
>>array
>>creation statement should looks like this:
>>
>>  itk::Array<double> *classMeans = new
>>  itk::Array<double>(2);
>>
>>  itk::Array<double> *classSigmas= new
>>  itk::Array<double>(2) ;
>>
>>Thank you,
>>
>>Jisung Kim.
>>
>>--- cspl <affable@hd2.dot.net.in> wrote:
>>
>>>Dear Mr.bjorn & Friends,
>>>
>>>I am working on itkMRIBiasFieldCorrectionFilter.I
>>>got hte problem with array to assign mean and
>>>
>>sigma
>>
>>>values. I got exception while running at setting
>>>parameters like as follows.
>>>
>>>Volume* CIPServerDoc::Inhomogenity(Volume
>>>*vol,Volume *mask)
>>>{
>>> if (vol == NULL) return vol;
>>> short *Buffer = new short[vol->width *
>>>
>>vol->height
>>
>>>*  vol->depth];
>>> short *Buffer1 = new short[mask->width *
>>>mask->height *  mask->depth];
>>> 
>>> Buffer = (short *) vol->Mem;
>>> Buffer1= (short *) mask->Mem; 
>>> 
>>> typedef itk::Image<float,3> ImageType;
>>> typedef itk::Image<float,3> ImageType1;
>>> typedef itk::Image<float,3> ImageType2;
>>>
>>> itk::RawImageIO<float, 3>::Pointer output_io
>>>      = itk::RawImageIO<float, 3>::New();
>>> 
>>>     output_io->SetByteOrderToLittleEndian();
>>>     output_io->SetFileTypeToBinary();
>>>     output_io->SetFileDimensionality(3);
>>>
>>>     // Initialize the initial and target volumes.
>>>        ImageType::Pointer vol_init =
>>>ImageType::New();
>>> ImageType1::Pointer vol_init1 =
>>>
>>ImageType1::New();
>>
>>> ImageType2::Pointer vol_init2 =
>>>
>>ImageType2::New();
>>
>>> 
>>>    
>>>    ImageType::SizeType sz;
>>>    sz[0] = vol->width;
>>>    sz[1] = vol->height;
>>> sz[2]=vol->depth; 
>>>
>>> ImageType1::SizeType sz1;
>>> sz1[0] = mask->width;
>>> sz1[1] = mask->height;
>>> sz1[2]=  mask->depth; 
>>>
>>>    
>>> ImageType::IndexType idx;
>>> idx[0] = 0;
>>> idx[1] = 0;
>>> idx[2] = 0 ;
>>>
>>> ImageType1::IndexType idx1;
>>>        idx1[0] = 0;
>>>        idx1[1] = 0;
>>> idx1[2] = 0 ;
>>>    
>>>        ImageType::RegionType reg;  
>>>        reg.SetSize(sz);
>>>        reg.SetIndex(idx);
>>>
>>> ImageType1::RegionType reg1;  
>>>        reg.SetSize(sz1);
>>>        reg.SetIndex(idx1);
>>>    
>>>        vol_init->SetRegions(reg);
>>> vol_init1->SetRegions(reg1);
>>> vol_init2->SetRegions(reg);
>>>        vol_init->Allocate();
>>> vol_init1->Allocate();
>>> vol_init2->Allocate();
>>>
>>>
>>> 
>>> for(int z=0; z<vol->depth; ++z )
>>> { 
>>>  idx[2]=z;
>>> 
>>>         for (int y = 0; y < vol->height; ++y)
>>>          {
>>>           idx[1] = y;
>>>           for (int x = 0; x < vol->width; ++x)
>>>            {
>>>              idx[0] = x;
>>>      vol_init->SetPixel(idx, *(Buffer  
>>>+(z*vol->height*vol->width)+y *vol->width +x));
>>>         }
>>>     }
>>>  
>>> }
>>> 
>>> 
>>> ////mask
>>> for(int z1=0; z1<mask->depth; ++z1 )
>>> { 
>>>     idx1[2]=z1;
>>>    for (int y1 = 0; y1<mask->height; ++y1)
>>>        {
>>>          idx1[1] = y1;
>>>    //AfxMessageBox("in mask height loop");
>>>          for (int x1=0;  x1 < mask->width; ++x1)
>>>            {
>>>              idx1[0] = x1;
>>>       vol_init2->SetPixel(idx1,*(Buffer1  
>>>+(z1*mask->height*mask->width)+y1 *mask->width
>>>+x1));
>>>      }
>>>  
>>>         }
>>>   
>>> }
>>>    
>>>typedef
>>>
>>>
> BiasFieldCorrectionFilter<ImageType,ImageType,ImageType1>
> 
>>> Corrector;
>>> Corrector::Pointer filter = Corrector::New() ;
>>>//parameters initiliazation
>>>  bool useLog = true;
>>>  int degree = 3;
>>>  int sliceDirection = 2;
>>>  
>>>
>>>  vnl_vector<double> coefficientVector ;
>>>  itk::Array<double> *classMeans = new
>>>itk::Array<double>;
>>>  
>>> classMeans[0]=1500;
>>> classMeans[1]=570;
>>>
>>> itk::Array<double> *classSigmas= new
>>>itk::Array<double> ;
>>> classSigmas[0]=100;
>>> classSigmas[1]=70;
>>>
>>> int volumeMaximumIteration = 2000; 
>>> int interSliceMaximumIteration = 20; 
>>>// double initialRadius ;
>>> double growth = 1.05;
>>> double shrink = 0.0;
>>> AfxMessageBox("before  set input");
>>> filter->SetInput(vol_init);
>>> filter->SetInputMask(vol_init2);
>>>
>>>
>>> filter->IsBiasFieldMultiplicative(useLog) ;
>>>
>>>
> ///////////////////////////////////////////////////////////////////////
> 
>>>
> filter->SetTissueClassStatistics(*classMeans,*classSigmas)
> 
>>>;/// /* here Getting exception  */
>>>
>>>
> //////////////////////////////////////////////////////////////////////
> 
>>> filter->SetOptimizerGrowthFactor(growth) ;
>>>
>>> filter->SetOptimizerShrinkFactor(shrink) ;
>>>
>>>
>>>
>>>
> filter->SetVolumeCorrectionMaximumIteration(volumeMaximumIteration)
> 
>>>;
>>> 
>>>
>>>
>>>
> //filter->SetInterSliceCorrectionMaximumIteration(interSliceMaximumIteration)
> 
>>>;
>>>
>>>
>>//filter->SetOptimizerInitialRadius(initialRadius)
>>
>>>;
>>> filter->SetBiasFieldDegree(degree) ;
>>> 
>>>//
>>>
>>>
> filter->SetUsingSlabIdentification(usingSlabIdentification)
> 
>>>;
>>> filter->SetSlicingDirection(sliceDirection) ;
>>> 
>>>  
>>> AfxMessageBox("before update");
>>> filter->Update();
>>> 
>>> vol_init2=filter->GetOutput();
>>> 
>>> for(unsigned int z=0; z<vol->depth; ++z )
>>> {
>>>  idx[2]=z;
>>> 
>>>      for (unsigned int y = 0; y < vol->height;
>>>
>>++y)
>>
>>>        {
>>>          idx[1] = y;
>>>          for (unsigned int x = 0; x < vol->width;
>>>++x)
>>>            {
>>>              idx[0] = x;
>>>              *(Buffer
>>>
>>+(z*vol->height*vol->width)+y
>>
>>>*vol->width    +x)=vol_init->GetPixel(idx);
>>>            }
>>>        }
>>> }
>>>
>>> vol->Mem = Buffer;
>>>        return vol;
>>>
>>>}
>>>
>>>
>>>
>>=== message truncated ===
>>
>>
>>=====
>>Jisung Kim
>>bahrahm@yahoo.com
>>106 Mason Farm Rd.
>>129 Radiology Research Lab., CB# 7515
>>Univ. of North Carolina at Chapel Hill
>>Chapel Hill, NC 27599-7515
>>
>>__________________________________________________
>>Do You Yahoo!?
>>Yahoo! Health - Feel better, live better
>>http://health.yahoo.com
>>
>>
> 
> 
> =====
> Jisung Kim
> bahrahm@yahoo.com
> 106 Mason Farm Rd.
> 129 Radiology Research Lab., CB# 7515
> Univ. of North Carolina at Chapel Hill
> Chapel Hill, NC 27599-7515
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Health - Feel better, live better
> http://health.yahoo.com
> _______________________________________________
> Insight-users mailing list
> Insight-users@public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-users
> 
>