user189320
user189320

Reputation:

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?

Upvotes: 3

Views: 10048

Answers (2)

Michal Kottman
Michal Kottman

Reputation: 16753

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);

Upvotes: 6

morynicz
morynicz

Reputation: 2332

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();

Upvotes: 12

Related Questions