Conversion and copying between Cvmat,mat and Iplimage

Source: Internet
Author: User

1. Replication between Cvmat

Note: Deep copy-separate space allocated, both independent of each other  
cvmat* A;  
cvmat* B = Cvclonemat (a);   Copy A to B  

2, the reproduction between the mat

Note: Shallow copy-  do not copy data only create matrix header, data sharing (change a,b,c any one will have the same effect on the other 2)
Mat A;
Mat B = A; A "copy" to B
-Mat C (a);//a "Copy" to C

//Note: Deep copy
mat A;
Mat B = A.clone (); A copy to B
Mat C;
A.copyto (c); A copy to C

3, Cvmat turn mat

Using the Mat constructor: Mat::mat (const cvmat* m, bool copydata=false);    By default CopyData is False
cvmat* A;
Note: The following three kinds of effects are consistent, both are light copy
Mat B (a);    A "copy" to B
Mat B (A, false);    A "copy" to B
Mat B = A;    A "copy" to B

//NOTE: When the parameter copydata is set to true, it is a deep copy (copying the entire image data)
mat B = Mat (A, true);//a Copy to B

4, Mat turn Cvmat

Note: Light copy
Mat A;
Cvmat B = A; A "copy" to B

//NOTE: Deep copy
Mat A;
Cvmat *b;
Cvmat temp = A; Convert to Cvmat type, not copy data
cvcopy (&temp, b);//real copy of data cvcopy to open up memory space before use

==========iplimage and the conversion and copying between the two ===========

1. Replication between iplimage
This does not repeat, that is, cvcopy and cvcloneimage Use the difference, paste the picture on the Internet:

2, Iplimage turn mat

Using the Mat constructor: Mat::mat (const iplimage* IMG, bool copydata=false);    By default CopyData is False
iplimage* srcimg = cvloadimage ("lena.jpg");
Note: The following three kinds of effects are consistent, both are shallow copy
Mat M (srcimg);
Mat M (srcimg, false);
Mat M = srcimg;

Note: When the parameter copydata is set to true, it is a deep copy (copying the entire image data)
Mat M (srcimg, true);

3, Mat turn iplimage

Note: Shallow copy-also just creates the image header without duplicating data
Mat M;
Iplimage img = M;
Iplimage img = iplimage (M);
Deep copy
Cv::mat img2;
Iplimage imgtmp = Img2;
Iplimage *input = Cvcloneimage (&imgtmp);

4, Iplimage turn Cvmat

Law One: Cvgetmat function
iplimage* img;
Cvmat temp;
cvmat* mat = Cvgetmat (img, &temp);    Deep copy
//Method Two: Cvconvert function
Cvmat *mat = Cvcreatemat (Img->height, Img->width, CV_64FC3);    Note the height and width of the order
Cvconvert (IMG, mat);    Deep copy

5, Cvmat turn iplimage

Method One: cvgetimage function
Cvmat M;
iplimage* img = Cvcreateimageheader (m.size (), m.depth (), M.channels ());
Cvgetimage (&m, IMG);    Deep copy: Function return img
//can also be written as
Cvmat M;
iplimage* img = cvgetimage (&m, Cvcreateimageheader (M.size (), m.depth (), M.channels ()));
Method Two: Cvconvert function
Cvmat M;
iplimage* img = cvcreateimage (m.size (), m.depth (), M.channels ());
Cvconvert (&m, IMG);    Deep copy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.