18 #ifndef itkOpenCVImageBridge_h
19 #define itkOpenCVImageBridge_h
56 template<
typename TOutputImageType>
60 template<
typename TOutputImageType>
61 static typename TOutputImageType::Pointer
CVMatToITKImage(
const cv::Mat & in);
64 template<
typename TInputImageType>
65 static IplImage*
ITKImageToIplImage(
const TInputImageType* in,
bool force3Channels =
false);
68 template<
typename TInputImageType>
69 static cv::Mat
ITKImageToCVMat(
const TInputImageType* in,
bool force3Channels =
false);
81 template<
typename TOutputImageType,
typename TPixel >
83 TOutputImageType* out,
87 typedef TOutputImageType ImageType;
88 typedef typename ImageType::PixelType OutputPixelType;
92 unsigned int inChannels = in->nChannels;
96 IplImage* current =
const_cast<IplImage*
>(in);
98 bool isVectorImage(strcmp(out->GetNameOfClass(),
"VectorImage") == 0);
100 bool freeCurrent =
false;
101 if (inChannels == 3 && outChannels == 1)
103 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 1);
104 cvCvtColor(in, current, CV_BGR2GRAY);
107 else if (inChannels == 1 && outChannels == 3)
109 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
110 cvCvtColor(in, current, CV_GRAY2RGB);
113 else if (inChannels == 3 && outChannels == 3)
115 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
116 cvCvtColor(in, current, CV_BGR2RGB);
119 else if (inChannels != 1 || outChannels != 1)
121 itkGenericExceptionMacro(
"Conversion from " << inChannels <<
" channels to "
122 << outChannels <<
" channels is not supported");
124 typename ImageType::RegionType region;
125 typename ImageType::RegionType::SizeType size;
126 typename ImageType::RegionType::IndexType start;
127 typename ImageType::SpacingType spacing;
129 size[0] = current->width;
130 size[1] = current->height;
133 region.SetSize(size);
134 region.SetIndex(start);
135 out->SetRegions(region);
136 out->SetSpacing(spacing);
138 size_t lineLength = current->width*current->nChannels;
139 void* unpaddedBuffer =
reinterpret_cast< void*
>(
140 new TPixel[current->height*lineLength]);
141 unsigned int paddedBufPos = 0;
142 unsigned int unpaddedBufPos = 0;
143 for (
int i = 0; i < current->height; ++i)
145 memcpy(&(reinterpret_cast<TPixel*>(unpaddedBuffer)[unpaddedBufPos]),
146 &(reinterpret_cast<TPixel*>(current->imageData)[paddedBufPos]),
148 paddedBufPos += current->widthStep;
149 unpaddedBufPos += lineLength;
156 out->GetPixelContainer()->GetBufferPointer(),
157 out->GetPixelContainer()->Size());
162 ::Convert(static_cast< TPixel* >(unpaddedBuffer),
164 out->GetPixelContainer()->GetBufferPointer(),
165 out->GetPixelContainer()->Size());
167 delete[]
reinterpret_cast<TPixel*
>(unpaddedBuffer);
170 cvReleaseImage(¤t);
174 template<
typename TPixel,
unsigned int VDimension >
178 IplImage* itkNotUsed( out ) )
182 template<
typename TValue,
unsigned int VDimension >
193 for(
int r=0;r < out->height; r++ )
195 ValueType* ptr =
reinterpret_cast< ValueType*
>( out->imageData + r * out->widthStep );
196 for(
int c=0;c < out->width; c++ )
202 for(
unsigned int i=0; i< 3; i++ )
214 #ifndef ITK_MANUAL_INSTANTIATION
215 #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).
OpenCVImageBridge(const Self &)
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
void operator=(const Self &)
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.