Something More for Research

Explorer of Research #HEMBAD

The conversion and copy CvMat, Mat and between IplImage

Posted by Hemprasad Y. Badgujar on May 15, 2015


The conversion and copy CvMat, Mat and between IplImage

In OpenCV Mat, CvMat and IplImage types can represent and display the image. IplImage derived from the CvMat, and CvMat that is derived from the CvArr CvArr -> CvMat -> IplImage, Mat type is a C ++ version of the matrix type (CvArr used as a function of the parameters, either passed or are CvMat IplImage, inside it is by CvMat deal with).

Mat type which focuses on computing, mathematics higher, OpenCV Mat ​​type of calculation is also optimized; while CvMat and IplImage type is more focused on the “image”, OpenCV on which the image manipulation (zoom, single extraction, image thresholding operation, etc.) were optimized.Many times the need for mutual conversion of three types, here a brief overview.

Conversion and copy
CvMat and between Mat

1 replication between CvMat,

  1. // Note: deep copy – separately allocated space, two independent
  2. CvMat* a;
  3. CvMat* b = cvCloneMat(a);   //copy a to b

2 Copy between Mat,

  1. // Note: shallow copy – not just copy the data to create a matrix head, data sharing (change a, b, c of the same effect will be on any one of the other two production)
  2. Mat a;
  3. Mat b = a; //a “copy” to b
  4. Mat c(a); //a “copy” to c
  5. // Note: deep copy
  6. Mat a;
  7. Mat b = a.clone(); //a copy to b
  8. Mat c;
  9. a.copyTo(c); //a copy to c

3, CvMat turn Mat

  1. // Use the constructor Mat: Mat :: Mat (const CvMat * m, bool copyData = false); copyData default is false
  2. CvMat* a;
  3. // Note: the following three consistent results, are shallow copy
  4. Mat b(a);   //a “copy” to b
  5. Mat b(a, false);    //a “copy” to b
  6. Mat b = a;  //a “copy” to b
  7. // Note: When the parameter copyData set to true, it was a deep copy (copying the entire image data)
  8. Mat b = Mat(a, true); //a copy to b

4, Mat turn CvMat

  1. // Note: shallow copy
  2. Mat a;
  3. CvMat b = a; //a “copy” to b
  4. // Note: deep copy
  5. Mat a;
  6. CvMat *b;
  7. CvMat temp = a;  // into CvMat type, instead of copying data
  8. CVCopy  (& temp, b);  // true copy data

Conversion and copy ================ ======================== IplImage above between the two ======== 1. Copy IplImage between this does not go into details, that is cvCopy use with cvCloneImage difference, Zhang posted online map:

2, IplImage turn Mat

  1. // Use the constructor Mat: Mat :: Mat (const IplImage * img, bool copyData = false); default is false copyData
  2. IplImage* srcImg = cvLoadImage(“Lena.jpg”);
  3. // Note: the following three consistent results, are shallow copy
  4. Mat M(srcImg);
  5. Mat M(srcImg, false);
  6. Mat M = srcImg;
  7. // Note: When the parameter copyData set to true, it was a deep copy (copying the entire image data)
  8. Mat M(srcImg, true);

3, Mat turn IplImage

  1. // Note: shallow copy – again, just to create an image first, but not to copy data
  2. Mat M;
  3. IplImage img = M;
  4. IplImage img = IplImage(M);

4, IplImage turn CvMat

  1. // Method a: cvGetMat function
  2. IplImage* img;
  3. CvMat temp;
  4. CvMat* mat = cvGetMat(img, &temp);  //深拷贝
  5. // Act II: cvConvert function
  6. CvMat *mat = cvCreateMat(img->height, img->width, CV_64FC3);  //注意height和width的顺序
  7. cvConvert (img, mat);     // a deep copy

5, CvMat turn IplImage

  1. // Method a: cvGetImage function
  2. CvMat M;
  3. IplImage* img = cvCreateImageHeader(M.size(), M.depth(), M.channels());
  4. cvGetImage (& M, img);     // a deep copy: The function returns img
  5. // Also be written as
  6. CvMat M;
  7. IplImage* img = cvGetImage(&M, cvCreateImageHeader(M.size(), M.depth(), M.channels()));
  8. // Act II: cvConvert function
  9. CvMat M;
  10. IplImage* img = cvCreateImage(M.size(), M.depth(), M.channels());
  11. cvConvert (& M, img);  // a deep copy

 

A final note:

1, Mat type is automatic memory management, no explicit release (of course, you can also call the manual release () method to force Mat matrix data release); and CvMat you need to call cvReleaseMat (& cvmat) to release, IplImage call cvReleaseImage (& iplimage) to release.
2, the establishment of CvMat matrix, the first parameter is the number of rows, the second parameter is the number of columns: CvMat * cvCreateMat (int rows, int cols, int type); 3, when establishing IplImage image, CvSize first parameter width, namely the number of columns; the second argument is the height of that line number: IplImage * cvCreateImage (CvSize size, int depth, int channels); CvSize CvSize (int width, int height); 4, IplImage internal buffer per line is by 4 byte alignment, CvMat not have this limitation.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Extracts from a Personal Diary

dedicated to the life of a silent girl who eventually learnt to open up

Num3ri v 2.0

I miei numeri - seconda versione

ThuyDX

Just another WordPress.com site

Algunos Intereses de Abraham Zamudio Chauca

Matematica, Linux , Programacion Serial , Programacion Paralela (CPU - GPU) , Cluster de Computadores , Software Cientifico

josephdung

thoughts...

Tech_Raj

A great WordPress.com site

Travel tips

Travel tips

Experience the real life.....!!!

Shurwaat achi honi chahiye ...

Ronzii's Blog

Just your average geek's blog

Karan Jitendra Thakkar

Everything I think. Everything I do. Right here.

VentureBeat

News About Tech, Money and Innovation

Chetan Solanki

Helpful to u, if u need it.....

ScreenCrush

Explorer of Research #HEMBAD

managedCUDA

Explorer of Research #HEMBAD

siddheshsathe

A great WordPress.com site

Ari's

This is My Space so Dont Mess With IT !!

%d bloggers like this: