Ejemplo n.º 1
0
mitkIpPicDescriptor *mitkIpFuncRank ( mitkIpPicDescriptor *pic_old,
                              mitkIpUInt4_t       rank,
                              mitkIpUInt4_t       mask_dim,
                              mitkIpUInt4_t       mask_size,
                              mitkIpFuncFlagI_t   border )
{
  
  mitkIpPicDescriptor *pic_new;            /* pointer to transformed image            */
  mitkIpInt4_t       i;                    /* loop index                              */
  mitkIpInt4_t       offset;               /* offset of image                         */
  mitkIpInt4_t       ind[_mitkIpPicNDIM];      /* loop index vector                       */
  mitkIpInt4_t       *off_vekt;            /* pointer to offset vector                */
  mitkIpInt4_t       begin;                /* 0.5 * mask_size                         */
  mitkIpUInt4_t      size[_mitkIpPicNDIM];     /*                                         */
  mitkIpInt4_t       n[_mitkIpPicNDIM];        /* size of each dimension                  */
  mitkIpUInt4_t      no_elem;              /* number of elements in mask              */
  mitkIpUInt4_t      len;                  /* length of offset vector                 */

  /* calculate number of elements in mask                                         */

  no_elem = mask_size;
  for ( i = 1; i < mask_dim; i++ )
    no_elem = no_elem * mask_size;

  /* check whether data are correct                                               */

  if ( _mitkIpFuncError ( pic_old ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );
  if ( mask_dim < 1 || mask_dim > pic_old->dim )  
    { 
       _mitkIpFuncSetErrno ( mitkIpFuncDIMMASC_ERROR );
       return ( mitkIpFuncERROR );
    }
  if ( rank > no_elem ) 
    { 
       _mitkIpFuncSetErrno ( mitkIpFuncDATA_ERROR );
       return ( mitkIpFuncERROR );
    }
  if ( mask_size % 2 != 1 ) 
    { 
       _mitkIpFuncSetErrno ( mitkIpFuncSIZE_ERROR );
       return ( mitkIpFuncERROR );
    }

  /* initialize vectors and variables                                             */

  size[0] = 1;
  for ( i = 1; i < _mitkIpPicNDIM; i++ )
    size[i] = size[i-1] * pic_old->n[i-1];

  len = 0;
  begin = mask_size / 2;

  for ( i = 0; i < mask_dim; i++ )
    n[i] = begin + 1;
  for ( i = mask_dim; i < _mitkIpPicNDIM; i++ )
    n[i] = 1 - begin;
  
  /* allocate image structure                                                     */

  if ( border == mitkIpFuncBorderOld )
    pic_new = mitkIpPicClone ( pic_old );
  else if ( border == mitkIpFuncBorderZero )
    {
       pic_new = mitkIpPicCopyHeader ( pic_old, 0 );
       pic_new->data = calloc ( _mitkIpPicElements ( pic_new ), pic_new->bpe/8  );
    }
  else 
    {
       _mitkIpFuncSetErrno ( mitkIpFuncFLAG_ERROR );
       return ( mitkIpFuncERROR );
    }

  if ( pic_new == NULL )
    {
       _mitkIpFuncSetErrno ( mitkIpFuncPICNEW_ERROR );
       return ( mitkIpFuncERROR );
    }

  /* allocate offset vector                                                       */

  off_vekt = malloc ( no_elem * sizeof ( mitkIpUInt4_t ) );
  if ( off_vekt == NULL )
    {
       _mitkIpFuncSetErrno ( mitkIpFuncMALLOC_ERROR );
       return ( mitkIpFuncERROR );
    }

  /* calculate offset vector                                                      */

  for ( ind[0] = -begin; ind[0] < n[0]; ind[0]++ )
    for ( ind[7] = -begin; ind[7] < n[7]; ind[7]++ )
      for ( ind[6] = -begin; ind[6] < n[6]; ind[6]++ )
        for ( ind[5] = -begin; ind[5] < n[5]; ind[5]++ )
          for ( ind[4] = -begin; ind[4] < n[4]; ind[4]++ )
            for ( ind[3] = -begin; ind[3] < n[3]; ind[3]++ )
              for ( ind[2] = -begin; ind[2] < n[2]; ind[2]++ )
                for ( ind[1] = -begin; ind[1] < n[1]; ind[1]++ )
                  { 
                     offset = 0;
                     for ( i = 0; i < pic_old->dim; i++ )
                       offset = offset + ind[i] * size[i]; 
                     off_vekt[len] = offset;
                     len++;
                  }
  if ( rank == 0 ) rank = no_elem / 2 + 1;
  mitkIpPicFORALL_4 ( RANK, pic_old, begin, no_elem, size, rank );
      
  free ( off_vekt );

  /* Copy Tags */

  mitkIpFuncCopyTags(pic_new, pic_old);
  
  
                        
  return ( pic_new );
}
Ejemplo n.º 2
0
int mitkImageTest(int argc, char* argv[])
{

  MITK_TEST_BEGIN(mitkImageTest);

  //Create Image out of nowhere
  mitk::Image::Pointer imgMem;
  mitk::PixelType pt(typeid(int));
  unsigned int dim[]={100,100,20};

  std::cout << "Testing creation of Image: ";
  imgMem=mitk::Image::New();
  if(imgMem.IsNull())
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing Initialize(const mitk::PixelType& type, unsigned int dimension, unsigned int *dimensions): ";
  imgMem->Initialize(mitk::PixelType(typeid(int)), 3, dim);
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing IsInitialized(): ";
  if(imgMem->IsInitialized()==false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing GetData(): ";
  int *p = (int*)imgMem->GetData();
  if(p==NULL)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Filling image: ";
  unsigned int i;
  unsigned int size = dim[0]*dim[1]*dim[2];
  for(i=0; i<size; ++i, ++p)
    *p= (signed int)i;
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Getting it again and compare with filled values: ";
  int *p2 = (int*)imgMem->GetData();
  if(p2==NULL)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  for(i=0; i<size; ++i, ++p2)
  {
    if(*p2!= (signed int)i )
    {
      std::cout<<"[FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing IsInitialized(): ";
  if(imgMem->IsInitialized()==false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;
  
  std::cout << "Testing GetSliceData() and compare with filled values: ";
  p2 = (int*)imgMem->GetSliceData(dim[2]/2)->GetData();
  unsigned int xy_size = dim[0]*dim[1];
  unsigned int start_mid_slice = (dim[2]/2)*xy_size;
  for(i=0; i<xy_size; ++i, ++p2)
  {
    if(*p2!=(signed int)(i+start_mid_slice))
    {
      std::cout<<"[FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
  }
  std::cout<<"[PASSED]"<<std::endl;

  //----
  mitkIpPicDescriptor *pic_slice=mitkIpPicClone(imgMem->GetSliceData(dim[2]/2)->GetPicDescriptor());
  imgMem=mitk::Image::New();

  std::cout << "Testing reinitializing via Initialize(const mitk::PixelType& type, unsigned int dimension, unsigned int *dimensions): ";
  imgMem->Initialize(mitk::PixelType(typeid(int)), 3, dim);
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing slice-wise filling via SetPicSlice(): ";
  for(i=0;i<dim[2];++i)
  {
    imgMem->SetPicSlice(pic_slice, i, 0, 0);
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Getting it again and compare with filled values: ";
  p2 = (int*)imgMem->GetData();
  if(p2==NULL)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  for(i=0; i<size; ++i, ++p2)
  {
    if(*p2!= (signed int)((i%xy_size)+start_mid_slice))
    {
      std::cout<<"[FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing IsInitialized(): ";
  if(imgMem->IsInitialized()==false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Setting a copy of the volume once again: ";
  imgMem->SetPicVolume(mitkIpPicClone(imgMem->GetVolumeData(0)->GetPicDescriptor()),0);
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Set a slice with different content via SetPicSlice(): ";
  memset(pic_slice->data,0,xy_size*sizeof(int));
  imgMem->SetPicSlice(pic_slice, 1);

  std::cout << "Getting the volume again and compare the check the changed slice: ";
  p2 = (int*)imgMem->GetData();
  if(p2==NULL)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  p2+=xy_size;
  for(i=0; i<xy_size; ++i, ++p2)
  {
    if(*p2!=0)
    {
      std::cout<<"[FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
  }
  std::cout<<"[PASSED]"<<std::endl;


  std::cout << "Setting volume again: ";
  imgMem->SetVolume(imgMem->GetData());
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Set a slice with different content via SetSlice(): ";
  memset(pic_slice->data,0,xy_size*sizeof(int));
  imgMem->SetSlice(pic_slice->data, 0);

  std::cout << "Getting the volume again and compare the check the changed slice: ";
  p2 = (int*)imgMem->GetData();
  if(p2==NULL)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  for(i=0; i<xy_size; ++i, ++p2)
  {
    if(*p2!=0)
    {
      std::cout<<"[FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
  }
  std::cout<<"[PASSED]"<<std::endl;



  //std::cout << "Testing SetVolume(): ";
  //imgMem->SetVolume(data);
  //std::cout<<"[PASSED]"<<std::endl;

  mitkIpPicFree(pic_slice);

  //-----------------
  // geometry information for image
  mitk::Point3D origin;
  mitk::Vector3D right, bottom;
  mitk::Vector3D spacing;
  mitk::FillVector3D(origin, 17.0, 19.92, 7.83);
  mitk::FillVector3D(right, 1.0, 2.0, 3.0);
  mitk::FillVector3D(bottom, 0.0, -3.0, 2.0);
  mitk::FillVector3D(spacing, 0.78, 0.91, 2.23);

  std::cout << "Testing InitializeStandardPlane(rightVector, downVector, spacing): " << std::flush;
  mitk::PlaneGeometry::Pointer planegeometry = mitk::PlaneGeometry::New();
  planegeometry->InitializeStandardPlane(100, 100, right, bottom, &spacing);
  planegeometry->SetOrigin(origin);
  std::cout << "done" << std::endl;

  std::cout << "Testing Initialize(const mitk::PixelType& type, const mitk::Geometry3D& geometry, unsigned int slices) with PlaneGeometry and GetData(): ";
  imgMem->Initialize(mitk::PixelType(typeid(int)), *planegeometry);
  p = (int*)imgMem->GetData();
  if(p==NULL)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing Initialize(const mitk::PixelType& type, int sDim, const mitk::PlaneGeometry& geometry) and GetData(): ";
  imgMem->Initialize(mitk::PixelType(typeid(int)), 40, *planegeometry);
  p = (int*)imgMem->GetData();
  if(p==NULL)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  //-----------------
  // testing origin information and methods
  std::cout << "Testing correctness of origin via GetGeometry()->GetOrigin(): ";
  if( mitk::Equal(imgMem->GetGeometry()->GetOrigin(), origin) == false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing correctness of origin via GetTimeSlicedGeometry()->GetOrigin(): ";
  if( mitk::Equal(imgMem->GetTimeSlicedGeometry()->GetOrigin(), origin) == false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  mitk::FillVector3D(origin, 37.0, 17.92, 27.83);
  std::cout << "Setting origin via SetOrigin(origin): ";
  imgMem->SetOrigin(origin);
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing correctness of changed origin via GetGeometry()->GetOrigin(): ";
  if( mitk::Equal(imgMem->GetGeometry()->GetOrigin(), origin) == false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing correctness of changed origin via GetTimeSlicedGeometry()->GetOrigin(): ";
  if( mitk::Equal(imgMem->GetTimeSlicedGeometry()->GetOrigin(), origin) == false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing correctness of changed origin via GetSlicedGeometry()->GetGeometry2D(0)->GetOrigin(): ";
  if( mitk::Equal(imgMem->GetSlicedGeometry()->GetGeometry2D(0)->GetOrigin(), origin) == false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  //-----------------
  // testing spacing information and methods
  std::cout << "Testing correctness of spacing via GetGeometry()->GetSpacing(): ";
  if( mitk::Equal(imgMem->GetGeometry()->GetSpacing(), spacing) == false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing correctness of spacing via GetTimeSlicedGeometry()->GetSpacing(): ";
  if( mitk::Equal(imgMem->GetTimeSlicedGeometry()->GetSpacing(), spacing) == false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  mitk::FillVector3D(spacing, 7.0, 0.92, 1.83);
  std::cout << "Setting spacing via SetSpacing(spacing): ";
  imgMem->SetSpacing(spacing);
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing correctness of changed spacing via GetGeometry()->GetSpacing(): ";
  if( mitk::Equal(imgMem->GetGeometry()->GetSpacing(), spacing) == false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing correctness of changed spacing via GetTimeSlicedGeometry()->GetSpacing(): ";
  if( mitk::Equal(imgMem->GetTimeSlicedGeometry()->GetSpacing(), spacing) == false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing correctness of changed spacing via GetSlicedGeometry()->GetGeometry2D(0)->GetSpacing(): ";
  if( mitk::Equal(imgMem->GetSlicedGeometry()->GetGeometry2D(0)->GetSpacing(), spacing) == false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  //-----------------
  MITK_TEST_OUTPUT(<< "Testing SetImportChannel");
  mitk::Image::Pointer vecImg = mitk::Image::New();
  vecImg->Initialize(*imgMem->GetPixelType().GetTypeId(), *imgMem->GetGeometry(), 2 /* #channels */, 0 /*tDim*/ );
  vecImg->SetImportChannel(imgMem->GetData(), 0, mitk::Image::CopyMemory );
  vecImg->SetImportChannel(imgMem->GetData(), 1, mitk::Image::CopyMemory );
  std::cout<<"[PASSED]"<<std::endl;

  MITK_TEST_OUTPUT(<< " Testing whether IsValidSlice returns valid after SetImportChannel");
  MITK_TEST_CONDITION_REQUIRED( vecImg->IsValidSlice(0,0,1) , "");

  MITK_TEST_OUTPUT(<< " Testing whether CopyMemory worked");
  MITK_TEST_CONDITION_REQUIRED(imgMem->GetData() != vecImg->GetData(), "");

  MITK_TEST_OUTPUT(<< " Testing destruction after SetImportChannel");
  vecImg = NULL; 
  std::cout<<"[PASSED]"<<std::endl;

  //-----------------
  MITK_TEST_OUTPUT(<< "Testing initialization via vtkImageData");
  MITK_TEST_OUTPUT(<< " Setting up vtkImageData");
  vtkImageData* vtkimage = vtkImageData::New();
  vtkimage->Initialize();
  vtkimage->SetDimensions( 2, 3, 4);
  double vtkorigin[] =  {-350,-358.203, -1363.5};
  vtkimage->SetOrigin(vtkorigin);
  mitk::Point3D vtkoriginAsMitkPoint;
  mitk::vtk2itk(vtkorigin, vtkoriginAsMitkPoint);
  double vtkspacing[] =  {1.367, 1.367, 2};
  vtkimage->SetSpacing(vtkspacing);
  vtkimage->SetScalarType( VTK_SHORT );
  vtkimage->AllocateScalars();
  std::cout<<"[PASSED]"<<std::endl;
  
  MITK_TEST_OUTPUT(<< " Testing mitk::Image::Initialize(vtkImageData*, ...)");
  mitk::Image::Pointer mitkByVtkImage = mitk::Image::New();
  mitkByVtkImage ->Initialize(vtkimage);
  MITK_TEST_CONDITION_REQUIRED(mitkByVtkImage->IsInitialized(), "");

  MITK_TEST_OUTPUT(<< " vtkimage->Delete");
  vtkimage->Delete();
  std::cout<<"[PASSED]"<<std::endl;

  MITK_TEST_OUTPUT(<< " Testing whether spacing has been correctly initialized from vtkImageData");
  mitk::Vector3D spacing2 = mitkByVtkImage->GetGeometry()->GetSpacing();
  mitk::Vector3D vtkspacingAsMitkVector;
  mitk::vtk2itk(vtkspacing, vtkspacingAsMitkVector);
  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(spacing2,vtkspacingAsMitkVector), "");

  MITK_TEST_OUTPUT(<< " Testing whether GetSlicedGeometry(0)->GetOrigin() has been correctly initialized from vtkImageData");
  mitk::Point3D origin2 = mitkByVtkImage->GetSlicedGeometry(0)->GetOrigin();
  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(origin2,vtkoriginAsMitkPoint), "");

  MITK_TEST_OUTPUT(<< " Testing whether GetGeometry()->GetOrigin() has been correctly initialized from vtkImageData");
  origin2 = mitkByVtkImage->GetGeometry()->GetOrigin();
  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(origin2,vtkoriginAsMitkPoint), "");

  MITK_TEST_OUTPUT(<< " Testing whether GetTimeSlicedGeometry()->GetOrigin() has been correctly initialized from vtkImageData");
  origin2 = mitkByVtkImage->GetTimeSlicedGeometry()->GetOrigin();
  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(origin2,vtkoriginAsMitkPoint), "");

  // TODO test the following initializers on channel-incorporation
  //  void mitk::Image::Initialize(const mitk::PixelType& type, unsigned int dimension, unsigned int *dimensions, unsigned int channels)
  //  void mitk::Image::Initialize(const mitk::PixelType& type, int sDim, const mitk::Geometry2D& geometry2d, bool flipped, unsigned int channels, int tDim )
  //  void mitk::Image::Initialize(const mitk::Image* image) 
  //  void mitk::Image::Initialize(const mitkIpPicDescriptor* pic, int channels, int tDim, int sDim)

  //mitk::Image::Pointer vecImg = mitk::Image::New();
  //vecImg->Initialize(PixelType(typeid(float), 6, itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR), *imgMem->GetGeometry(), 2 /* #channels */, 0 /*tDim*/, false /*shiftBoundingBoxMinimumToZero*/ );
  //vecImg->Initialize(PixelType(typeid(itk::Vector<float,6>)), *imgMem->GetGeometry(), 2 /* #channels */, 0 /*tDim*/, false /*shiftBoundingBoxMinimumToZero*/ );

  // testing access by index coordinates and by world coordinates
  
  mitk::DataNode::Pointer node;      
  mitk::DataNodeFactory::Pointer nodeReader = mitk::DataNodeFactory::New();
  MITK_TEST_CONDITION_REQUIRED(argc == 2, "Check if test image is accessible!"); 
  const std::string filename = std::string(argv[1]);
  try
  {
    nodeReader->SetFileName(filename);
    nodeReader->Update();
    node = nodeReader->GetOutput();      
  }
  catch(...) {
    MITK_TEST_FAILED_MSG(<< "Could not read file for testing: " << filename);
    return NULL;
  }  

  mitk::Image::Pointer image = dynamic_cast<mitk::Image*>(node->GetData());
    
  // test by index coordinates
  mitk::Index3D index;
  mitk::FillVector3D(index, 55, 39, 50);
  MITK_TEST_OUTPUT(<< "Testing mitk::Image::GetPixelValueByIndex");
  double val = image->GetPixelValueByIndex(index);
  MITK_TEST_CONDITION_REQUIRED( mitk::Equal(val,112.22475433349609), "");
  
  //test by world coordinates
  MITK_TEST_OUTPUT(<< "Testing mitk::Image::GetPixelValueByWorldCoordinate");
  mitk::Point3D point;
  mitk::FillVector3D(point, -5.93752, 18.7199, 6.74218);
  val = image->GetPixelValueByWorldCoordinate(point);
  MITK_TEST_CONDITION_REQUIRED( mitk::Equal(val,94.456184387207031), "");

  MITK_TEST_OUTPUT(<< "Convert to index and access value by mitk::Image::GetPixelValueByIndex again");
  mitk::Index3D index2;
  image->GetGeometry()->WorldToIndex(point, index2);
  float val2 = image->GetPixelValueByIndex(index2);
  MITK_TEST_CONDITION_REQUIRED( mitk::Equal(val2,94.456184387207031), "");

  //access via itk
  MITK_TEST_OUTPUT(<< "Test conversion to itk::Image");
  typedef itk::Image<float,3> ItkFloatImage3D;
  ItkFloatImage3D::Pointer itkimage;
  mitk::CastToItkImage(image, itkimage);
  std::cout<<"[PASSED]"<<std::endl;
 
  MITK_TEST_OUTPUT(<< "Testing world->itk-physical->world consistency");
  mitk::Point3D itkPhysicalPoint;
  image->GetGeometry()->WorldToItkPhysicalPoint(point, itkPhysicalPoint);

  mitk::Point3D backTransformedPoint;
  image->GetGeometry()->ItkPhysicalPointToWorld(itkPhysicalPoint, backTransformedPoint);
  MITK_TEST_CONDITION_REQUIRED( mitk::Equal(point,backTransformedPoint), "");

  MITK_TEST_OUTPUT(<< "Compare value of pixel returned by mitk in comparison to itk");
  itk::Index<3> idx;  
  itkimage->TransformPhysicalPointToIndex(itkPhysicalPoint, idx);
  float valByItk = itkimage->GetPixel(idx);

  MITK_TEST_CONDITION_REQUIRED( mitk::Equal(valByItk,94.456184387207031), "");

  mitk::Image::Pointer cloneImage = image->Clone();
  MITK_TEST_CONDITION_REQUIRED(cloneImage->GetDimension() == image->GetDimension(), "Clone (testing dimension)");
  MITK_TEST_CONDITION_REQUIRED(cloneImage->GetPixelType() == image->GetPixelType(), "Clone (testing pixel type)");
  
  for (unsigned int i = 0u; i < cloneImage->GetDimension(); ++i)
  {
    MITK_TEST_CONDITION_REQUIRED(cloneImage->GetDimension(i) == image->GetDimension(i), "Clone (testing dimension " << i << ")");
  }

  
  MITK_TEST_END();

  return EXIT_SUCCESS;
}
Ejemplo n.º 3
0
bool mitk::CorrectorAlgorithm::modifySegment( int lineStart, int lineEnd, ipMITKSegmentationTYPE state, mitkIpPicDescriptor *pic, int* _ofsArray )
{
  // offsets for pixels right, top, left, bottom
  int nbDelta4[4];
  nbDelta4[0]=1;   nbDelta4[1]=pic->n[0];  nbDelta4[1]*=-1;  // necessary because of unsigned declaration of pic->n
  nbDelta4[2]=-1;  nbDelta4[3]=pic->n[0];

  // offsets for pixels right, top-right, top, top-left left, bottom-left, bottom, bottom-right
  int nbDelta8[8];
  nbDelta8[0] = 1;   nbDelta8[1] = nbDelta4[1]+1; nbDelta8[2] = nbDelta4[1];  nbDelta8[3] = nbDelta4[1]-1; 
  nbDelta8[4] = -1;  nbDelta8[5] = nbDelta4[3]-1; nbDelta8[6] = nbDelta4[3];  nbDelta8[7] = nbDelta4[3]+1; 
  
  ipMITKSegmentationTYPE* picdata = static_cast<ipMITKSegmentationTYPE*>(pic->data);

  ipMITKSegmentationTYPE saveStart = *(picdata + _ofsArray[lineStart]);
  ipMITKSegmentationTYPE saveEnd = *(picdata + _ofsArray[lineEnd]);
  ipMITKSegmentationTYPE newState = ((!state)&1) + 2; // probably equal to: ipMITKSegmentationTYPE newState = 3 - state; 

  // make two copies of pic:
  mitkIpPicDescriptor *seg1 = mitkIpPicClone( pic );
  mitkIpPicDescriptor *seg2 = mitkIpPicClone( pic );

  int i;

  // mark line in original
  for (i=lineStart; i<=lineEnd; i++) {
    *(picdata + _ofsArray[i]) = 3;
  }

  // mark the first side in copy 1:
  bool firstPix = true;
  bool modified;
  int line = pic->n[0];                 // #pixels in line
  int maxOfs = (int)(line * pic->n[1]); // #pixels in slice

  for (i=lineStart+1; i<lineEnd; i++) {
    do {
      modified = false;
      for (int nb=0; nb<8; nb++) {
        int nbOfs = _ofsArray[i] + nbDelta8[nb];
        if (   nbOfs < 0        // above first line
            || nbOfs >= maxOfs   // below last line
           ) continue;
        ipMITKSegmentationTYPE nbVal = *(picdata + nbOfs);
        ipMITKSegmentationTYPE destVal = *(((ipMITKSegmentationTYPE*)seg1->data) + nbOfs);
        if (nbVal!=3 && destVal!=newState) {  // get only neigbhours that are not part of the line itself
          if (firstPix) {
            *(((ipMITKSegmentationTYPE*)seg1->data) + nbOfs) = newState;  // this one is used to mark the side!
            firstPix = false;
            modified = true;
          }
          else {
            int tnb = 0;
            while (   tnb < 4 
                   && ((nbOfs + nbDelta4[tnb]) >= 0) &&  ((nbOfs + nbDelta4[tnb]) < maxOfs) 
                   && *(((ipMITKSegmentationTYPE*)seg1->data) + nbOfs + nbDelta4[tnb]) != newState
                  ) 
                  tnb++;

            if (tnb < 4 && ((nbOfs + nbDelta4[tnb]) >= 0) &&  ((nbOfs + nbDelta4[tnb]) < maxOfs) ) {
              *(((ipMITKSegmentationTYPE*)seg1->data) + nbOfs) = newState;  // we've got a buddy close
              modified = true;
            }
          }
        }
      }
    } while (modified);
  }

  // mark the other side in copy 2:
  for (i=lineStart+1; i<lineEnd; i++) {
    for (int nb=0; nb<4; nb++) {
      int nbOfs = _ofsArray[i] + nbDelta4[nb];
      if (   nbOfs < 0        // above first line
          || nbOfs >= maxOfs   // below last line
          ) continue;
      ipMITKSegmentationTYPE lineVal = *(picdata + nbOfs);
      ipMITKSegmentationTYPE side1Val = *(((ipMITKSegmentationTYPE*)seg1->data) + nbOfs);
      if (lineVal != 3 && side1Val != newState) {
        *(((ipMITKSegmentationTYPE*)seg2->data) + nbOfs) = newState;
      }
    }
  }

  // take care of line ends for multiple segments:
  *(((ipMITKSegmentationTYPE*)seg1->data) + _ofsArray[lineStart]) = newState;
  *(((ipMITKSegmentationTYPE*)seg1->data) + _ofsArray[lineEnd]) = newState;
  *(((ipMITKSegmentationTYPE*)seg2->data) + _ofsArray[lineStart]) = newState;
  *(((ipMITKSegmentationTYPE*)seg2->data) + _ofsArray[lineEnd]) = newState;


  // replace regions:
  newState = (!state)&1;
  int sizeRegion1 = 0, sizeRegion2 = 0;
  for (i=lineStart+1; i<lineEnd; i++) {
    if (*(((ipMITKSegmentationTYPE*)seg1->data) + _ofsArray[i]) != newState) {
      sizeRegion1 += ipMITKSegmentationReplaceRegion4N( seg1, _ofsArray[i], newState );
    }
    if (*(((ipMITKSegmentationTYPE*)seg2->data) + _ofsArray[i]) != newState) {
      sizeRegion2 += ipMITKSegmentationReplaceRegion4N( seg2, _ofsArray[i], newState );
    }
  }

  // combine image:
  //printf( "Size Region1 = %8i      Size Region2 = %8i\n", sizeRegion1, sizeRegion2 );
  int sizeDif;
  ipMITKSegmentationTYPE *current, *segSrc;
  if (sizeRegion1 < sizeRegion2) {
    segSrc = (ipMITKSegmentationTYPE*)seg1->data;
    sizeDif = sizeRegion2 - sizeRegion1;    
  }
  else {
    segSrc = (ipMITKSegmentationTYPE*)seg2->data;
    sizeDif = sizeRegion1 - sizeRegion2;
  }

  modified = false;
  if (sizeDif > 2*(lineEnd-lineStart)) {
    // decision is safe enough:
    ipMITKSegmentationTYPE *end = picdata + (pic->n[0]*pic->n[1]);
    for (current = picdata; current<end; current++) {
      if (*segSrc == newState) *current = newState;
      segSrc++;
    }
    modified = true;
  }

  // restore line:
  for (int i=lineStart+1; i<lineEnd; i++) {
    *(picdata + _ofsArray[i]) = state;
  }
  // restore end points:
  *(picdata + _ofsArray[lineStart]) = saveStart;
  *(picdata + _ofsArray[lineEnd]) = saveEnd;
  
  mitkIpPicFree( seg1 );
  mitkIpPicFree( seg2 );

  return modified;
}
Ejemplo n.º 4
0
mitkIpPicDescriptor *mitkIpFuncWindowR ( mitkIpPicDescriptor *pic_1,  
                                 mitkIpPicDescriptor *pic_2,
                                 mitkIpUInt4_t       *begin,
                                 mitkIpFuncFlagI_t    keep )
{
  mitkIpPicDescriptor *pic_new;            /* pointer to transformed image            */
  mitkIpInt4_t       i;                    /* loop index                              */
  mitkIpUInt4_t      end[_mitkIpPicNDIM];      /* end of image                            */
  mitkIpUInt4_t      size[_mitkIpPicNDIM];     /*                                         */

  /* check whether data are correct                                               */

  if ( _mitkIpFuncError ( pic_1 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );
  if ( _mitkIpFuncError ( pic_2 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );

  if ( pic_1->dim != pic_2->dim ) 
     {
       _mitkIpFuncSetErrno ( mitkIpFuncDIM_ERROR );
       return ( mitkIpFuncERROR );
     }
  if ( pic_1->type != pic_2->type ) 
     {
       _mitkIpFuncSetErrno ( mitkIpFuncTYPE_ERROR );
       return ( mitkIpFuncERROR );
     }
  if ( pic_1->bpe != pic_2->bpe )
     {
       _mitkIpFuncSetErrno ( mitkIpFuncSIZE_ERROR );
       return ( mitkIpFuncERROR );
     }
  
  for ( i = 0; i < pic_1->dim; i++ )
    {
      if ( begin[i] < 0 || begin[i] > pic_1->n[i] ) 
        {
           _mitkIpFuncSetErrno ( mitkIpFuncDATA_ERROR );
           return ( mitkIpFuncERROR );
        }
      end[i] = begin[i] + pic_2->n[i];
      if ( end[i] > pic_1->n[i] )
        {
           _mitkIpFuncSetErrno ( mitkIpFuncDATA_ERROR );
           return ( mitkIpFuncERROR );
        }
    }

  /* initialize vectors and variables                                             */

  size[0] = 1;
  for ( i = 1; i < _mitkIpPicNDIM; i++ )
    size[i] = size[i-1] * pic_1->n[i-1];
  size[pic_1->dim] = 0;


  for ( i = pic_1->dim; i < _mitkIpPicNDIM; i++ )
    {
       begin[i] = 0;
       end[i]   = 1;
    }

  /* allocate image structure                                                     */

  if ( keep == mitkIpFuncKeep )
     pic_new = mitkIpPicClone ( pic_1 ); 
  else 
     pic_new = pic_1;

  mitkIpPicFORALL_3 ( WINDR, pic_2, begin, end, size );

  /* Copy Tags */

  mitkIpFuncCopyTags(pic_new, pic_1);
                        
  return ( pic_new );
}