Ask Your Question

CopyTo() and Clone() functions

asked 2013-02-19 08:50:42 -0500

izzo gravatar image

updated 2013-02-19 08:57:56 -0500

Need description of the following code :

    1   Mat A = imread(argv[1], CV_LOAD_IMAGE_COLOR);
    2   Mat F = A.clone();
    3   Mat G;
    4   A.copyTo(G);

The second line, is the header A is copied to F ?

The forth line, is the header A copied to G or vice versa( G copied to A) ?

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2013-02-19 09:13:40 -0500

unxnut gravatar image

Refer to the link to clone and copyTo in the documentation. On line 2, the header A is copied to F and on line 4, A is copied to G.

edit flag offensive delete link more


Thank you, i get the idea..

izzo gravatar imageizzo ( 2013-02-19 10:04:57 -0500 )edit

answered 2013-02-19 09:13:08 -0500

berak gravatar image
Mat A = imread(argv[1], CV_LOAD_IMAGE_COLOR);

Mat B = A.clone();  // B is a deep copy of A. (has its own copy of the pixels)
Mat C = A;          // C is a shallow copy of A ( rows, cols copied, but shared pixel-pointer )
Mat D; A.copyTo(D); // D is a deep copy of A, like B
edit flag offensive delete link more



Thanks berak, understood :)

izzo gravatar imageizzo ( 2013-02-19 10:02:19 -0500 )edit

But is there a performance difference between copyTo() and clone() ?

kanirudh gravatar imagekanirudh ( 2016-03-08 00:46:31 -0500 )edit

So, it means copy() and clone() does the same task?

Scott_777 gravatar imageScott_777 ( 2020-04-09 02:04:53 -0500 )edit

answered 2016-03-18 03:01:47 -0500

nicolbiden gravatar image

Clone will copy the structure of a data where as Copy will copy the complete structure as well as data. More about....Clone Vs Copy


edit flag offensive delete link more

Question Tools

1 follower


Asked: 2013-02-19 08:50:42 -0500

Seen: 196,631 times

Last updated: Feb 19 '13

Related questions