简体   繁体   中英

How to make a deep copy of a rectangular region of a cv::Mat?

Using opencv 2.3. I have a region bounded by a rectangle that I want to extract and make a deep copy of, so that when that image is destroyed this rectangular region can live on. I'm using this as a template for matching in a video sequence. How can you do that?

Quoting OpenCV 2.3 online documentation use this constructor to create rectangular region of interest of a cv::Mat:

// select a ROI
Mat roi(img, Rect(10,10,100,100));

and then to make a deep copy do:

Mat deepRoiExplorer=roi.clone();

You can use Mat::clone() to copy a ROI to a new image, or you can create the target image yourself and use Mat::copyTo() :

Mat source...;        // your source image
Rect sourceRect(...); // your ROI

// using clone
Mat target = source(sourceRect).clone();

// using copyTo
Mat target(sourceRect.size(), source.type());
source(sourceRect).copyTo(target);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM