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>
71 template <
typename TOutputImageType>
76 template <
typename TInputImageType>
81 template <
typename TInputImageType>
83 ITKImageToCVMat(
const TInputImageType * in,
bool force3Channels =
false);
92 template <
typename TOutputImageType,
typename TPixel>
97 using ImageType = TOutputImageType;
98 using OutputPixelType =
typename ImageType::PixelType;
101 unsigned int inChannels = in->nChannels;
105 if ((inChannels != outChannels || (inChannels == 3 && outChannels == 3)) &&
106 (iDepth == IPL_DEPTH_8S || iDepth == IPL_DEPTH_16S || iDepth == IPL_DEPTH_32S || iDepth == IPL_DEPTH_64F))
108 itkGenericExceptionMacro(
"OpenCV IplImage to ITK Image conversion - the necessary color"
109 " conversion is not supported for the input OpenCV pixel type");
113 checkMatchingTypes<TPixel, OutputPixelType>(outChannels);
116 auto * current = const_cast<IplImage *>(in);
118 bool freeCurrent =
false;
119 if (inChannels == 3 && outChannels == 1)
121 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 1);
122 cvCvtColor(in, current, CV_BGR2GRAY);
125 else if (inChannels == 1 && outChannels == 3)
127 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
128 cvCvtColor(in, current, CV_GRAY2RGB);
131 else if (inChannels == 3 && outChannels == 3)
133 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
134 cvCvtColor(in, current, CV_BGR2RGB);
137 else if (inChannels != 1 || outChannels != 1)
139 itkGenericExceptionMacro(
"Conversion from " << inChannels <<
" channels to " << outChannels
140 <<
" channels is not supported");
143 ITKConvertImageBuffer<TOutputImageType, TPixel>(
144 current->imageData, out, current->nChannels, current->width, current->height, current->widthStep);
148 cvReleaseImage(¤t);
152 template <
typename TOutputImageType,
typename TPixel>
159 using ImageType = TOutputImageType;
160 using OutputPixelType =
typename ImageType::PixelType;
162 unsigned int inChannels = in.channels();
163 unsigned int iDepth = in.depth();
167 if ((inChannels != outChannels || (inChannels == 3 && outChannels == 3)) &&
168 (iDepth == CV_8S || iDepth == CV_16S || iDepth == CV_32S || iDepth == CV_64F))
170 itkGenericExceptionMacro(
"OpenCV Mat to ITK Image conversion - the necessary color"
171 " conversion is not supported for the input OpenCV pixel type");
175 checkMatchingTypes<TPixel, OutputPixelType>(outChannels);
179 if (inChannels == 3 && outChannels == 1)
181 cvtColor(in, current, COLOR_BGR2GRAY);
183 else if (inChannels == 1 && outChannels == 3)
185 cvtColor(in, current, COLOR_GRAY2RGB);
187 else if (inChannels == 3 && outChannels == 3)
189 cvtColor(in, current, COLOR_BGR2RGB);
191 else if (inChannels != 1 || outChannels != 1)
193 itkGenericExceptionMacro(
"Conversion from " << inChannels <<
" channels to " << outChannels
194 <<
" channels is not supported");
201 ITKConvertImageBuffer<TOutputImageType, TPixel>(
202 reinterpret_cast<char *>(current.ptr()), out, current.channels(), current.cols, current.rows, current.step);
205 template <
typename InputPixelType,
typename OutputPixelType>
209 if (outChannels == 3)
213 itkGenericExceptionMacro(
"OpenCV to ITK conversion channel component type mismatch");
216 else if (
typeid(InputPixelType) !=
typeid(OutputPixelType))
218 itkGenericExceptionMacro(
"OpenCV to ITK conversion pixel type mismatch");
222 template <
typename TOutputImageType,
typename TPixel>
225 TOutputImageType * out,
226 unsigned int inChannels,
231 using ImageType = TOutputImageType;
232 using OutputPixelType =
typename ImageType::PixelType;
235 bool isVectorImage(strcmp(out->GetNameOfClass(),
"VectorImage") == 0);
240 typename ImageType::SpacingType spacing;
246 region.SetSize(size);
247 region.SetIndex(start);
248 out->SetRegions(region);
249 out->SetSpacing(spacing);
251 size_t lineLength = imgWidth * inChannels *
sizeof(TPixel);
252 auto * unpaddedBuffer = reinterpret_cast<void *>(
new TPixel[imgHeight * lineLength]);
253 unsigned int paddedBufPos = 0;
254 unsigned int unpaddedBufPos = 0;
256 for (
int i = 0; i < imgHeight; ++i)
258 memcpy(&(reinterpret_cast<char *>(unpaddedBuffer)[unpaddedBufPos]), in + paddedBufPos, lineLength);
259 paddedBufPos += widthStep;
260 unpaddedBufPos += lineLength;
266 static_cast<TPixel *>(unpaddedBuffer),
268 out->GetPixelContainer()->GetBufferPointer(),
269 out->GetPixelContainer()->Size());
274 static_cast<TPixel *>(unpaddedBuffer),
276 out->GetPixelContainer()->GetBufferPointer(),
277 out->GetPixelContainer()->Size());
280 delete[] reinterpret_cast<TPixel *>(unpaddedBuffer);
283 template <
typename TPixel,
unsigned int VDimension>
291 template <
typename TValue,
unsigned int VDimension>
303 for (
int r = 0; r < out->height; ++r)
305 auto * ptr = reinterpret_cast<ValueType *>(out->imageData + r * out->widthStep);
306 for (
int c = 0; c < out->width; ++c)
312 for (
unsigned int i = 0; i < 3; ++i)
324 #ifndef ITK_MANUAL_INSTANTIATION
325 # include "itkOpenCVImageBridge.hxx"