Ejemplo n.º 1
0
void 
rich_cell::
update_texture( ImageType::Pointer image )
{
  // Compute the gradient magnitude
  GradMagFilterType::Pointer gmFilter = GradMagFilterType::New();
  gmFilter->SetInput( image );
  try {
    gmFilter->Update();
  }
  catch (itk::ExceptionObject & e) {
      std::cerr << "Exception in SubFilter: " << e << std::endl;
      exit(0);
  }
  FloatImageType::Pointer gmImage = gmFilter->GetOutput();

  // Compute the texture as the average gradient of the first portion
  // of the sorted gradient magnitudes.

  std::cout<<"class = "<<class_type_<<std::endl;
  double interior_grad = 0;
  std::vector< float > grad_magnitudes;
  grad_magnitudes.reserve(interior_points_.size());
  FloatImageType::IndexType ind;
  for (unsigned int i = 0; i<interior_points_.size(); i++) {
    ind[0] = interior_points_[i][0];
    ind[1] = interior_points_[i][1];
    ind[2] = interior_points_[i][2];
    //interior_grad += gmImage->GetPixel( ind );
    //std::cout<<interior_points_[i]<<", grad_mag = "<<gmImage->GetPixel( ind )<<std::endl;
    grad_magnitudes.push_back(gmImage->GetPixel( ind ));
  }
  
  /*
  std::vector<float>::iterator loc = grad_magnitudes.begin()+interior_points_.size()/2;
  std::nth_element(grad_magnitudes.begin(), loc, grad_magnitudes.end());
  //texture_ = interior_grad/(double)interior_points_.size();
  */
  std::vector<float>::iterator loc = grad_magnitudes.begin()+grad_magnitudes.size()/2;
  std::partial_sort(grad_magnitudes.begin(), loc, grad_magnitudes.end());
  for (std::vector<float>::iterator itr=grad_magnitudes.begin(); itr<loc; itr++) {
    interior_grad += *itr;
  }
  texture_ = interior_grad/grad_magnitudes.size()*2;
  //std::cout<<"texture = "<<texture_<<std::endl;

  /*
  image->SetRequestedRegion( bounding_box_ );
  
  int radius = 1; //for both median filtering and structuring element

  //set neighborhood
  ImageType::SizeType miIndexRadius;
  
  miIndexRadius[0] = radius; // radius along x
  miIndexRadius[1] = radius; // radius along y
  miIndexRadius[2] = 0;	// radius along z
  MedianFilterType::Pointer f_med = MedianFilterType::New();
  
  //set radius and input
  f_med->SetRadius( miIndexRadius );
  f_med->SetInput( image );
  
  OpenFilterType::Pointer f_open = OpenFilterType::New();
  ErodeFilterType::Pointer  f_erode  = ErodeFilterType::New();
  DilateFilterType::Pointer f_dilate = DilateFilterType::New();
  SubFilterType::Pointer f_sub = SubFilterType::New();
  
  StructuringElementType  structuringElement;
  structuringElement.SetRadius( radius );
  structuringElement.CreateStructuringElement();
  
  f_open->SetKernel( structuringElement );
  f_erode->SetKernel(  structuringElement );
  f_dilate->SetKernel( structuringElement );

  //connect open to medium filter
  f_open->SetInput( f_med->GetOutput() );
  f_erode->SetInput(  f_open->GetOutput() );
  f_dilate->SetInput( f_open->GetOutput() ); 

  //set inputs of substract filter for morphological gradient
  //(dilation-erosion=morphological gradient)
  f_sub->SetInput1( f_dilate->GetOutput() );
  f_sub->SetInput2( f_erode->GetOutput() );

  try
    {
      f_sub->Update();
    }
  catch (itk::ExceptionObject & e)
    {
      std::cerr << "Exception in SubFilter: " << e << std::endl;
      exit(0);
    }

  // Now we can compute the gradient values!
  ImageType::Pointer morph_image = f_sub->GetOutput();
  double interior_grad = 0;
  ImageType::IndexType ind;
  for (unsigned int i = 0; i<interior_points_.size(); i++) {
    ind[0] = interior_points_[i][0];
    ind[1] = interior_points_[i][1];
    ind[2] = interior_points_[i][2];
    interior_grad += morph_image->GetPixel( ind );
  }
  texture_ = interior_grad/(double)interior_points_.size();
  */

  /*
  double bdry_grad = 0;
  for (unsigned int i = 0; i<cell->boundary_points_.size(); i++) {
    ind[0] = cell->interior_points_[i][0];
    ind[1] = cell->interior_points_[i][1];
    ind[2] = cell->interior_points_[i][2];
    bdry_grad += morph_image->GetPixel( ind );
  }
  cell->ave_bound_gradient_ = vol_grad/(double)cell->boundary_points_.size();
  */
}
Ejemplo n.º 2
0
static void
ExtractSlicesFromImages(mitk::Image::Pointer image, mitk::Image::Pointer mask,
                        mitk::Image::Pointer maskNoNaN, mitk::Image::Pointer morphMask,
                        int direction,
                        std::vector<mitk::Image::Pointer> &imageVector,
                        std::vector<mitk::Image::Pointer> &maskVector,
                        std::vector<mitk::Image::Pointer> &maskNoNaNVector,
                        std::vector<mitk::Image::Pointer> &morphMaskVector)
{
  typedef itk::Image< double, 2 >                 FloatImage2DType;
  typedef itk::Image< unsigned short, 2 >          MaskImage2DType;

  FloatImageType::Pointer itkFloat = FloatImageType::New();
  MaskImageType::Pointer itkMask = MaskImageType::New();
  MaskImageType::Pointer itkMaskNoNaN = MaskImageType::New();
  MaskImageType::Pointer itkMorphMask = MaskImageType::New();
  mitk::CastToItkImage(mask, itkMask);
  mitk::CastToItkImage(maskNoNaN, itkMaskNoNaN);
  mitk::CastToItkImage(image, itkFloat);
  mitk::CastToItkImage(morphMask, itkMorphMask);

  int idxA, idxB, idxC;
  switch (direction)
  {
  case 0:
    idxA = 1; idxB = 2; idxC = 0;
    break;
  case 1:
    idxA = 0; idxB = 2; idxC = 1;
    break;
  case 2:
    idxA = 0; idxB = 1; idxC = 2;
    break;
  default:
    idxA = 1; idxB = 2; idxC = 0;
    break;
  }

  auto imageSize = image->GetLargestPossibleRegion().GetSize();
  FloatImageType::IndexType index3D;
  FloatImage2DType::IndexType index2D;
  FloatImage2DType::SpacingType spacing2D;
  spacing2D[0] = itkFloat->GetSpacing()[idxA];
  spacing2D[1] = itkFloat->GetSpacing()[idxB];

  for (unsigned int i = 0; i < imageSize[idxC]; ++i)
  {
    FloatImage2DType::RegionType region;
    FloatImage2DType::IndexType start;
    FloatImage2DType::SizeType size;
    start[0] = 0; start[1] = 0;
    size[0] = imageSize[idxA];
    size[1] = imageSize[idxB];
    region.SetIndex(start);
    region.SetSize(size);

    FloatImage2DType::Pointer image2D = FloatImage2DType::New();
    image2D->SetRegions(region);
    image2D->Allocate();

    MaskImage2DType::Pointer mask2D = MaskImage2DType::New();
    mask2D->SetRegions(region);
    mask2D->Allocate();

    MaskImage2DType::Pointer masnNoNaN2D = MaskImage2DType::New();
    masnNoNaN2D->SetRegions(region);
    masnNoNaN2D->Allocate();

    MaskImage2DType::Pointer morph2D = MaskImage2DType::New();
    morph2D->SetRegions(region);
    morph2D->Allocate();


    unsigned long voxelsInMask = 0;

    for (unsigned int a = 0; a < imageSize[idxA]; ++a)
    {
      for (unsigned int b = 0; b < imageSize[idxB]; ++b)
      {
        index3D[idxA] = a;
        index3D[idxB] = b;
        index3D[idxC] = i;
        index2D[0] = a;
        index2D[1] = b;
        image2D->SetPixel(index2D, itkFloat->GetPixel(index3D));
        mask2D->SetPixel(index2D, itkMask->GetPixel(index3D));
        masnNoNaN2D->SetPixel(index2D, itkMaskNoNaN->GetPixel(index3D));
        morph2D->SetPixel(index2D, itkMorphMask->GetPixel(index3D));
        voxelsInMask += (itkMask->GetPixel(index3D) > 0) ? 1 : 0;

      }
    }

    image2D->SetSpacing(spacing2D);
    mask2D->SetSpacing(spacing2D);
    masnNoNaN2D->SetSpacing(spacing2D);
    morph2D->SetSpacing(spacing2D);

    mitk::Image::Pointer tmpFloatImage = mitk::Image::New();
    tmpFloatImage->InitializeByItk(image2D.GetPointer());
    mitk::GrabItkImageMemory(image2D, tmpFloatImage);

    mitk::Image::Pointer tmpMaskImage = mitk::Image::New();
    tmpMaskImage->InitializeByItk(mask2D.GetPointer());
    mitk::GrabItkImageMemory(mask2D, tmpMaskImage);

    mitk::Image::Pointer tmpMaskNoNaNImage = mitk::Image::New();
    tmpMaskNoNaNImage->InitializeByItk(masnNoNaN2D.GetPointer());
    mitk::GrabItkImageMemory(masnNoNaN2D, tmpMaskNoNaNImage);

    mitk::Image::Pointer tmpMorphMaskImage = mitk::Image::New();
    tmpMorphMaskImage->InitializeByItk(morph2D.GetPointer());
    mitk::GrabItkImageMemory(morph2D, tmpMorphMaskImage);

    if (voxelsInMask > 0)
    {
      imageVector.push_back(tmpFloatImage);
      maskVector.push_back(tmpMaskImage);
      maskNoNaNVector.push_back(tmpMaskNoNaNImage);
      morphMaskVector.push_back(tmpMorphMaskImage);
    }
  }
}