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"
63 template<
typename TOutputImageType>
67 template<
typename TOutputImageType>
68 static typename TOutputImageType::Pointer
CVMatToITKImage(
const cv::Mat & in);
71 template<
typename TInputImageType>
72 static IplImage*
ITKImageToIplImage(
const TInputImageType* in,
bool force3Channels =
false);
75 template<
typename TInputImageType>
76 static cv::Mat
ITKImageToCVMat(
const TInputImageType* in,
bool force3Channels =
false);
87 template<
typename TOutputImageType,
typename TPixel >
89 TOutputImageType* out,
93 typedef TOutputImageType ImageType;
94 typedef typename ImageType::PixelType OutputPixelType;
98 unsigned int inChannels = in->nChannels;
102 IplImage* current =
const_cast<IplImage*
>(in);
104 bool isVectorImage(strcmp(out->GetNameOfClass(),
"VectorImage") == 0);
106 bool freeCurrent =
false;
107 if (inChannels == 3 && outChannels == 1)
109 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 1);
110 cvCvtColor(in, current, CV_BGR2GRAY);
113 else if (inChannels == 1 && outChannels == 3)
115 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
116 cvCvtColor(in, current, CV_GRAY2RGB);
119 else if (inChannels == 3 && outChannels == 3)
121 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
122 cvCvtColor(in, current, CV_BGR2RGB);
125 else if (inChannels != 1 || outChannels != 1)
127 itkGenericExceptionMacro(
"Conversion from " << inChannels <<
" channels to "
128 << outChannels <<
" channels is not supported");
130 typename ImageType::RegionType region;
131 typename ImageType::RegionType::SizeType size;
132 typename ImageType::RegionType::IndexType start;
133 typename ImageType::SpacingType spacing;
135 size[0] = current->width;
136 size[1] = current->height;
139 region.SetSize(size);
140 region.SetIndex(start);
141 out->SetRegions(region);
142 out->SetSpacing(spacing);
144 size_t lineLength = current->width*current->nChannels;
145 void* unpaddedBuffer =
reinterpret_cast< void*
>(
146 new TPixel[current->height*lineLength]);
147 unsigned int paddedBufPos = 0;
148 unsigned int unpaddedBufPos = 0;
149 for (
int i = 0; i < current->height; ++i)
151 memcpy(&(reinterpret_cast<TPixel*>(unpaddedBuffer)[unpaddedBufPos]),
152 reinterpret_cast<TPixel*>(current->imageData + paddedBufPos),
153 lineLength*
sizeof(TPixel) );
154 paddedBufPos += current->widthStep;
155 unpaddedBufPos += lineLength;
162 out->GetPixelContainer()->GetBufferPointer(),
163 out->GetPixelContainer()->Size());
168 ::Convert(static_cast< TPixel* >(unpaddedBuffer),
170 out->GetPixelContainer()->GetBufferPointer(),
171 out->GetPixelContainer()->Size());
173 delete[]
reinterpret_cast<TPixel*
>(unpaddedBuffer);
176 cvReleaseImage(¤t);
180 template<
typename TPixel,
unsigned int VDimension >
184 IplImage* itkNotUsed( out ) )
188 template<
typename TValue,
unsigned int VDimension >
199 for(
int r=0;r < out->height; r++ )
201 ValueType* ptr =
reinterpret_cast< ValueType*
>( out->imageData + r * out->widthStep );
202 for(
int c=0;c < out->width; c++ )
208 for(
unsigned int i=0; i< 3; i++ )
220 #ifndef ITK_MANUAL_INSTANTIATION
221 #include "itkOpenCVImageBridge.hxx"
Image< PixelType, VDimension > ImageType
static IplImage * ITKImageToIplImage(const TInputImageType *in, bool force3Channels=false)
static void ITKConvertIplImageBuffer(const IplImage *in, TOutputImageType *out, int iDepth)
static cv::Mat ITKImageToCVMat(const TInputImageType *in, bool force3Channels=false)
static TOutputImageType::Pointer IplImageToITKImage(const IplImage *in)
static void ConvertVectorImage(InputPixelType *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
Traits class used to by ConvertPixels to convert blocks of pixels.
const TPixel & GetPixel(const IndexType &index) const
Get a pixel (read only version).
This class provides static methods to convert between OpenCV images and itk::Image.
static void Convert(InputPixelType *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
static void Padding(const Image< TPixel, VDimension > *, IplImage *)
Superclass::IndexType IndexType
static TOutputImageType::Pointer CVMatToITKImage(const cv::Mat &in)
Represent Red, Green and Blue components for color images.
static void Padding(const ImageType *in, IplImage *out)
RGBPixel< ValueType > PixelType
Define additional traits for native types such as int or float.
Templated n-dimensional image class.