Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 #include <itkImportImageFilter.h>
16 #include <itkRGBPixel.h>
17 #include <mitkITKImageImport.txx>
18 #include <itkOpenCVImageBridge.h>
19 #include <itkImageFileWriter.h>
20 
22 
23 namespace mitk{
24 
26  {
27  m_ImageMutex = itk::FastMutexLock::New();
28  m_OpenCVMatMutex = itk::FastMutexLock::New();
29  }
30 
32  {
33  }
34 
36  {
37  m_OpenCVMatMutex->Lock();
39  m_OpenCVMatMutex->Unlock();
40  this->Modified();
41  }
42 
44  {
45  const cv::Mat cvMat = cv::cvarrToMat(image, false);
46  this->SetOpenCVMat(cvMat);
47  }
48 
50  {
51  if (m_OpenCVMat.cols != 0 && m_OpenCVMat.rows != 0 && m_OpenCVMat.data)
52  {
53  // copy current cvMat
54  m_OpenCVMatMutex->Lock();
55  const cv::Mat input = m_OpenCVMat;
56  m_OpenCVMatMutex->Unlock();
57  // convert cvMat to mitk::Image
58  m_ImageMutex->Lock();
59  // now convert rgb image
60  if ((input.depth() >= 0) && ((unsigned int)input.depth() == CV_8S) && (input.channels() == 1))
61  {
62  m_Image = ConvertCVMatToMitkImage< char, 2>(input);
63  }
64  else if (input.depth() == CV_8U && input.channels() == 1)
65  {
66  m_Image = ConvertCVMatToMitkImage< unsigned char, 2>(input);
67  }
68  else if (input.depth() == CV_8U && input.channels() == 3)
69  {
70  m_Image = ConvertCVMatToMitkImage< UCRGBPixelType, 2>(input);
71  }
72  else if (input.depth() == CV_16U && input.channels() == 1)
73  {
74  m_Image = ConvertCVMatToMitkImage< unsigned short, 2>(input);
75  }
76  else if (input.depth() == CV_16U && input.channels() == 3)
77  {
78  m_Image = ConvertCVMatToMitkImage< USRGBPixelType, 2>(input);
79  }
80  else if (input.depth() == CV_32F && input.channels() == 1)
81  {
82  m_Image = ConvertCVMatToMitkImage< float, 2>(input);
83  }
84  else if (input.depth() == CV_32F && input.channels() == 3)
85  {
86  m_Image = ConvertCVMatToMitkImage< FloatRGBPixelType, 2>(input);
87  }
88  else if (input.depth() == CV_64F && input.channels() == 1)
89  {
90  m_Image = ConvertCVMatToMitkImage< double, 2>(input);
91  }
92  else if (input.depth() == CV_64F && input.channels() == 3)
93  {
94  m_Image = ConvertCVMatToMitkImage< DoubleRGBPixelType, 2>(input);
95  }
96  else
97  {
98  MITK_WARN << "Unknown image depth and/or pixel type. Cannot convert OpenCV to MITK image.";
99  return;
100  }
101  //inputMutex->Unlock();
102  m_ImageMutex->Unlock();
103  }
104  else
105  {
106  MITK_WARN << "Cannot start filter. OpenCV Image not set.";
107  return;
108  }
109  }
110 
112  {
113  return m_Image;
114  }
115 
116  /********************************************
117  * Converting from OpenCV image to ITK Image
118  *********************************************/
119  template <typename TPixel, unsigned int VImageDimension>
121  {
122  typedef itk::Image< TPixel, VImageDimension > ImageType;
123 
124  typename ImageType::Pointer output = itk::OpenCVImageBridge::CVMatToITKImage<ImageType>(input);
125  Image::Pointer mitkImage = Image::New();
126  mitkImage = GrabItkImageMemory(output);
127 
128  return mitkImage;
129  }
130 
131 
132  void OpenCVToMitkImageFilter::InsertOpenCVImageAsMitkTimeSlice(cv::Mat openCVImage, Image::Pointer mitkImage, int timeStep)
133  {
134  // convert it to an mitk::Image
135  this->SetOpenCVMat(openCVImage);
136  this->Modified();
137  this->Update();
138 
139  //insert it as a timeSlice
140  mitkImage->GetGeometry(timeStep)->SetSpacing(this->GetOutput()->GetGeometry()->GetSpacing());
141  mitkImage->GetGeometry(timeStep)->SetOrigin(this->GetOutput()->GetGeometry()->GetOrigin());
142  mitkImage->GetGeometry(timeStep)->SetIndexToWorldTransform(this->GetOutput()->GetGeometry()->GetIndexToWorldTransform());
143 
144  mitk::ImageReadAccessor readAccess(this->GetOutput());
145  mitkImage->SetImportVolume(readAccess.GetData(), timeStep);
146 
147  mitkImage->Modified();
148  mitkImage->Update();
149 
150  m_ImageMutex->Lock();
151  m_Image = mitkImage;
152  m_ImageMutex->Unlock();
153  }
154 
155 } // end namespace mitk
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...
itk::Image< unsigned char, 3 > ImageType
static void Update(vtkPolyData *)
Definition: mitkSurface.cpp:31
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
#define MITK_WARN
Definition: mitkLogMacros.h:19
void SetOpenCVImage(const IplImage *image)
Image class for storing images.
Definition: mitkImage.h:72
mitk::Image::Pointer image
static Pointer New()
itk::FastMutexLock::Pointer m_OpenCVMatMutex
static Image::Pointer ConvertCVMatToMitkImage(const cv::Mat input)
ImageReadAccessor class to get locked read access for a particular image part.
const void * GetData() const
Gives const access to the data.