Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkImageToOpenCVImageFilter.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 #include <itkImportImageFilter.h>
20 #include <itkRGBPixel.h>
21 #include <mitkImageAccessByItk.h>
22 
23 namespace mitk{
24 
26  : m_OpenCVImage(nullptr)
27  {
28  m_sliceSelector = ImageSliceSelector::New();
29  }
30 
32  {
33  m_OpenCVImage = nullptr;
34  }
35 
36 
38  {
39  m_Image = _Image;
40  }
41 
42 
44  {
45  if(image == nullptr)
46  {
47  MITK_WARN << "MITK Image is 0";
48  return false;
49  }
50  if(image->GetDimension() > 2 )
51  {
52  MITK_WARN << "Only 2D Images allowed";
53  return false;
54  }
55  return true;
56  }
57 
59  {
60 
61  if(!this->CheckImage( m_Image ))
62  return nullptr;
63 
64  m_OpenCVImage = (nullptr);
65 
66  try
67  {
71  (2) // dimensions
72  )
73  }
74  catch (const AccessByItkException& e) {
75  std::cout << "Caught exception [from AccessFixedTypeByItk]: \n" << e.what() << "\n";
76  return nullptr;
77  }
78  return m_OpenCVImage;
79  }
80 
82  {
83  IplImage* img = this->GetOpenCVImage();
84 
85  cv::Mat mat;
86  if( img )
87  {
88  // do not copy data, then release just the header
89  mat = cv::Mat ( img, false );
90  cvReleaseImageHeader( &img );
91  }
92 
93  return mat;
94  }
95 
96  template<typename TPixel, unsigned int VImageDimension>
97  void ImageToOpenCVImageFilter::ItkImageProcessing( itk::Image<TPixel,VImageDimension>* image )
98  {
99  m_OpenCVImage = itk::OpenCVImageBridge::ITKImageToIplImage(image);
100  }
101 
102  void ImageToOpenCVImageFilter::SetInputFromTimeSlice(Image::Pointer mitkImage, int timeStep, int slice)
103  {
104  m_sliceSelector->SetInput(mitkImage);
105  m_sliceSelector->SetSliceNr(slice);
106  m_sliceSelector->SetTimeNr(timeStep);
107  m_sliceSelector->Update();
108  this->SetImage(m_sliceSelector->GetOutput());
109  }
110 
111 } // end namespace mitk
void ItkImageProcessing(itk::Image< TPixel, VImageDimension > *image)
#define MITK_ACCESSBYITK_PIXEL_TYPES_SEQ
Definition: mitkConfig.h:25
DataCollection - Class to facilitate loading/accessing structured data.
mitk::WeakPointer< mitk::Image > m_Image
void SetInputFromTimeSlice(Image::Pointer mitkImage, int timeStep, int slice)
Convenient method to set a certain slice of a 3D or 4D mitk::Image as input to convert it to an openC...
static Pointer New()
bool CheckImage(mitk::Image *image)
get the input MITK image
void SetImage(mitk::Image *_Image)
set the input MITK image
itk::RGBPixel< unsigned short > USRGBPixelType
#define AccessFixedTypeByItk(mitkImage, itkImageTypeFunction, pixelTypeSeq, dimSeq)
Access a mitk-image with known type (pixel type and dimension) by an itk-image.
#define MITK_WARN
Definition: mitkLogMacros.h:23
Image class for storing images.
Definition: mitkImage.h:76
Exception class thrown in AccessByItk macros.
unsigned int GetDimension() const
Get dimension of the image.
Definition: mitkImage.cpp:110
itk::RGBPixel< unsigned char > UCRGBPixelType