ITK  4.13.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 #include "opencv2/imgproc/types_c.h" // CV_RGB2BGR, CV_BGR2GRAY, ...
32 #include "opencv2/imgproc/imgproc_c.h" // cvCvtColor
33 #else
34 // OpenCV 2.4.x
35 #include "cv.h"
36 #include "highgui.h"
37 #endif
38 
39 namespace itk
40 {
41 
58 {
59 public:
60 
63 
65  template<typename TOutputImageType>
66  static typename TOutputImageType::Pointer IplImageToITKImage(const IplImage* in);
67 
69  template<typename TOutputImageType>
70  static typename TOutputImageType::Pointer CVMatToITKImage(const cv::Mat & in);
71 
73  template<typename TInputImageType>
74  static IplImage* ITKImageToIplImage(const TInputImageType* in, bool force3Channels = false);
75 
77  template<typename TInputImageType>
78  static cv::Mat ITKImageToCVMat(const TInputImageType* in, bool force3Channels = false);
79 
80 private:
81  ITK_DISALLOW_COPY_AND_ASSIGN(OpenCVImageBridge);
82 
89  template< typename TOutputImageType, typename TPixel >
90  static void ITKConvertIplImageBuffer( const IplImage* in,
91  TOutputImageType* out,
92  int iDepth )
93  {
94  // Typedefs
95  typedef TOutputImageType ImageType;
96  typedef typename ImageType::PixelType OutputPixelType;
97  typedef DefaultConvertPixelTraits<OutputPixelType> ConvertPixelTraits;
99 
100  unsigned int inChannels = in->nChannels;
102 
103  // We only change current if it no longer points at in, so this is safe
104  IplImage* current = const_cast<IplImage*>(in);
105 
106  bool isVectorImage(strcmp(out->GetNameOfClass(), "VectorImage") == 0);
107 
108  bool freeCurrent = false;
109  if (inChannels == 3 && outChannels == 1)
110  {
111  current = cvCreateImage(cvSize(in->width, in->height), iDepth, 1);
112  cvCvtColor(in, current, CV_BGR2GRAY);
113  freeCurrent = true;
114  }
115  else if (inChannels == 1 && outChannels == 3)
116  {
117  current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
118  cvCvtColor(in, current, CV_GRAY2RGB);
119  freeCurrent = true;
120  }
121  else if (inChannels == 3 && outChannels == 3)
122  {
123  current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
124  cvCvtColor(in, current, CV_BGR2RGB);
125  freeCurrent = true;
126  }
127  else if (inChannels != 1 || outChannels != 1)
128  {
129  itkGenericExceptionMacro("Conversion from " << inChannels << " channels to "
130  << outChannels << " channels is not supported");
131  }
132  typename ImageType::RegionType region;
133  typename ImageType::RegionType::SizeType size;
134  typename ImageType::RegionType::IndexType start;
135  typename ImageType::SpacingType spacing;
136  size.Fill( 1 );
137  size[0] = current->width;
138  size[1] = current->height;
139  start.Fill(0);
140  spacing.Fill(1);
141  region.SetSize(size);
142  region.SetIndex(start);
143  out->SetRegions(region);
144  out->SetSpacing(spacing);
145  out->Allocate();
146  size_t lineLength = current->width*current->nChannels;
147  void* unpaddedBuffer = reinterpret_cast< void* >(
148  new TPixel[current->height*lineLength]);
149  unsigned int paddedBufPos = 0;
150  unsigned int unpaddedBufPos = 0;
151  for (int i = 0; i < current->height; ++i)
152  {
153  memcpy(&(reinterpret_cast<TPixel*>(unpaddedBuffer)[unpaddedBufPos]),
154  reinterpret_cast<TPixel*>(current->imageData + paddedBufPos),
155  lineLength*sizeof(TPixel) );
156  paddedBufPos += current->widthStep;
157  unpaddedBufPos += lineLength;
158  }
159  if (isVectorImage)
160  {
162  ::ConvertVectorImage(static_cast< TPixel* >(unpaddedBuffer),
163  current->nChannels,
164  out->GetPixelContainer()->GetBufferPointer(),
165  out->GetPixelContainer()->Size());
166  }
167  else
168  {
170  ::Convert(static_cast< TPixel* >(unpaddedBuffer),
171  current->nChannels,
172  out->GetPixelContainer()->GetBufferPointer(),
173  out->GetPixelContainer()->Size());
174  }
175  delete[] reinterpret_cast<TPixel*>(unpaddedBuffer);
176  if (freeCurrent)
177  {
178  cvReleaseImage(&current);
179  }
180  }
181 
182  template< typename TPixel, unsigned int VDimension >
184  {
185  static void Padding( const Image< TPixel, VDimension >* itkNotUsed( in ),
186  IplImage* itkNotUsed( out ) )
187  {}
188  };
189 
190  template< typename TValue, unsigned int VDimension >
191  struct HandleRGBPixel< RGBPixel< TValue >, VDimension >
192  {
193  typedef TValue ValueType;
196 
197  static void Padding( const ImageType* in, IplImage* out )
198  {
199  typename ImageType::IndexType pixelIndex = {{0,0}};
200 
201  for( int r=0;r < out->height; r++ )
202  {
203  ValueType* ptr = reinterpret_cast< ValueType* >( out->imageData + r * out->widthStep );
204  for( int c=0;c < out->width; c++ )
205  {
206  pixelIndex[0] = c;
207  pixelIndex[1] = r;
208  typename ImageType::PixelType pixel = in->GetPixel(pixelIndex);
209 
210  for( unsigned int i=0; i< 3; i++ )
211  {
212  *ptr++ = pixel[i];
213  }
214  }
215  }
216  }
217  };
218 }; // end class OpenCVImageBridge
219 
220 } // end namespace itk
221 
222 #ifndef ITK_MANUAL_INSTANTIATION
223 #include "itkOpenCVImageBridge.hxx"
224 #endif
225 
226 #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 *)
void Fill(IndexValueType value)
Definition: itkIndex.h:293
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