Deep Copy of OpenCV cv::Mat

前端 未结 3 1291
春和景丽
春和景丽 2021-02-01 03:48

The behaviour of copying cv::Mat is confusing me.

I understand from the documentation that Mat::copyTo() is deep copy while the assignment ope

3条回答
  •  面向向阳花
    2021-02-01 04:27

    Take a look at c++11 std::shared_ptr effectively works in the same way, by using a reference counter cv::Mat cleverly remembers every time the pointer is referenced, once the count reaches 0 it is automatically released i.e. memory is deallocated and cv::Mat is no longer available. This is effectively a "shallow copy" and saves resources in allocating/deallocating large amounts of memory.

    On the other hand cv::Mat::clone will provide a "deep copy" that allocates a whole new block of memory for the matrix to reside in, this can be useful if you are making transformations to an image that you may want to undo however, more memory allocating/deallocating increases the amount of resources required.

    Hope this helps someone.

提交回复
热议问题