关于Mat的拷贝、赋值

关于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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值