void setUp() override
    {
        m_ReferenceImage = mitk::IOUtil::LoadImage(GetTestDataFilePath("Pic3D.nrrd"));
        CPPUNIT_ASSERT_MESSAGE("Failed to load image for test: [Pic3D.nrrd]", m_ReferenceImage.IsNotNull());

        m_InterpolationController = mitk::SegmentationInterpolationController::GetInstance();

        // Create empty segmentation
        // Surely there must be a better way to get an image with all zeros?
        m_SegmentationImage = mitk::Image::New();
        const mitk::PixelType pixelType(mitk::MakeScalarPixelType<mitk::Tool::DefaultSegmentationDataType>());
        m_SegmentationImage->Initialize(pixelType, m_ReferenceImage->GetDimension(), m_ReferenceImage->GetDimensions());
        m_SegmentationImage->SetClonedTimeGeometry(m_ReferenceImage->GetTimeGeometry());
        unsigned int size = sizeof(mitk::Tool::DefaultSegmentationDataType);
        for (unsigned int dim = 0; dim < m_SegmentationImage->GetDimension(); ++dim)
        {
            size *= m_SegmentationImage->GetDimension(dim);
        }
        mitk::ImageWriteAccessor imageAccessor(m_SegmentationImage);
        memset(imageAccessor.GetData(), 0, size);

        // Work in the center of the image (Pic3D)
        m_CenterPoint = {{ 127, 127, 25 }};

    }
Example #2
0
static bool CompareImages(mitk::Image::Pointer mitkImage, cv::Mat openCVImage)
{
    float equal = true;
    if ((mitkImage->GetDimension(0)!=openCVImage.cols)||(mitkImage->GetDimension(1)!=openCVImage.rows))
    {
        equal = false;
    }
    for (unsigned int i=0; i<openCVImage.cols; i++)
    {
        for (unsigned int j=0; j<openCVImage.rows; j++)
        {
            mitk::Index3D currentIndex;
            currentIndex[0] = i;
            currentIndex[1] = j;
            currentIndex[2] = 0;
            float mitkImageValue = mitkImage->GetPixelValueByIndex(currentIndex);
            float openCVImageValue = openCVImage.at<float>(j,i);
            if (!mitk::Equal(mitkImageValue,openCVImageValue))
            {
                equal = false;
            }
        }
    }
    return equal;
}
Example #3
0
void mitk::NonStaticArithmeticOperation::CallExecuteTwoImageFilter(mitk::Image::Pointer imageA, mitk::Image::Pointer imageB)
{
  if (imageA->GetDimension() != imageB->GetDimension())
  {
    mitkThrow() << "Image have different dimensions. This is not supported by mitk::ArithmeticOperation";
  }

  switch (imageA->GetDimension())
  {
  case 1:
    AccessTwoImagesFixedDimensionByItk(imageA, imageB, mitk::NonStaticArithmeticOperation::ExecuteTwoImageFilter, 1);
    break;
  case 2:
    AccessTwoImagesFixedDimensionByItk(imageA, imageB, mitk::NonStaticArithmeticOperation::ExecuteTwoImageFilter, 2);
    break;
  case 3:
    AccessTwoImagesFixedDimensionByItk(imageA, imageB, mitk::NonStaticArithmeticOperation::ExecuteTwoImageFilter, 3);
    break;
  case 4:
    AccessTwoImagesFixedDimensionByItk(imageA, imageB, mitk::NonStaticArithmeticOperation::ExecuteTwoImageFilter, 4);
    break;
  default:
    mitkThrow() << "Image Dimension of "<<imageA->GetDimension() << " is not supported";
    break;
  }
}
static bool CompareImages(mitk::Image::Pointer image1, mitk::Image::Pointer image2)
{

  unsigned int dimX = image1->GetDimension(0);
  unsigned int dimY = image1->GetDimension(1);

  //make sure images have the same dimensions
  if((dimX != image1->GetDimension(0)) || (dimY != image1->GetDimension(1)))
    return false;

  //compare all pixel values
  for(unsigned int i = 0; i<dimX; i++)
  {
    for(unsigned int j = 0; j < dimY; j++)
    {
      mitk::Index3D idx;
      idx[0] = i; idx[1] = j; idx[2] = 0;
      if(!(mitk::Equal(image1->GetPixelValueByIndex(idx), image1->GetPixelValueByIndex(idx))))
      {
        return false;
      }

    }
  }

  //all pixels have identical values
  return true;
}
Example #5
0
        Attributes::Attributes(mitk::Image::Pointer mitkImage)
        {
            log_trace(Log::New, this);

            Pixel::ID pixelID;
            Component::ID componentID;
            unsigned int dimension;

            log_info("pixelType: " << mitkImage->GetPixelType().GetPixelTypeAsString());
            log_info("channels : " << mitkImage->GetPixelType().GetNumberOfComponents());
            log_info("component: " << mitkImage->GetPixelType().GetComponentTypeAsString());
            log_info("dimension: " << mitkImage->GetDimension());

            switch (mitkImage->GetPixelType().GetPixelType())
            {
                case itk::ImageIOBase::SCALAR:  pixelID = Pixel::ID::Scalar; break;
                case itk::ImageIOBase::RGB:     pixelID = Pixel::ID::Rgb; break;
                case itk::ImageIOBase::RGBA:    pixelID = Pixel::ID::Rgba; break;

                default: pixelID = Pixel::ID::Scalar; break;
            }

            switch ((itk::ImageIOBase::IOComponentType) mitkImage->GetPixelType().GetComponentType())
            {
                case itk::ImageIOBase::UCHAR:   componentID = Component::ID::UChar; break;
                case itk::ImageIOBase::USHORT:  componentID = Component::ID::UShort; break;
                case itk::ImageIOBase::UINT:    componentID = Component::ID::UInt; break;
                case itk::ImageIOBase::CHAR:    componentID = Component::ID::Char; break;
                case itk::ImageIOBase::SHORT:   componentID = Component::ID::Short; break;
                case itk::ImageIOBase::INT: 	componentID = Component::ID::Int; break;
                case itk::ImageIOBase::FLOAT:   componentID = Component::ID::Float; break;
                case itk::ImageIOBase::DOUBLE: 	componentID = Component::ID::Double; break;

                default: componentID = Component::ID::Double; break;
            }

            if (mitkImage->GetPixelType().GetNumberOfComponents() == 1)
            {
                pixelID = Pixel::ID::Scalar;
            }

            if (mitkImage->GetPixelType().GetNumberOfComponents() == 3)
            {
                pixelID = Pixel::ID::Rgb;
            }

            if (mitkImage->GetPixelType().GetNumberOfComponents() == 4)
            {
                pixelID = Pixel::ID::Rgba;
            }

            dimension = mitkImage->GetDimension();

            this->m_dimension = dimension;
            this->m_pixelID = pixelID;
            this->m_componentID = componentID;
        }
