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"
65 template<
typename TOutputImageType>
69 template<
typename TOutputImageType>
70 static typename TOutputImageType::Pointer
CVMatToITKImage(
const cv::Mat & in);
73 template<
typename TInputImageType>
74 static IplImage*
ITKImageToIplImage(
const TInputImageType* in,
bool force3Channels =
false);
77 template<
typename TInputImageType>
78 static cv::Mat
ITKImageToCVMat(
const TInputImageType* in,
bool force3Channels =
false);
89 template<
typename TOutputImageType,
typename TPixel >
91 TOutputImageType* out,
95 typedef TOutputImageType ImageType;
96 typedef typename ImageType::PixelType OutputPixelType;
100 unsigned int inChannels = in->nChannels;
104 IplImage* current =
const_cast<IplImage*
>(in);
106 bool isVectorImage(strcmp(out->GetNameOfClass(),
"VectorImage") == 0);
108 bool freeCurrent =
false;
109 if (inChannels == 3 && outChannels == 1)
111 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 1);
112 cvCvtColor(in, current, CV_BGR2GRAY);
115 else if (inChannels == 1 && outChannels == 3)
117 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
118 cvCvtColor(in, current, CV_GRAY2RGB);
121 else if (inChannels == 3 && outChannels == 3)
123 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
124 cvCvtColor(in, current, CV_BGR2RGB);
127 else if (inChannels != 1 || outChannels != 1)
129 itkGenericExceptionMacro(
"Conversion from " << inChannels <<
" channels to "
130 << outChannels <<
" channels is not supported");
132 typename ImageType::RegionType region;
135 typename ImageType::SpacingType spacing;
137 size[0] = current->width;
138 size[1] = current->height;
141 region.SetSize(size);
142 region.SetIndex(start);
143 out->SetRegions(region);
144 out->SetSpacing(spacing);
146 size_t lineLength = current->width*current->nChannels;
147 void* unpaddedBuffer =
reinterpret_cast< void*
>(
148 new TPixel[current->height*lineLength]);
149 unsigned int paddedBufPos = 0;
150 unsigned int unpaddedBufPos = 0;
151 for (
int i = 0; i < current->height; ++i)
153 memcpy(&(reinterpret_cast<TPixel*>(unpaddedBuffer)[unpaddedBufPos]),
154 reinterpret_cast<TPixel*>(current->imageData + paddedBufPos),
155 lineLength*
sizeof(TPixel) );
156 paddedBufPos += current->widthStep;
157 unpaddedBufPos += lineLength;
164 out->GetPixelContainer()->GetBufferPointer(),
165 out->GetPixelContainer()->Size());
170 ::Convert(static_cast< TPixel* >(unpaddedBuffer),
172 out->GetPixelContainer()->GetBufferPointer(),
173 out->GetPixelContainer()->Size());
175 delete[]
reinterpret_cast<TPixel*
>(unpaddedBuffer);
178 cvReleaseImage(¤t);
182 template<
typename TPixel,
unsigned int VDimension >
186 IplImage* itkNotUsed( out ) )
190 template<
typename TValue,
unsigned int VDimension >
201 for(
int r=0;r < out->height; r++ )
203 ValueType* ptr =
reinterpret_cast< ValueType*
>( out->imageData + r * out->widthStep );
204 for(
int c=0;c < out->width; c++ )
210 for(
unsigned int i=0; i< 3; i++ )
222 #ifndef ITK_MANUAL_INSTANTIATION
223 #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 *)
void Fill(IndexValueType value)
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
ImageBaseType::SizeType SizeType
Define additional traits for native types such as int or float.
Templated n-dimensional image class.
ImageBaseType::IndexType IndexType