ITK  5.1.0
Insight Toolkit
itkOpenCVImageBridge.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 
59 {
60 public:
61  ITK_DISALLOW_COPY_AND_ASSIGN(OpenCVImageBridge);
62 
65 
67  template <typename TOutputImageType>
68  static typename TOutputImageType::Pointer
69  IplImageToITKImage(const IplImage * in);
70 
72  template <typename TOutputImageType>
73  static typename TOutputImageType::Pointer
74  CVMatToITKImage(const cv::Mat & in);
75 
77  template <typename TInputImageType>
78  static IplImage *
79  ITKImageToIplImage(const TInputImageType * in, bool force3Channels = false);
80 
82  template <typename TInputImageType>
83  static cv::Mat
84  ITKImageToCVMat(const TInputImageType * in, bool force3Channels = false);
85 
86 private:
93  template <typename TOutputImageType, typename TPixel>
94  static void
95  ITKConvertIplImageBuffer(const IplImage * in, TOutputImageType * out, int iDepth)
96  {
97  // Typedefs
98  using ImageType = TOutputImageType;
99  using OutputPixelType = typename ImageType::PixelType;
100  using ConvertPixelTraits = DefaultConvertPixelTraits<OutputPixelType>;
102 
103  unsigned int inChannels = in->nChannels;
105 
106  // We only change current if it no longer points at in, so this is safe
107  IplImage * current = const_cast<IplImage *>(in);
108 
109  bool isVectorImage(strcmp(out->GetNameOfClass(), "VectorImage") == 0);
110 
111  bool freeCurrent = false;
112  if (inChannels == 3 && outChannels == 1)
113  {
114  current = cvCreateImage(cvSize(in->width, in->height), iDepth, 1);
115  cvCvtColor(in, current, CV_BGR2GRAY);
116  freeCurrent = true;
117  }
118  else if (inChannels == 1 && outChannels == 3)
119  {
120  current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
121  cvCvtColor(in, current, CV_GRAY2RGB);
122  freeCurrent = true;
123  }
124  else if (inChannels == 3 && outChannels == 3)
125  {
126  current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
127  cvCvtColor(in, current, CV_BGR2RGB);
128  freeCurrent = true;
129  }
130  else if (inChannels != 1 || outChannels != 1)
131  {
132  itkGenericExceptionMacro("Conversion from " << inChannels << " channels to " << outChannels
133  << " channels is not supported");
134  }
135  typename ImageType::RegionType region;
136  typename ImageType::RegionType::SizeType size;
137  typename ImageType::RegionType::IndexType start;
138  typename ImageType::SpacingType spacing;
139  size.Fill(1);
140  size[0] = current->width;
141  size[1] = current->height;
142  start.Fill(0);
143  spacing.Fill(1);
144  region.SetSize(size);
145  region.SetIndex(start);
146  out->SetRegions(region);
147  out->SetSpacing(spacing);
148  out->Allocate();
149  size_t lineLength = current->width * current->nChannels;
150  void * unpaddedBuffer = reinterpret_cast<void *>(new TPixel[current->height * lineLength]);
151  unsigned int paddedBufPos = 0;
152  unsigned int unpaddedBufPos = 0;
153  for (int i = 0; i < current->height; ++i)
154  {
155  memcpy(&(reinterpret_cast<TPixel *>(unpaddedBuffer)[unpaddedBufPos]),
156  reinterpret_cast<TPixel *>(current->imageData + paddedBufPos),
157  lineLength * sizeof(TPixel));
158  paddedBufPos += current->widthStep;
159  unpaddedBufPos += lineLength;
160  }
161  if (isVectorImage)
162  {
164  static_cast<TPixel *>(unpaddedBuffer),
165  current->nChannels,
166  out->GetPixelContainer()->GetBufferPointer(),
167  out->GetPixelContainer()->Size());
168  }
169  else
170  {
172  static_cast<TPixel *>(unpaddedBuffer),
173  current->nChannels,
174  out->GetPixelContainer()->GetBufferPointer(),
175  out->GetPixelContainer()->Size());
176  }
177  delete[] reinterpret_cast<TPixel *>(unpaddedBuffer);
178  if (freeCurrent)
179  {
180  cvReleaseImage(&current);
181  }
182  }
183 
184  template <typename TPixel, unsigned int VDimension>
186  {
187  static void
188  Padding(const Image<TPixel, VDimension> * itkNotUsed(in), IplImage * itkNotUsed(out))
189  {}
190  };
191 
192  template <typename TValue, unsigned int VDimension>
193  struct HandleRGBPixel<RGBPixel<TValue>, VDimension>
194  {
195  using ValueType = TValue;
198 
199  static void
200  Padding(const ImageType * in, IplImage * out)
201  {
202  typename ImageType::IndexType pixelIndex = { { 0, 0 } };
203 
204  for (int r = 0; r < out->height; r++)
205  {
206  ValueType * ptr = reinterpret_cast<ValueType *>(out->imageData + r * out->widthStep);
207  for (int c = 0; c < out->width; c++)
208  {
209  pixelIndex[0] = c;
210  pixelIndex[1] = r;
211  typename ImageType::PixelType pixel = in->GetPixel(pixelIndex);
212 
213  for (unsigned int i = 0; i < 3; i++)
214  {
215  *ptr++ = pixel[i];
216  }
217  }
218  }
219  }
220  };
221 }; // end class OpenCVImageBridge
222 
223 } // end namespace itk
224 
225 #ifndef ITK_MANUAL_INSTANTIATION
226 # include "itkOpenCVImageBridge.hxx"
227 #endif
228 
229 #endif
itk::OpenCVImageBridge
This class provides static methods to convert between OpenCV images and itk::Image.
Definition: itkOpenCVImageBridge.h:58
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itk::OpenCVImageBridge::HandleRGBPixel< RGBPixel< TValue >, VDimension >::ValueType
TValue ValueType
Definition: itkOpenCVImageBridge.h:195
itkConvertPixelBuffer.h
itk::OpenCVImageBridge::HandleRGBPixel::Padding
static void Padding(const Image< TPixel, VDimension > *, IplImage *)
Definition: itkOpenCVImageBridge.h:188
itk::OpenCVImageBridge::IplImageToITKImage
static TOutputImageType::Pointer IplImageToITKImage(const IplImage *in)
itk::Image::GetPixel
const TPixel & GetPixel(const IndexType &index) const
Get a pixel (read only version).
Definition: itkImage.h:217
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
itk::OpenCVImageBridge::ITKImageToIplImage
static IplImage * ITKImageToIplImage(const TInputImageType *in, bool force3Channels=false)
itk::Index::Fill
void Fill(IndexValueType value)
Definition: itkIndex.h:270
itk::OpenCVImageBridge::ITKConvertIplImageBuffer
static void ITKConvertIplImageBuffer(const IplImage *in, TOutputImageType *out, int iDepth)
Definition: itkOpenCVImageBridge.h:95
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itkDefaultConvertPixelTraits.h
itk::OpenCVImageBridge::ITKImageToCVMat
static cv::Mat ITKImageToCVMat(const TInputImageType *in, bool force3Channels=false)
itk::ConvertPixelBuffer::ConvertVectorImage
static void ConvertVectorImage(InputPixelType *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
itk::DefaultConvertPixelTraits
Traits class used to by ConvertPixels to convert blocks of pixels.
Definition: itkDefaultConvertPixelTraits.h:41
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::Image::PixelType
TPixel PixelType
Definition: itkImage.h:106
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::ConvertPixelBuffer::Convert
static void Convert(InputPixelType *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
itk::OpenCVImageBridge::HandleRGBPixel< RGBPixel< TValue >, VDimension >::Padding
static void Padding(const ImageType *in, IplImage *out)
Definition: itkOpenCVImageBridge.h:200
itk::Image::IndexType
typename Superclass::IndexType IndexType
Definition: itkImage.h:132
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::OpenCVImageBridge::CVMatToITKImage
static TOutputImageType::Pointer CVMatToITKImage(const cv::Mat &in)
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itk::OpenCVImageBridge::HandleRGBPixel
Definition: itkOpenCVImageBridge.h:185