1、Mat, IplImage, CvMat, CvArr的关系:
1. opencv文档中明确声明,CvMat已经过时了(CvMat is now obsolete,consider using Mat instead)不建议用;
2. 派生关系:CvArr -> CvMat -> IplImage
3. Mat用的一套东西是imread,imshow等,有别于CvArr及其子类的cvLoadImage(),cvShowImage()...
2. 相互转换
所有代码已经过测试,但是这里我就示意一下,真正写的时候不要忘记初始化转化过去的变量。
2.1 Mat与IplImage相互转换
IplImage* src;
某文章说,转换应该是Mat m(src); 而这不会复制内容,真正能复制内容的是:
Mat -> IplImage:
Mat m;
IplImage* transIplimage = cvCloneImage(&(IplImage) m);
IplImage -> Mat
IplImage* transIplImage;
Mat m = cvarrToMat(transIplImage,true);
2.2 CvMat与IplImage相互转换
IplImage* transIplImage;
CvMat* cvmat;
cvGetMat(transIplImage,cvmat);
cvGetImage(cvmat,transIplImage);
3. (多通道)Mat, IplImage, CvMat的元素获取
单通道的网上很多,这里只写多通道:
3.1 IplImage
//i is the index of rows
//j is the index of cols
//c is the index of channel
((uchar*)pImg->imageData+i*pImg->widthStep)[j*3+c]
CV_IMAGE_ELEM(pImg,uchar,i,3*j+c)
3.2 Mat
Mat m;
int h = m.rows; int w = m.cols;
int nc = m.channels();
for (int i = 0;i<h;i++)
{
for(int j = 0;j<w;j++)
{
Vec3b& elem = m.at<Vec3b>(i,j);
for (int c = 0; c<nc; c++)
{
uchar uc = elem[c] ;//Mat(i,j) of channel c
}
}
}
3.3 CvMat
CV_MAT_ELEM(cvmat,uchar,i,3*j+c)
4. 验证获取元素代码(Mat转IplImage)
此代码只用于验证多通道元素获取没有错误,具体用的时候最好还是用opencv直接给的吧(见第3小节)
IplImage* cvcvt_mat2IplImage(Mat m)
{
int h = m.rows; int w = m.cols;
int nc = m.channels();
IplImage* pImg = cvCreateImage(cvSize(w,h),8,nc);
for (int i = 0;i<h;i++)
{
for(int j = 0;j<w;j++)
{
Vec3b& elem = m.at<Vec3b>(i,j);
for (int c = 0; c<nc; c++)
{
//以下两种都可以
//((uchar*)pImg->imageData+i*pImg->widthStep)[j*3+c] = elem[c];
CV_IMAGE_ELEM(pImg,uchar,i,3*j+c) = elem[c];
}
}
}
return pImg;
}