ITK  4.11.0
Insight Segmentation and Registration Toolkit
itkOpenCVImageBridge.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkOpenCVImageBridge_h
19 #define itkOpenCVImageBridge_h
20 
21 #include <string>
22 
23 #include "itkImage.h"
25 #include "itkConvertPixelBuffer.h"
26 
27 #include "opencv2/core/version.hpp"
28 #if !defined(CV_VERSION_EPOCH)
29 // OpenCV 3.x
30 #include "opencv2/core.hpp"
31 #else
32 // OpenCV 2.4.x
33 #include "cv.h"
34 #include "highgui.h"
35 #endif
36 
37 namespace itk
38 {
39 
56 {
57 public:
58 
61 
63  template<typename TOutputImageType>
64  static typename TOutputImageType::Pointer IplImageToITKImage(const IplImage* in);
65 
67  template<typename TOutputImageType>
68  static typename TOutputImageType::Pointer CVMatToITKImage(const cv::Mat & in);
69 
71  template<typename TInputImageType>
72  static IplImage* ITKImageToIplImage(const TInputImageType* in, bool force3Channels = false);
73 
75  template<typename TInputImageType>
76  static cv::Mat ITKImageToCVMat(const TInputImageType* in, bool force3Channels = false);
77 
78 private:
79  ITK_DISALLOW_COPY_AND_ASSIGN(OpenCVImageBridge);
80 
87  template< typename TOutputImageType, typename TPixel >
88  static void ITKConvertIplImageBuffer( const IplImage* in,
89  TOutputImageType* out,
90  int iDepth )
91  {
92  // Typedefs
93  typedef TOutputImageType ImageType;
94  typedef typename ImageType::PixelType OutputPixelType;
95  typedef DefaultConvertPixelTraits<OutputPixelType> ConvertPixelTraits;
97 
98  unsigned int inChannels = in->nChannels;
100 
101  // We only change current if it no longer points at in, so this is safe
102  IplImage* current = const_cast<IplImage*>(in);
103 
104  bool isVectorImage(strcmp(out->GetNameOfClass(), "VectorImage") == 0);
105 
106  bool freeCurrent = false;
107  if (inChannels == 3 && outChannels == 1)
108  {
109  current = cvCreateImage(cvSize(in->width, in->height), iDepth, 1);
110  cvCvtColor(in, current, CV_BGR2GRAY);
111  freeCurrent = true;
112  }
113  else if (inChannels == 1 && outChannels == 3)
114  {
115  current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
116  cvCvtColor(in, current, CV_GRAY2RGB);
117  freeCurrent = true;
118  }
119  else if (inChannels == 3 && outChannels == 3)
120  {
121  current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
122  cvCvtColor(in, current, CV_BGR2RGB);
123  freeCurrent = true;
124  }
125  else if (inChannels != 1 || outChannels != 1)
126  {
127  itkGenericExceptionMacro("Conversion from " << inChannels << " channels to "
128  << outChannels << " channels is not supported");
129  }
130  typename ImageType::RegionType region;
131  typename ImageType::RegionType::SizeType size;
132  typename ImageType::RegionType::IndexType start;
133  typename ImageType::SpacingType spacing;
134  size.Fill( 1 );
135  size[0] = current->width;
136  size[1] = current->height;
137  start.Fill(0);
138  spacing.Fill(1);
139  region.SetSize(size);
140  region.SetIndex(start);
141  out->SetRegions(region);
142  out->SetSpacing(spacing);
143  out->Allocate();
144  size_t lineLength = current->width*current->nChannels;
145  void* unpaddedBuffer = reinterpret_cast< void* >(
146  new TPixel[current->height*lineLength]);
147  unsigned int paddedBufPos = 0;
148  unsigned int unpaddedBufPos = 0;
149  for (int i = 0; i < current->height; ++i)
150  {
151  memcpy(&(reinterpret_cast<TPixel*>(unpaddedBuffer)[unpaddedBufPos]),
152  reinterpret_cast<TPixel*>(current->imageData + paddedBufPos),
153  lineLength*sizeof(TPixel) );
154  paddedBufPos += current->widthStep;
155  unpaddedBufPos += lineLength;
156  }
157  if (isVectorImage)
158  {
160  ::ConvertVectorImage(static_cast< TPixel* >(unpaddedBuffer),
161  current->nChannels,
162  out->GetPixelContainer()->GetBufferPointer(),
163  out->GetPixelContainer()->Size());
164  }
165  else
166  {
168  ::Convert(static_cast< TPixel* >(unpaddedBuffer),
169  current->nChannels,
170  out->GetPixelContainer()->GetBufferPointer(),
171  out->GetPixelContainer()->Size());
172  }
173  delete[] reinterpret_cast<TPixel*>(unpaddedBuffer);
174  if (freeCurrent)
175  {
176  cvReleaseImage(&current);
177  }
178  }
179 
180  template< typename TPixel, unsigned int VDimension >
182  {
183  static void Padding( const Image< TPixel, VDimension >* itkNotUsed( in ),
184  IplImage* itkNotUsed( out ) )
185  {}
186  };
187 
188  template< typename TValue, unsigned int VDimension >
189  struct HandleRGBPixel< RGBPixel< TValue >, VDimension >
190  {
191  typedef TValue ValueType;
194 
195  static void Padding( const ImageType* in, IplImage* out )
196  {
197  typename ImageType::IndexType pixelIndex = {{0,0}};
198 
199  for( int r=0;r < out->height; r++ )
200  {
201  ValueType* ptr = reinterpret_cast< ValueType* >( out->imageData + r * out->widthStep );
202  for( int c=0;c < out->width; c++ )
203  {
204  pixelIndex[0] = c;
205  pixelIndex[1] = r;
206  typename ImageType::PixelType pixel = in->GetPixel(pixelIndex);
207 
208  for( unsigned int i=0; i< 3; i++ )
209  {
210  *ptr++ = pixel[i];
211  }
212  }
213  }
214  }
215  };
216 }; // end class OpenCVImageBridge
217 
218 } // end namespace itk
219 
220 #ifndef ITK_MANUAL_INSTANTIATION
221 #include "itkOpenCVImageBridge.hxx"
222 #endif
223 
224 #endif
static IplImage * ITKImageToIplImage(const TInputImageType *in, bool force3Channels=false)
static void ITKConvertIplImageBuffer(const IplImage *in, TOutputImageType *out, int iDepth)
static cv::Mat ITKImageToCVMat(const TInputImageType *in, bool force3Channels=false)
static TOutputImageType::Pointer IplImageToITKImage(const IplImage *in)
static void ConvertVectorImage(InputPixelType *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
Traits class used to by ConvertPixels to convert blocks of pixels.
const TPixel & GetPixel(const IndexType &index) const
Get a pixel (read only version).
Definition: itkImage.h:194
This class provides static methods to convert between OpenCV images and itk::Image.
TPixel PixelType
Definition: itkImage.h:89
static void Convert(InputPixelType *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
static void Padding(const Image< TPixel, VDimension > *, IplImage *)
Superclass::IndexType IndexType
Definition: itkImage.h:119
static TOutputImageType::Pointer CVMatToITKImage(const cv::Mat &in)
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
Define additional traits for native types such as int or float.
Templated n-dimensional image class.
Definition: itkImage.h:75