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"
66 template<
typename TOutputImageType>
70 template<
typename TOutputImageType>
71 static typename TOutputImageType::Pointer
CVMatToITKImage(
const cv::Mat & in);
74 template<
typename TInputImageType>
75 static IplImage*
ITKImageToIplImage(
const TInputImageType* in,
bool force3Channels =
false);
78 template<
typename TInputImageType>
79 static cv::Mat
ITKImageToCVMat(
const TInputImageType* in,
bool force3Channels =
false);
88 template<
typename TOutputImageType,
typename TPixel >
90 TOutputImageType* out,
94 using ImageType = TOutputImageType;
95 using OutputPixelType =
typename ImageType::PixelType;
99 unsigned int inChannels = in->nChannels;
103 IplImage* current =
const_cast<IplImage*
>(in);
105 bool isVectorImage(strcmp(out->GetNameOfClass(),
"VectorImage") == 0);
107 bool freeCurrent =
false;
108 if (inChannels == 3 && outChannels == 1)
110 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 1);
111 cvCvtColor(in, current, CV_BGR2GRAY);
114 else if (inChannels == 1 && outChannels == 3)
116 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
117 cvCvtColor(in, current, CV_GRAY2RGB);
120 else if (inChannels == 3 && outChannels == 3)
122 current = cvCreateImage(cvSize(in->width, in->height), iDepth, 3);
123 cvCvtColor(in, current, CV_BGR2RGB);
126 else if (inChannels != 1 || outChannels != 1)
128 itkGenericExceptionMacro(
"Conversion from " << inChannels <<
" channels to "
129 << outChannels <<
" channels is not supported");
134 typename ImageType::SpacingType spacing;
136 size[0] = current->width;
137 size[1] = current->height;
140 region.SetSize(size);
141 region.SetIndex(start);
142 out->SetRegions(region);
143 out->SetSpacing(spacing);
145 size_t lineLength = current->width*current->nChannels;
146 void* unpaddedBuffer =
reinterpret_cast< void*
>(
147 new TPixel[current->height*lineLength]);
148 unsigned int paddedBufPos = 0;
149 unsigned int unpaddedBufPos = 0;
150 for (
int i = 0; i < current->height; ++i)
152 memcpy(&(reinterpret_cast<TPixel*>(unpaddedBuffer)[unpaddedBufPos]),
153 reinterpret_cast<TPixel*>(current->imageData + paddedBufPos),
154 lineLength*
sizeof(TPixel) );
155 paddedBufPos += current->widthStep;
156 unpaddedBufPos += lineLength;
163 out->GetPixelContainer()->GetBufferPointer(),
164 out->GetPixelContainer()->Size());
169 ::Convert(static_cast< TPixel* >(unpaddedBuffer),
171 out->GetPixelContainer()->GetBufferPointer(),
172 out->GetPixelContainer()->Size());
174 delete[]
reinterpret_cast<TPixel*
>(unpaddedBuffer);
177 cvReleaseImage(¤t);
181 template<
typename TPixel,
unsigned int VDimension >
185 IplImage* itkNotUsed( out ) )
189 template<
typename TValue,
unsigned int VDimension >
200 for(
int r=0;r < out->height; r++ )
202 ValueType* ptr =
reinterpret_cast< ValueType*
>( out->imageData + r * out->widthStep );
203 for(
int c=0;c < out->width; c++ )
209 for(
unsigned int i=0; i< 3; i++ )
221 #ifndef ITK_MANUAL_INSTANTIATION
222 #include "itkOpenCVImageBridge.hxx"
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.
constexpr unsigned int Dimension
static void Convert(InputPixelType *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
static void Padding(const Image< TPixel, VDimension > *, IplImage *)
ImageBaseType::SizeType SizeType
ImageBaseType::IndexType IndexType
static TOutputImageType::Pointer CVMatToITKImage(const cv::Mat &in)
typename Superclass::IndexType IndexType
Represent Red, Green and Blue components for color images.
static void Padding(const ImageType *in, IplImage *out)
void Fill(IndexValueType value)
ImageBaseType::RegionType RegionType
Templated n-dimensional image class.