Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkOpenCVToMitkImageFilter.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 <mitkITKImageImport.txx>
22 #include <itkOpenCVImageBridge.h>
23 #include <itkImageFileWriter.h>
24 
26 
27 namespace mitk{
28 
30  {
33  }
34 
36  {
37  }
38 
39  void OpenCVToMitkImageFilter::SetOpenCVMat(const cv::Mat &image)
40  {
41  m_OpenCVMatMutex->Lock();
42  m_OpenCVMat = image;
43  m_OpenCVMatMutex->Unlock();
44  this->Modified();
45  }
46 
47  void OpenCVToMitkImageFilter::SetOpenCVImage(const IplImage* image)
48  {
49  const cv::Mat cvMat = cv::Mat(image);
50  this->SetOpenCVMat(cvMat);
51  }
52 
54  {
55  if (m_OpenCVMat.cols != 0 && m_OpenCVMat.rows != 0 && m_OpenCVMat.data)
56  {
57  // copy current cvMat
58  m_OpenCVMatMutex->Lock();
59  const cv::Mat input = m_OpenCVMat;
60  m_OpenCVMatMutex->Unlock();
61  // convert cvMat to mitk::Image
62  m_ImageMutex->Lock();
63  // now convert rgb image
64  if ((input.depth() >= 0) && ((unsigned int)input.depth() == CV_8S) && (input.channels() == 1))
65  {
66  m_Image = ConvertCVMatToMitkImage< char, 2>(input);
67  }
68  else if (input.depth() == CV_8U && input.channels() == 1)
69  {
70  m_Image = ConvertCVMatToMitkImage< unsigned char, 2>(input);
71  }
72  else if (input.depth() == CV_8U && input.channels() == 3)
73  {
74  m_Image = ConvertCVMatToMitkImage< UCRGBPixelType, 2>(input);
75  }
76  else if (input.depth() == CV_16U && input.channels() == 1)
77  {
78  m_Image = ConvertCVMatToMitkImage< unsigned short, 2>(input);
79  }
80  else if (input.depth() == CV_16U && input.channels() == 3)
81  {
82  m_Image = ConvertCVMatToMitkImage< USRGBPixelType, 2>(input);
83  }
84  else if (input.depth() == CV_32F && input.channels() == 1)
85  {
86  m_Image = ConvertCVMatToMitkImage< float, 2>(input);
87  }
88  else if (input.depth() == CV_32F && input.channels() == 3)
89  {
90  m_Image = ConvertCVMatToMitkImage< FloatRGBPixelType, 2>(input);
91  }
92  else if (input.depth() == CV_64F && input.channels() == 1)
93  {
94  m_Image = ConvertCVMatToMitkImage< double, 2>(input);
95  }
96  else if (input.depth() == CV_64F && input.channels() == 3)
97  {
98  m_Image = ConvertCVMatToMitkImage< DoubleRGBPixelType, 2>(input);
99  }
100  else
101  {
102  MITK_WARN << "Unknown image depth and/or pixel type. Cannot convert OpenCV to MITK image.";
103  return;
104  }
105  //inputMutex->Unlock();
106  m_ImageMutex->Unlock();
107  }
108  else
109  {
110  MITK_WARN << "Cannot start filter. OpenCV Image not set.";
111  return;
112  }
113  }
114 
116  {
117  return m_Image;
118  }
119 
120  /********************************************
121  * Converting from OpenCV image to ITK Image
122  *********************************************/
123  template <typename TPixel, unsigned int VImageDimension>
125  {
126  typedef itk::Image< TPixel, VImageDimension > ImageType;
127 
128  typename ImageType::Pointer output = itk::OpenCVImageBridge::CVMatToITKImage<ImageType>(input);
129  Image::Pointer mitkImage = Image::New();
130  mitkImage = GrabItkImageMemory(output);
131 
132  return mitkImage;
133  }
134 
135 
136  void OpenCVToMitkImageFilter::InsertOpenCVImageAsMitkTimeSlice(cv::Mat openCVImage, Image::Pointer mitkImage, int timeStep)
137  {
138  // convert it to an mitk::Image
139  this->SetOpenCVMat(openCVImage);
140  this->Modified();
141  this->Update();
142 
143  //insert it as a timeSlice
144  mitkImage->GetGeometry(timeStep)->SetSpacing(this->GetOutput()->GetGeometry()->GetSpacing());
145  mitkImage->GetGeometry(timeStep)->SetOrigin(this->GetOutput()->GetGeometry()->GetOrigin());
146  mitkImage->GetGeometry(timeStep)->SetIndexToWorldTransform(this->GetOutput()->GetGeometry()->GetIndexToWorldTransform());
147  mitkImage->SetImportVolume(this->GetOutput()->GetData(), timeStep);
148 
149  mitkImage->Modified();
150  mitkImage->Update();
151 
152  m_ImageMutex->Lock();
153  m_Image = mitkImage;
154  m_ImageMutex->Unlock();
155  }
156 
157 } // end namespace mitk
itk::SmartPointer< Self > Pointer
void InsertOpenCVImageAsMitkTimeSlice(const cv::Mat openCVImage, Image::Pointer mitkImage, int timeStep)
Convenient method to insert an openCV image as a slice at a certain time step into a 3D or 4D mitk::I...
static void Update(vtkPolyData *)
Definition: mitkSurface.cpp:35
virtual void GenerateData() override
A version of GenerateData() specific for image processing filters.
DataCollection - Class to facilitate loading/accessing structured data.
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
itk::FastMutexLock::Pointer m_ImageMutex
T::Pointer GetData(const std::string &name)
map::core::discrete::Elements< 3 >::InternalImageType ImageType
#define MITK_WARN
Definition: mitkLogMacros.h:23
void SetOpenCVImage(const IplImage *image)
Image class for storing images.
Definition: mitkImage.h:76
static Pointer New()
itk::FastMutexLock::Pointer m_OpenCVMatMutex
static Image::Pointer ConvertCVMatToMitkImage(const cv::Mat input)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.