18#ifndef itkOpenCVImageBridge_h
19#define itkOpenCVImageBridge_h
27#include "opencv2/core/version.hpp"
28#if !defined(CV_VERSION_EPOCH)
30# include "opencv2/imgproc.hpp"
31# include "opencv2/imgproc/types_c.h"
32# include "opencv2/imgproc/imgproc_c.h"
66 template <
typename TOutputImageType>
67 static typename TOutputImageType::Pointer
71 template <
typename TOutputImageType>
72 static typename TOutputImageType::Pointer
76 template <
typename TInputImageType>
81 template <
typename TInputImageType>
92 template <
typename TOutputImageType,
typename TPixel>
97 using ImageType = TOutputImageType;
98 using OutputPixelType =
typename ImageType::PixelType;
100 unsigned int inChannels = in->nChannels;
104 if ((inChannels != outChannels || (inChannels == 3 && outChannels == 3)) &&
105 (iDepth == IPL_DEPTH_8S || iDepth == IPL_DEPTH_16S || iDepth == IPL_DEPTH_32S || iDepth == IPL_DEPTH_64F))
107 itkGenericExceptionMacro(
"OpenCV IplImage to ITK Image conversion - the necessary color conversion is not "
108 "supported for the input OpenCV pixel type");
115 auto * current =
const_cast<IplImage *
>(in);
117 bool freeCurrent =
false;
118 if (inChannels == 3 && outChannels == 1)
120 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 1);
121 cvCvtColor(in, current, CV_BGR2GRAY);
124 else if (inChannels == 1 && outChannels == 3)
126 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
127 cvCvtColor(in, current, CV_GRAY2RGB);
130 else if (inChannels == 3 && outChannels == 3)
132 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
133 cvCvtColor(in, current, CV_BGR2RGB);
136 else if (inChannels != 1 || outChannels != 1)
138 itkGenericExceptionMacro(
"Conversion from " << inChannels <<
" channels to " << outChannels
139 <<
" channels is not supported");
143 current->imageData, out, current->nChannels, current->width, current->height, current->widthStep);
147 cvReleaseImage(¤t);
151 template <
typename TOutputImageType,
typename TPixel>
158 using ImageType = TOutputImageType;
159 using OutputPixelType =
typename ImageType::PixelType;
161 unsigned int inChannels = in.channels();
162 unsigned int iDepth = in.depth();
166 if ((inChannels != outChannels || (inChannels == 3 && outChannels == 3)) &&
167 (iDepth == CV_8S || iDepth == CV_16S || iDepth == CV_32S || iDepth == CV_64F))
169 itkGenericExceptionMacro(
"OpenCV Mat to ITK Image conversion - the necessary color conversion is not supported "
170 "for the input OpenCV pixel type");
178 if (inChannels == 3 && outChannels == 1)
180 cvtColor(in, current, COLOR_BGR2GRAY);
182 else if (inChannels == 1 && outChannels == 3)
184 cvtColor(in, current, COLOR_GRAY2RGB);
186 else if (inChannels == 3 && outChannels == 3)
188 cvtColor(in, current, COLOR_BGR2RGB);
190 else if (inChannels != 1 || outChannels != 1)
192 itkGenericExceptionMacro(
"Conversion from " << inChannels <<
" channels to " << outChannels
193 <<
" channels is not supported");
201 reinterpret_cast<char *
>(current.ptr()), out, current.channels(), current.cols, current.rows, current.step);
204 template <
typename InputPixelType,
typename OutputPixelType>
208 if (outChannels == 3)
212 itkGenericExceptionMacro(
"OpenCV to ITK conversion channel component type mismatch");
215 else if (
typeid(InputPixelType) !=
typeid(OutputPixelType))
217 itkGenericExceptionMacro(
"OpenCV to ITK conversion pixel type mismatch");
221 template <
typename TOutputImageType,
typename TPixel>
224 TOutputImageType * out,
225 unsigned int inChannels,
230 using ImageType = TOutputImageType;
231 using OutputPixelType =
typename ImageType::PixelType;
234 bool isVectorImage(strcmp(out->GetNameOfClass(),
"VectorImage") == 0);
235 typename ImageType::RegionType::SizeType size;
239 typename ImageType::RegionType::IndexType start{};
240 typename ImageType::RegionType region = { start, size };
241 out->SetRegions(region);
242 typename ImageType::SpacingType spacing;
244 out->SetSpacing(spacing);
246 size_t lineLength = imgWidth * inChannels *
sizeof(TPixel);
247 auto * unpaddedBuffer =
reinterpret_cast<void *
>(
new TPixel[imgHeight * lineLength]);
248 unsigned int paddedBufPos = 0;
249 unsigned int unpaddedBufPos = 0;
251 for (
int i = 0; i < imgHeight; ++i)
253 memcpy(&(
reinterpret_cast<char *
>(unpaddedBuffer)[unpaddedBufPos]), in + paddedBufPos, lineLength);
254 paddedBufPos += widthStep;
255 unpaddedBufPos += lineLength;
261 static_cast<TPixel *
>(unpaddedBuffer),
263 out->GetPixelContainer()->GetBufferPointer(),
264 out->GetPixelContainer()->Size());
269 static_cast<TPixel *
>(unpaddedBuffer),
271 out->GetPixelContainer()->GetBufferPointer(),
272 out->GetPixelContainer()->Size());
275 delete[]
reinterpret_cast<TPixel *
>(unpaddedBuffer);
278 template <
typename TPixel,
unsigned int VDimension>
286 template <
typename TValue,
unsigned int VDimension>
298 for (
int r = 0; r < out->height; ++r)
300 auto * ptr =
reinterpret_cast<ValueType *
>(out->imageData + r * out->widthStep);
301 for (
int c = 0; c < out->width; ++c)
307 for (
unsigned int i = 0; i < 3; ++i)
319#ifndef ITK_MANUAL_INSTANTIATION
320# include "itkOpenCVImageBridge.hxx"
static void ConvertVectorImage(const InputPixelType *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
static void Convert(const InputPixelType *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
Traits class used to by ConvertPixels to convert blocks of pixels.
static constexpr unsigned int Dimension
Templated n-dimensional image class.
const TPixel & GetPixel(const IndexType &index) const
Get a pixel (read only version).
Index< VImageDimension > IndexType
This class provides static methods to convert between OpenCV images and itk::Image.
static TOutputImageType::Pointer IplImageToITKImage(const IplImage *in)
static IplImage * ITKImageToIplImage(const TInputImageType *in, bool force3Channels=false)
static void ITKConvertIplImageBuffer(const IplImage *in, TOutputImageType *out, unsigned int iDepth)
static cv::Mat ITKImageToCVMat(const TInputImageType *in, bool force3Channels=false)
static TOutputImageType::Pointer CVMatToITKImage(const cv::Mat &in)
static void ITKConvertMatImageBuffer(const cv::Mat &in, TOutputImageType *out)
static void ITKConvertImageBuffer(const char *in, TOutputImageType *out, unsigned int inChannels, int imgWidth, int imgHeight, int widthStep)
static void checkMatchingTypes(unsigned int outChannels)
Represent Red, Green and Blue components for color images.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Image< PixelType, VDimension > ImageType
static void Padding(const ImageType *in, IplImage *out)
RGBPixel< ValueType > PixelType
static void Padding(const Image< TPixel, VDimension > *in, IplImage *out)