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"
67 template <
typename TOutputImageType>
68 static typename TOutputImageType::Pointer
72 template <
typename TOutputImageType>
73 static typename TOutputImageType::Pointer
77 template <
typename TInputImageType>
82 template <
typename TInputImageType>
84 ITKImageToCVMat(
const TInputImageType * in,
bool force3Channels =
false);
93 template <
typename TOutputImageType,
typename TPixel>
98 using ImageType = TOutputImageType;
99 using OutputPixelType =
typename ImageType::PixelType;
102 unsigned int inChannels = in->nChannels;
106 if ((inChannels != outChannels || (inChannels == 3 && outChannels == 3)) &&
107 (iDepth == IPL_DEPTH_8S || iDepth == IPL_DEPTH_16S || iDepth == IPL_DEPTH_32S || iDepth == IPL_DEPTH_64F))
109 itkGenericExceptionMacro(
"OpenCV IplImage to ITK Image conversion - the necessary color"
110 " conversion is not supported for the input OpenCV pixel type");
114 checkMatchingTypes<TPixel, OutputPixelType>(outChannels);
117 IplImage * current = const_cast<IplImage *>(in);
119 bool freeCurrent =
false;
120 if (inChannels == 3 && outChannels == 1)
122 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 1);
123 cvCvtColor(in, current, CV_BGR2GRAY);
126 else if (inChannels == 1 && outChannels == 3)
128 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
129 cvCvtColor(in, current, CV_GRAY2RGB);
132 else if (inChannels == 3 && outChannels == 3)
134 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
135 cvCvtColor(in, current, CV_BGR2RGB);
138 else if (inChannels != 1 || outChannels != 1)
140 itkGenericExceptionMacro(
"Conversion from " << inChannels <<
" channels to " << outChannels
141 <<
" channels is not supported");
144 ITKConvertImageBuffer<TOutputImageType, TPixel>(
145 current->imageData, out, current->nChannels, current->width, current->height, current->widthStep);
149 cvReleaseImage(¤t);
153 template <
typename TOutputImageType,
typename TPixel>
160 using ImageType = TOutputImageType;
161 using OutputPixelType =
typename ImageType::PixelType;
163 unsigned int inChannels = in.channels();
164 unsigned int iDepth = in.depth();
168 if ((inChannels != outChannels || (inChannels == 3 && outChannels == 3)) &&
169 (iDepth == CV_8S || iDepth == CV_16S || iDepth == CV_32S || iDepth == CV_64F))
171 itkGenericExceptionMacro(
"OpenCV Mat to ITK Image conversion - the necessary color"
172 " conversion is not supported for the input OpenCV pixel type");
176 checkMatchingTypes<TPixel, OutputPixelType>(outChannels);
180 if (inChannels == 3 && outChannels == 1)
182 cvtColor(in, current, COLOR_BGR2GRAY);
184 else if (inChannels == 1 && outChannels == 3)
186 cvtColor(in, current, COLOR_GRAY2RGB);
188 else if (inChannels == 3 && outChannels == 3)
190 cvtColor(in, current, COLOR_BGR2RGB);
192 else if (inChannels != 1 || outChannels != 1)
194 itkGenericExceptionMacro(
"Conversion from " << inChannels <<
" channels to " << outChannels
195 <<
" channels is not supported");
202 ITKConvertImageBuffer<TOutputImageType, TPixel>(
203 reinterpret_cast<char *>(current.ptr()), out, current.channels(), current.cols, current.rows, current.step);
206 template <
typename InputPixelType,
typename OutputPixelType>
210 if (outChannels == 3)
214 itkGenericExceptionMacro(
"OpenCV to ITK conversion channel component type mismatch");
217 else if (
typeid(InputPixelType) !=
typeid(OutputPixelType))
219 itkGenericExceptionMacro(
"OpenCV to ITK conversion pixel type mismatch");
223 template <
typename TOutputImageType,
typename TPixel>
226 TOutputImageType * out,
227 unsigned int inChannels,
232 using ImageType = TOutputImageType;
233 using OutputPixelType =
typename ImageType::PixelType;
236 bool isVectorImage(strcmp(out->GetNameOfClass(),
"VectorImage") == 0);
241 typename ImageType::SpacingType spacing;
247 region.SetSize(size);
248 region.SetIndex(start);
249 out->SetRegions(region);
250 out->SetSpacing(spacing);
252 size_t lineLength = imgWidth * inChannels *
sizeof(TPixel);
253 void * unpaddedBuffer = reinterpret_cast<void *>(
new TPixel[imgHeight * lineLength]);
254 unsigned int paddedBufPos = 0;
255 unsigned int unpaddedBufPos = 0;
257 for (
int i = 0; i < imgHeight; ++i)
259 memcpy(&(reinterpret_cast<char *>(unpaddedBuffer)[unpaddedBufPos]), in + paddedBufPos, lineLength);
260 paddedBufPos += widthStep;
261 unpaddedBufPos += lineLength;
267 static_cast<TPixel *>(unpaddedBuffer),
269 out->GetPixelContainer()->GetBufferPointer(),
270 out->GetPixelContainer()->Size());
275 static_cast<TPixel *>(unpaddedBuffer),
277 out->GetPixelContainer()->GetBufferPointer(),
278 out->GetPixelContainer()->Size());
281 delete[] reinterpret_cast<TPixel *>(unpaddedBuffer);
284 template <
typename TPixel,
unsigned int VDimension>
292 template <
typename TValue,
unsigned int VDimension>
304 for (
int r = 0; r < out->height; r++)
306 ValueType * ptr = reinterpret_cast<ValueType *>(out->imageData + r * out->widthStep);
307 for (
int c = 0; c < out->width; c++)
313 for (
unsigned int i = 0; i < 3; i++)
325 #ifndef ITK_MANUAL_INSTANTIATION
326 # include "itkOpenCVImageBridge.hxx"