[英]C++ Opencv - Any Changes to mat, change the vector used to init mat as well
I have a vector data
and I create a cv::mat
: 我有矢量data
并创建了cv::mat
:
cv::Mat HNorm(this->data.size(), 1, CV_32FC1, this->data.data());
Then I normalize HNorm
's values: 然后我将HNorm
的值归一化:
normalize(HNorm, HNorm, 0, 300, cv::NORM_MINMAX, -1, cv::Mat());
The thing is that the same normalization happens to data
vector. 问题是data
向量也发生了相同的归一化。 What I want is to be able to work with HNorm
without affecting data
. 我想要的是能够与HNorm
一起HNorm
而不影响data
。
The particular constructor you use does not copy the data. 您使用的特定构造函数不会复制数据。 It is designed this way for efficiency reasons. 出于效率考虑,采用这种方式进行设计。 This basically just creates the Mat
headers and changes the internal pointer to point to the supplied data
array. 这基本上只是创建Mat
头,并更改内部指针以指向提供的data
数组。 If you want to not change the underlying data in subsequent operations without affecting the input data array, you need to copy it. 如果不想在后续操作中更改基础数据而不影响输入数据数组,则需要对其进行复制。
You could clone
the Mat
object and this will copy the underlying data. 您可以clone
Mat
对象,这将复制基础数据。 See the docs here: 在这里查看文档:
http://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#ad1c9cc37d66c4e5bd05fae36f62d1cb4 http://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#ad1c9cc37d66c4e5bd05fae36f62d1cb4
So something like: 所以像这样:
cv::Mat HNorm(this->data.size(), 1, CV_32FC1, this->data.data());
cv::Mat cloned = HNorm.clone();
// work on the cloned object now
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.