MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkOpenCVToMitkImageFilter.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found 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
23namespace mitk{
24
28
32
33 void OpenCVToMitkImageFilter::SetOpenCVMat(const cv::Mat &image)
34 {
35 m_OpenCVMatMutex.lock();
36 m_OpenCVMat = image;
37 m_OpenCVMatMutex.unlock();
38 this->Modified();
39 }
40
41 void OpenCVToMitkImageFilter::SetOpenCVImage(const IplImage* image)
42 {
43 const cv::Mat cvMat = cv::cvarrToMat(image, false);
44 this->SetOpenCVMat(cvMat);
45 }
46
48 {
49 if (m_OpenCVMat.cols != 0 && m_OpenCVMat.rows != 0 && m_OpenCVMat.data)
50 {
51 // copy current cvMat
52 m_OpenCVMatMutex.lock();
53 const cv::Mat input = m_OpenCVMat;
54 m_OpenCVMatMutex.unlock();
55 // convert cvMat to mitk::Image
56 m_ImageMutex.lock();
57 // now convert rgb image
58 if ((input.depth() >= 0) && ((unsigned int)input.depth() == CV_8S) && (input.channels() == 1))
59 {
61 }
62 else if (input.depth() == CV_8U && input.channels() == 1)
63 {
65 }
66 else if (input.depth() == CV_8U && input.channels() == 3)
67 {
69 }
70 else if (input.depth() == CV_16U && input.channels() == 1)
71 {
73 }
74 else if (input.depth() == CV_16U && input.channels() == 3)
75 {
77 }
78 else if (input.depth() == CV_32F && input.channels() == 1)
79 {
81 }
82 else if (input.depth() == CV_32F && input.channels() == 3)
83 {
85 }
86 else if (input.depth() == CV_64F && input.channels() == 1)
87 {
89 }
90 else if (input.depth() == CV_64F && input.channels() == 3)
91 {
93 }
94 else
95 {
96 MITK_WARN << "Unknown image depth and/or pixel type. Cannot convert OpenCV to MITK image.";
97 return;
98 }
99 m_ImageMutex.unlock();
100 }
101 else
102 {
103 MITK_WARN << "Cannot start filter. OpenCV Image not set.";
104 return;
105 }
106 }
107
108 ImageSource::OutputImageType* OpenCVToMitkImageFilter::GetOutput()
109 {
110 return m_Image;
111 }
112
113 /********************************************
114 * Converting from OpenCV image to ITK Image
115 *********************************************/
116 template <typename TPixel, unsigned int VImageDimension>
118 {
119 typedef itk::Image< TPixel, VImageDimension > ImageType;
120
121 typename ImageType::Pointer output = itk::OpenCVImageBridge::CVMatToITKImage<ImageType>(input);
122 Image::Pointer mitkImage = Image::New();
123 mitkImage = GrabItkImageMemory(output);
124
125 return mitkImage;
126 }
127
128
129 void OpenCVToMitkImageFilter::InsertOpenCVImageAsMitkTimeSlice(cv::Mat openCVImage, Image::Pointer mitkImage, int timeStep)
130 {
131 // convert it to an mitk::Image
132 this->SetOpenCVMat(openCVImage);
133 this->Modified();
134 this->Update();
135
136 //insert it as a timeSlice
137 mitkImage->GetGeometry(timeStep)->SetSpacing(this->GetOutput()->GetGeometry()->GetSpacing());
138 mitkImage->GetGeometry(timeStep)->SetOrigin(this->GetOutput()->GetGeometry()->GetOrigin());
139 mitkImage->GetGeometry(timeStep)->SetIndexToWorldTransform(this->GetOutput()->GetGeometry()->GetIndexToWorldTransform());
140
141 mitk::ImageReadAccessor readAccess(this->GetOutput());
142 mitkImage->SetImportVolume(readAccess.GetData(), timeStep);
143
144 mitkImage->Modified();
145 mitkImage->Update();
146
147 m_ImageMutex.lock();
148 m_Image = mitkImage;
149 m_ImageMutex.unlock();
150 }
151
152} // end namespace mitk
itk::Image< int, 3 > ImageType
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...
void SetOpenCVImage(const IplImage *image)
static Image::Pointer ConvertCVMatToMitkImage(const cv::Mat input)
IGT Exceptions.