Mat, CvMat & IplImage

There are 9 kinds of transformations here.

  • Mat To Mat
//Shadow Copy
Mat a;
Mat b = a; //“copy” a to b
Mat c(a); //“copy” a to c
//Deep Copy
Mat a;
Mat b = a.clone(); //copy a to b
Mat c;
a.copyTo(c); //copy a to c
  • CvMat To CvMat
//Deep Copy
CvMat* a; 
CvMat* b = cvCloneMat(a); //copy a to b
  • IplImage To IplImage
void cvCopy(const CvArr* src, CvArr* dst, const CvArr*mask=NULL)
•src — The source array
•dst — The destination array
•mask — Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed
If any of the passed arrays is of IplImage type, then its ROI and COI fields are used. Both arrays must have the same type, the same number of dimensions, and the same size. The function can also copy sparse arrays (mask is not supported in this case).
— — — — — —
IplImage* cvCloneImage(const IplImage* image)
•image — The original image
  • Mat To CvMat
//Shadow Copy
Mat a;
CvMat b = a; //“copy” a to b
//Deep Copy
Mat a;
CvMat *b;
CvMat temp = a; //Turn to CvMat type.
cvCopy(&temp, b); //Real copy. cvCopy follows allocating space.
  • Mat To IplImage
//Shadow Copy
Mat M;
IplImage img = M;
IplImage img = IplImage(M);
//Deep Copy
cv::Mat img2;
IplImage imgTmp = img2;
IplImage *input = cvCloneImage(&imgTmp);
  • CvMat To Mat
//Constructor: Mat::Mat(const CvMat* m, bool copyData=false); 
CvMat* a;
//Shadow Copy
Mat b(a); //“copy” a to b
Mat b(a, false); //“copy” a to b
Mat b = a; //“copy” to b
//Deep Copy with copyData set to true
Mat b = Mat(a, true); //copy a to b
  • CvMat To IplImage
CvMat M;
IplImage* img = cvCreateImageHeader(M.size(), M.depth(), M.channels());
cvGetImage(&M, img); //Deep Copy
CvMat M;
IplImage* img = cvGetImage(&M, cvCreateImageHeader(M.size(), M.depth(), M.channels()));
CvMat M;
IplImage* img = cvCreateImage(M.size(), M.depth(), M.channels());
cvConvert(&M, img); //Deep Copy
  • IplImage To Mat
Mat cvarrToMat(const CvArr* arr, bool copyData=false, bool allowND=true, int coiMode=0 )
•arr — input CvMat, IplImage , or CvMatND.
•copyData — when false (default value), no data is copied and only the new header is created, in this case, the original array should not be deallocated while the new matrix header is used; if the parameter is true, all the data is copied and you may deallocate the original array right after the conversion.
•allowND — when true (default value), CvMatND is converted to 2-dimensional Mat, if it is possible (see the discussion below); if it is not possible, or when the parameter is false, the function will report an error.
•coiMode — 
parameter specifying how the IplImage COI (when set) is handled.
◦If coiMode=0 and COI is set, the function reports an error.
◦If coiMode=1 , the function never reports an error. Instead, it returns the header to the whole original image and you will have to check and process COI manually. See extractImageCOI() .
Normally, the function is used to convert an old-style 2D array ( CvMat or IplImage ) to Mat . However, the function can also take CvMatND as an input and create Mat() for it, if it is possible. And, for CvMatND A , it is possible if and only if A.dim[i].size*A.dim.step[i] == A.dim.step[i-1] for all or for all but one i, 0 < i < A.dims . That is, the matrix data should be continuous or it should be representable as a sequence of continuous matrices. By using this function in this way, you can process CvMatND using an arbitrary element-wise function.
The last parameter, coiMode , specifies how to deal with an image with COI set. By default, it is 0 and the function reports an error when an image with COI comes in. And coiMode=1 means that no error is signalled. You have to check COI presence and handle it manually. The modern structures, such as Mat and MatND do not support COI natively. To process an individual channel of a new-style array, you need either to organize a loop over the array (for example, using matrix iterators) where the channel of interest will be processed, or extract the COI using mixChannels() (for new-style arrays) or extractImageCOI() (for old-style arrays), process this individual channel, and insert it back to the output array if needed (using mixChannels() or insertImageCOI() , respectively).
  • IplImage To CvMat
IplImage* img;
CvMat temp;
CvMat* mat = cvGetMat(img, &temp); //Deep Copy
CvMat *mat = cvCreateMat(img->height, img->width, CV_64FC3); //Attention with the order of height and width
cvConvert(img, mat); //Deep Copy

Show your support

Clapping shows how much you appreciated Zixuan Wang’s story.