opencv中常见的与图像操作有关的数据容器有Mat,cvMat和IplImage,这三种类型都可以代表和显示图像,但是,Mat类型侧重于计算,数学性较高,openCV对Mat类型的计算也进行了优化。而CvMat和IplImage类型更侧重于“图像”,opencv对其中的图像操作(缩放、单通道提取、图像阈值操作等)进行了优化。在opencv2.0之前,opencv是完全用C实现的,但是,IplImage类型与CvMat类型的关系类似于面向对象中的继承关系。实际上,CvMat之上还有一个更抽象的基类----CvArr,这在源代码中会常见。
1. IplImage
opencv中的图像信息头,该结构体定义:
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
typedef struct _IplImage { int nSize; int ID; int nChannels; int alphaChannel; int depth; char colorModel[4]; char channelSeq[4]; int dataOrder; int origin; int align; int width; int height; struct _IplROI *roi; struct _IplImage *maskROI; void *imageId; struct _IplTileInfo *tileInfo; int imageSize; char *imageData; int widthStep; int BorderMode[4]; int BorderConst[4]; char *imageDataOrigin; } IplImage;
dataOrder中的两个取值:交叉存取颜色通道是颜色数据排列将会是BGRBGR...的交错排列。分开的颜色通道是有几个颜色通道就分几个颜色平面存储。roi是IplROI结构体,该结构体包含了xOffset,yOffset,height,width,coi成员变量,其中xOffset,yOffset是x,y坐标,coi代表channelof interest(感兴趣的通道),非0的时候才有效。访问图像中的数据元素,分间接存储和直接存储,当图像元素为浮点型时,(uchar*) 改为 (float *):
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
IplImage* img=cvLoadImage("lena.jpg", 1); CvScalar s; s=cvGet2D(img,i,j); cvSet2D(img,i,j,s); IplImage* img; //malloc memory by cvLoadImage or cvCreateImage for(int row = 0; row < img->height; row++) { for (int col = 0; col < img->width; col++) { b = CV_IMAGE_ELEM(img, UCHAR, row, col * img->nChannels + 0); g = CV_IMAGE_ELEM(img, UCHAR, row, col * img->nChannels + 1); r = CV_IMAGE_ELEM(img, UCHAR, row, col * img->nChannels + 2); } } IplImage* img; //malloc memory by cvLoadImage or cvCreateImage uchar b, g, r; // 3 channels for(int row = 0; row < img->height; row++) { for (int col = 0; col < img->width; col++) { b = ((uchar *)(img->imageData + row * img->widthStep))[col * img->nChannels + 0]; g = ((uchar *)(img->imageData + row * img->widthStep))[col * img->nChannels + 1]; r = ((uchar *)(img->imageData + row * img->widthStep))[col * img->nChannels + 2]; } }
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
IplImage * cvLoadImage(const char * filename, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR)); //load images from specified image IplImage * cvCreateImage(CvSize size, int depth, int channels); //allocate memory
2.CvMat
首先,我们需要知道,第一,在OpenCV中没有向量(vector)结构。任何时候需要向量,都只需要一个列矩阵(如果需要一个转置或者共轭向量,则需要一个行矩阵)。第二,OpenCV矩阵的概念与我们在线性代数课上学习的概念相比,更抽象,尤其是矩阵的元素,并非只能取简单的数值类型,可以是多通道的值。CvMat的结构:
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
CvMat * cvCreateMat(int rows, int cols, int type); CV_INLine CvMat cvMat((int rows, int cols, int type, void* data CV_DEFAULT); CvMat * cvInitMatHeader(CvMat * mat, int rows, int cols, int type, void * data CV_DEFAULT(NULL), int step CV_DEFAULT(CV_AUTOSTEP));
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
cvmSet( CvMat* mat, int row, int col, double value); cvmGet( const CvMat* mat, int row, int col ); CvScalar cvGet2D(const CvArr * arr, int idx0, int idx1); //CvArr只作为函数的形参void cvSet2D(CvArr* arr, int idx0, int idx1, CvScalar value);
CvMat * cvmat = cvCreateMat(4, 4, CV_32FC1); cvmat->data.fl[row * cvmat->cols + col] = (float)3.0; CvMat * cvmat = cvCreateMat(4, 4, CV_64FC1); cvmat->data.db[row * cvmat->cols + col] = 3.0;
CvMat * cvmat = cvCreateMat(4, 4, CV_64FC1); CV_MAT_ELEM(*cvmat, double, row, col) = 3.0;
if (CV_MAT_DEPTH(cvmat->type) == CV_32F) CV_MAT_ELEM_CN(*cvmat, float, row, col * CV_MAT_CN(cvmat->type) + ch) = (float)3.0; // ch为通道值 if (CV_MAT_DEPTH(cvmat->type) == CV_64F) CV_MAT_ELEM_CN(*cvmat, double, row, col * CV_MAT_CN(cvmat->type) + ch) = 3.0; // ch为通道值
for (int row = 0; row < cvmat->rows; row++) { p = cvmat ->data.fl + row * (cvmat->step / 4); for (int col = 0; col < cvmat->cols; col++) { *p = (float) row + col; *(p+1) = (float)row + col + 1; *(p+2) = (float)row + col + 2; p += 3; } } CvMat * vector = cvCreateMat(1,3, CV_32SC2);CV_MAT_ELEM(*vector, CvPoint, 0, 0) = cvPoint(100,100); CvMat * vector = cvCreateMat(1,3, CV_64FC4);CV_MAT_ELEM(*vector, CvScalar, 0, 0) = CvScalar(0, 0, 0, 0);
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
CvMat* M1 = cvCreateMat(4,4,CV_32FC1); CvMat* M2; M2=cvCloneMat(M1);
3.Mat
Mat是opencv2.0推出的处理图像的新的数据结构,现在越来越有趋势取代之前的cvMat和lplImage,相比之下Mat最大的好处就是能够更加方便的进行内存管理,不再需要程序员手动管理内存的释放。opencv2.3中提到Mat是一个多维的密集数据数组,可以用来处理向量和矩阵、图像、直方图等等常见的多维数据。
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
Mat(nrows, ncols, type, fillValue]); M.create(nrows, ncols, type);
例子: Mat M(7,7,CV_32FC2,Scalar(1,3)); M.create(100, 60, CV_8UC(15));
int sz[] = {100, 100, 100}; Mat bigCube(3, sz, CV_8U, Scalar:all(0));
double m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}}; Mat M = Mat(3, 3, CV_64F, m).inv();
Mat img(Size(320,240),CV_8UC3); Mat img(height, width, CV_8UC3, pixels, step);
IplImage* img = cvLoadImage("greatwave.jpg", 1); Mat mtx(img,0); // convert IplImage* -> Mat;
访问Mat的数据元素:
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
Mat M; M.row(3) = M.row(3) + M.row(5) * 3; Mat M1 = M.col(1); M.col(7).copyTo(M1); Mat M; M.at<<SPAN style="LINE-HEIGHT: 1.5 !important; FONT-FAMILY: 'Courier New' !important; COLOR: rgb(0,0,255); FONT-SIZE: 12px !important">double>(i,j); M.at(uchar)(i,j); Vec3i bgr1 = M.at(Vec3b)(i,j) Vec3s bgr2 = M.at(Vec3s)(i,j) Vec3w bgr3 = M.at(Vec3w)(i,j) double sum = 0.0f; for(int row = 0; row < M.rows; row++) { const double * Mi = M.ptr<<SPAN style="LINE-HEIGHT: 1.5 !important; FONT-FAMILY: 'Courier New' !important; COLOR: rgb(0,0,255); FONT-SIZE: 12px !important">double>(row); for (int col = 0; col < M.cols; col++) sum += std::max(Mi[j], 0.); } double sum=0; MatConstIterator<<SPAN style="LINE-HEIGHT: 1.5 !important; FONT-FAMILY: 'Courier New' !important; COLOR: rgb(0,0,255); FONT-SIZE: 12px !important">double> it = M.begin<<SPAN style="LINE-HEIGHT: 1.5 !important; FONT-FAMILY: 'Courier New' !important; COLOR: rgb(0,0,255); FONT-SIZE: 12px !important">double>(), it_end = M.end<<SPAN style="LINE-HEIGHT: 1.5 !important; FONT-FAMILY: 'Courier New' !important; COLOR: rgb(0,0,255); FONT-SIZE: 12px !important">double>(); for(; it != it_end; ++it) sum += std::max(*it, 0.);
Mat可进行Matlab风格的矩阵操作,如初始化的时候可以用initializers,zeros(), ones(), eye().除以上内容之外,Mat还有有3个重要的方法:
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
Mat mat = imread(const String* filename); // 读取图像 imshow(const string frameName, InputArray mat); // 显示图像 imwrite (const string& filename, InputArray img); //储存图像
4. CvMat, Mat, IplImage之间的互相转换
![(转载)IplImage, <wbr>CvMat, <wbr>Mat <wbr>的关系](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
IpIImage -> CvMat CvMat matheader; CvMat * mat = cvGetMat(img, &matheader); CvMat * mat = cvCreateMat(img->height, img->width, CV_64FC3); cvConvert(img, mat)
IplImage -> Mat Mat::Mat(const IplImage* img, bool copyData=false); 例子: IplImage* iplImg = cvLoadImage("greatwave.jpg", 1); Mat mtx(iplImg);
Mat -> IplImage Mat M IplImage iplimage = M;
CvMat -> Mat Mat::Mat(const CvMat* m, bool copyData=false);
Mat -> CvMat 例子(假设Mat类型的imgMat图像数据存在): CvMat cvMat = imgMat;/*Mat -> CvMat, 类似转换到IplImage,不复制数据只创建矩阵头