OPencv 3.0 IplImage轉成Mat 版本3.0
方法在原始碼matrix.cpp下面 各種方法沒有在標頭檔案中定義
IplImage* img = cvLoadImage(filePath, 1);
Mat mtx=iplImageToMat(img,TRUE);
static Mat iplImageToMat(const IplImage* img, bool copyData)
{
Mat m;
if( !img )
return m;
m.dims = 2;
CV_DbgAssert(CV_IS_IMAGE(img) && img->imageData != 0);
int imgdepth = IPL2CV_DEPTH(img->depth);
size_t esz;
m.step[0] = img->widthStep;
if(!img->roi)
{
CV_Assert(img->dataOrder == IPL_DATA_ORDER_PIXEL);
m.flags = Mat::MAGIC_VAL + CV_MAKETYPE(imgdepth, img->nChannels);
m.rows = img->height;
m.cols = img->width;
m.datastart = m.data = (uchar*)img->imageData;
esz = CV_ELEM_SIZE(m.flags);
}
else
{
CV_Assert(img->dataOrder == IPL_DATA_ORDER_PIXEL || img->roi->coi != 0);
bool selectedPlane = img->roi->coi && img->dataOrder == IPL_DATA_ORDER_PLANE;
m.flags = Mat::MAGIC_VAL + CV_MAKETYPE(imgdepth, selectedPlane ? 1 : img->nChannels);
m.rows = img->roi->height;
m.cols = img->roi->width;
esz = CV_ELEM_SIZE(m.flags);
m.datastart = m.data = (uchar*)img->imageData +
(selectedPlane ? (img->roi->coi - 1)*m.step*img->height : 0) +
img->roi->yOffset*m.step[0] + img->roi->xOffset*esz;
}
m.datalimit = m.datastart + m.step.p[0]*m.rows;
m.dataend = m.datastart + m.step.p[0]*(m.rows-1) + esz*m.cols;
m.flags |= (m.cols*esz == m.step.p[0] || m.rows == 1 ? Mat::CONTINUOUS_FLAG : 0);
m.step[1] = esz;
if( copyData )
{
Mat m2 = m;
m.release();
if( !img->roi || !img->roi->coi ||
img->dataOrder == IPL_DATA_ORDER_PLANE)
m2.copyTo(m);
else
{
int ch[] = {img->roi->coi - 1, 0};
m.create(m2.rows, m2.cols, m2.type());
mixChannels(&m2, 1, &m, 1, ch, 1);
}
}
return m;
}