18 #ifndef itkOpenCVImageBridge_h
19 #define itkOpenCVImageBridge_h
27 #include "opencv2/core/version.hpp"
28 #if !defined(CV_VERSION_EPOCH)
30 # include "opencv2/core.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;
103 unsigned int inChannels = in->nChannels;
107 IplImage * current = const_cast<IplImage *>(in);
109 bool isVectorImage(strcmp(out->GetNameOfClass(),
"VectorImage") == 0);
111 bool freeCurrent =
false;
112 if (inChannels == 3 && outChannels == 1)
114 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 1);
115 cvCvtColor(in, current, CV_BGR2GRAY);
118 else if (inChannels == 1 && outChannels == 3)
120 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
121 cvCvtColor(in, current, CV_GRAY2RGB);
124 else if (inChannels == 3 && outChannels == 3)
126 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
127 cvCvtColor(in, current, CV_BGR2RGB);
130 else if (inChannels != 1 || outChannels != 1)
132 itkGenericExceptionMacro(
"Conversion from " << inChannels <<
" channels to " << outChannels
133 <<
" channels is not supported");
138 typename ImageType::SpacingType spacing;
140 size[0] = current->width;
141 size[1] = current->height;
144 region.SetSize(size);
145 region.SetIndex(start);
146 out->SetRegions(region);
147 out->SetSpacing(spacing);
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)
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;
164 static_cast<TPixel *>(unpaddedBuffer),
166 out->GetPixelContainer()->GetBufferPointer(),
167 out->GetPixelContainer()->Size());
172 static_cast<TPixel *>(unpaddedBuffer),
174 out->GetPixelContainer()->GetBufferPointer(),
175 out->GetPixelContainer()->Size());
177 delete[] reinterpret_cast<TPixel *>(unpaddedBuffer);
180 cvReleaseImage(¤t);
184 template <
typename TPixel,
unsigned int VDimension>
192 template <
typename TValue,
unsigned int VDimension>
204 for (
int r = 0; r < out->height; r++)
206 ValueType * ptr = reinterpret_cast<ValueType *>(out->imageData + r * out->widthStep);
207 for (
int c = 0; c < out->width; c++)
213 for (
unsigned int i = 0; i < 3; i++)
225 #ifndef ITK_MANUAL_INSTANTIATION
226 # include "itkOpenCVImageBridge.hxx"