关于Mat的拷贝、赋值

来源:互联网 发布:阿德昆托博生涯数据 编辑:程序博客网 时间:2024/05/16 07:44

关于Mat的浅复制(占用相同内存,处理其中一个,会影响另外一个)&

                   深复制(生成副本,占用不同内存,相互之间无影响)

     Mat A, C;                                                                  // creates just the header parts

     A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // here we’ll know the method used (allocate matrix)

     Mat B(A);                                                                 // Use the copy constructor   拷贝构造

     C = A;                                                                     // Assignment operator          赋值运算符

opencv手册中描述如下:(浅复制

(All the above objects, in the end, point to the same single data matrix. Their headers are different, however, and
making a modification using any of them will affect all the other ones as well. In practice the different objects just
provide different access method to the same underlying data.)

也可以只取原图像的一部分区域(ROI),创建the headers ,如下:(浅复制

     Mat D (A, Rect(10, 10, 100, 100) );                          // using a rectangle

     Mat E = A(Range:all(), Range(1,3));                        // using row and column boundaries


如果想复制整个矩阵,需要(深复制):opencv提供了两个函数the clone()and copyTo()

 Mat F = A.clone();

 Mat G;

 A.copyTo(G);

现在修改F或者G不会影响A。

opencv手册总结如下:

• Output image allocation for OpenCV functions is automatic (unless specified otherwise).

• You do not need to think about memory management with OpenCVs C++ interface.(IplImage类型要手动释放内存,cvReleaseImage(IplImage **))

• The assignment operator and the copy constructor only copies the header.

• The underlying matrix of an image may be copied using theclone()and copyTo()functions.

0 0
原创粉丝点击