static bool CompareImages(mitk::Image::Pointer mitkImage, cv::Mat openCVImage)
{
  float equal = true;
  if (static_cast<int>(mitkImage->GetDimension(0)) != openCVImage.cols || static_cast<int>(mitkImage->GetDimension(1)) != openCVImage.rows)
  {
    equal = false;
  }
  mitk::ImagePixelReadAccessor<float,2> imageAcces(mitkImage, mitkImage->GetSliceData(0));
  for(int i=0; i<openCVImage.cols; i++)
  {
    for(int j=0; j<openCVImage.rows; j++)
    {
      itk::Index<2> currentIndex;
      currentIndex[0] = i;
      currentIndex[1] = j;
      float mitkImageValue = imageAcces.GetPixelByIndex(currentIndex);
      float openCVImageValue = openCVImage.at<float>(j,i);
      if (!mitk::Equal(mitkImageValue,openCVImageValue))
      {
        equal = false;
      }
    }
  }
  return equal;
}
static bool CompareImages(mitk::Image::Pointer image1, mitk::Image::Pointer image2)
{
  //check if epsilon is exceeded
  unsigned int sliceDimension = image1->GetDimension(0)*image1->GetDimension(1);
  bool picturesEqual = true;

  float* floatArray1 = (float*)image1->GetSliceData(0, 0, 0)->GetData();
  float* floatArray2 = (float*)image2->GetSliceData(0, 0, 0)->GetData();
  for(unsigned int i = 0; i < sliceDimension; i++)
  {
    if(!(mitk::Equal(floatArray1[i], floatArray2[i])))
    {
      picturesEqual = false;
    }
  }
  return picturesEqual;
}
Example #8
0
bool CompareImageMetaData( mitk::Image::Pointer image, mitk::Image::Pointer reference)
{
  // switch to AreIdentical() methods as soon as Bug 11925 (Basic comparison operators) is fixed

  if( image->GetDimension() != reference->GetDimension() )
  {
    MITK_ERROR << "The image dimension differs: IN (" << image->GetDimension() << ") REF(" << reference->GetDimension() << ")";
    return false;
  }

  // pixel type
  if( image->GetPixelType() != reference->GetPixelType()
      && image->GetPixelType().GetBitsPerComponent() != reference->GetPixelType().GetBitsPerComponent() )
  {
    MITK_ERROR << "Pixeltype differs ";
    return false;
  }

  return true;
}
mitk::Image::Pointer mitk::AutoSegmentationTool::Get3DImage(mitk::Image::Pointer image, unsigned int timestep)
{
  if (image->GetDimension() != 4)
    return image;

  mitk::ImageTimeSelector::Pointer imageTimeSelector = mitk::ImageTimeSelector::New();

  imageTimeSelector->SetInput(image);
  imageTimeSelector->SetTimeNr(static_cast<int>(timestep));

  imageTimeSelector->UpdateLargestPossibleRegion();

  return imageTimeSelector->GetOutput();
}
Example #10
0
void ReadPixel(mitk::PixelType, mitk::Image::Pointer image, itk::Index<3> indexPoint, double& value)
{
  if (image->GetDimension() == 2)
  {
    mitk::ImagePixelReadAccessor<PixelType,2> readAccess(image, image->GetSliceData(0));
    itk::Index<2> idx;
    idx[0] = indexPoint[0];
    idx[1] = indexPoint[1];
    value = readAccess.GetPixelByIndex(idx);
  }
  else if (image->GetDimension() == 3)
  {
    mitk::ImagePixelReadAccessor<PixelType,3> readAccess(image, image->GetVolumeData(0));
    itk::Index<3> idx;
    idx[0] = indexPoint[0];
    idx[1] = indexPoint[1];
    idx[2] = indexPoint[2];
    value = readAccess.GetPixelByIndex(idx);
  }
  else
  {
    //unhandled
  }
}
Float4DImageType::Pointer QmitkTbssSkeletonizationView::ConvertToItk(mitk::Image::Pointer image)
{

  Float4DImageType::Pointer output = Float4DImageType::New();

  mitk::Geometry3D* geo = image->GetGeometry();
  mitk::Vector3D mitkSpacing = geo->GetSpacing();
  mitk::Point3D mitkOrigin = geo->GetOrigin();

  Float4DImageType::SpacingType spacing;
  spacing[0] = mitkSpacing[0];
  spacing[1] = mitkSpacing[1];
  spacing[2] = mitkSpacing[2];
  spacing[3] = 1.0; // todo: check if spacing has length 4

  Float4DImageType::PointType origin;
  origin[0] = mitkOrigin[0];
  origin[1] = mitkOrigin[1];
  origin[2] = mitkOrigin[2];
  origin[3] = 0;

  Float4DImageType::SizeType size;
  size[0] = image->GetDimension(0);
  size[1] = image->GetDimension(1);
  size[2] = image->GetDimension(2);
  size[3] = image->GetDimension(3);

  Float4DImageType::DirectionType dir;
  vtkLinearTransform* lin = geo->GetVtkTransform();
  vtkMatrix4x4 *m = lin->GetMatrix();

  dir.Fill(0.0);
  for(int x=0; x<3; x++)
  {
    for(int y=0; y<3; y++)
    {
      dir[x][y] = m->GetElement(x,y);
    }
  }
  dir[3][3] = 1;



  output->SetSpacing(spacing);
  output->SetOrigin(origin);
  output->SetRegions(size);
  output->SetDirection(dir);
  output->Allocate();


  if(image->GetDimension() == 4)
  {
    int timesteps = image->GetDimension(3);

    try{
      // REPLACE THIS METHODE()ConvertToItk) WITH mitk::CastToItk

      // iterate through the subjects and copy data to output
      for(int t=0; t<timesteps; t++)
      {
        for(int x=0; x<image->GetDimension(0); x++)
        {
          for(int y=0; y<image->GetDimension(1); y++)
          {
            for(int z=0; z<image->GetDimension(2); z++)
            {
              itk::Index<3> ix = {x, y, z};
              itk::Index<4> ix4 = {x, y, z, t};

              output->SetPixel(ix4, image->GetPixelValueByIndex(ix, t));

            }
          }
        }
      }
    }
    catch(std::exception & e)
    {
      MITK_INFO << e.what();
    }

  }


  return output;
}
static bool ApplyTemporalMedianFilter(mitk::Image::Pointer& image, ItkImageType_2D::Pointer& itkImage2D)
{

  //initialize ITK output image
  unsigned int dimX = image->GetDimension(0);
  unsigned int dimY = image->GetDimension(1);
  unsigned int nbSlices = image->GetDimension(2);

  ItkImageType_2D::SizeType size;
  size[0] = dimX;
  size[1] = dimY;

  ItkImageType_2D::RegionType region;
  region.SetSize(size);

  ItkImageType_2D::SpacingType spacing;
  spacing[0] = 1.0;
  spacing[1] = 1.0;

  itkImage2D->SetRegions( region );
  itkImage2D->SetSpacing ( spacing );
  itkImage2D->Allocate();

  //initialize median filtering
  std::vector<ToFScalarType> allDistances;
  mitk::Index3D curIdx3D;
  ItkImageType_2D::IndexType curIdx2D;

  //compute median over time for each (x,y)
  for(unsigned int i = 0; i<dimX; i++)
  {
    for(unsigned int j = 0; j < dimY; j++)
    {
      allDistances.clear();

      curIdx3D[0] = i; curIdx3D[1] = j;
      curIdx2D[0] = i; curIdx2D[1] = j;
      //gather all distances for one pixel
      for(unsigned int k = 0; k < nbSlices; k++)
      {
        curIdx3D[2] = k;
        allDistances.push_back(image->GetPixelValueByIndex(curIdx3D));
      }

      //sort distances and compute median
      std::sort(allDistances.begin(),allDistances.end());
      unsigned int median_idx = nbSlices/2;
      if(nbSlices%2 == 1) //i.e., there is an odd number of slices
      {
        itkImage2D->SetPixel(curIdx2D,allDistances[median_idx]);
      } else
      {
        ToFScalarType upper = allDistances[median_idx];
        ToFScalarType lower = allDistances[median_idx+1];
        itkImage2D->SetPixel(curIdx2D,(upper+lower)/2.0);
      }
    }
  }
  return true;

}