18 #ifndef __itkOpenCVImageBridge_h
19 #define __itkOpenCVImageBridge_h
56 template<
class TOutputImageType>
60 template<
class TOutputImageType>
61 static typename TOutputImageType::Pointer
CVMatToITKImage(
const cv::Mat & in);
64 template<
class TInputImageType>
65 static IplImage*
ITKImageToIplImage(
const TInputImageType* in,
bool force3Channels =
false);
68 template<
class TInputImageType>
69 static cv::Mat
ITKImageToCVMat(
const TInputImageType* in,
bool force3Channels =
false);
81 template<
class TOutputImageType,
class 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<
class TPixel,
unsigned int VDimension >
178 IplImage* itkNotUsed( out ) )
182 template<
class TValue,
unsigned int VDimension >
192 pixelIndex.Fill( 0 );
194 for(
int r=0;r < out->height; r++ )
196 ValueType* ptr =
reinterpret_cast< ValueType*
>( out->imageData + r * out->widthStep );
197 for(
int c=0;c < out->width; c++ )
203 for(
unsigned int i=0; i< 3; i++ )
215 #ifndef ITK_MANUAL_INSTANTIATION
216 #include "itkOpenCVImageBridge.hxx"