Reputation: 51
I want to change my matrix via sub-matrices step by step. But there is no change on the pixel values. Output pixel values are the same as input pixel values. Also my "wavenoise" function pretty working.
Here is my code:
cv::Mat wave_trans = Mat::zeros(nr, nc, CV_64FC1);
for (int i = 0; i < L; i++){
Range Hhigh = Range(nc / 2, nc-1);
Range Hlow = Range(0, nc / 2 - 1);
Range Vhigh = Range(nr / 2, nr-1);
Range Vlow = Range(0, nr / 2 - 1);
Mat wave_trans_temp1 = Mat(wave_trans, Vlow, Hhigh);
wave_trans_temp1 = wavenoise(wave_trans_temp1, NoiseVar);
Mat wave_trans_temp2 = Mat(wave_trans, Vhigh, Hlow);
wave_trans_temp2 = wavenoise(wave_trans_temp2, NoiseVar);
Mat wave_trans_temp3 = Mat(wave_trans, Vhigh, Hhigh);
wave_trans_temp3 = wavenoise(wave_trans_temp3, NoiseVar);
nc = nc / 2;
nr = nr / 2;
}
Upvotes: 3
Views: 82
Reputation: 19041
When working with cv::Mat
, it is important to keep in mind that it is a reference counted handle to the underlying data array.
As such, there are two (for our purpose) overloads of the assignment operator, which have significantly different behaviours.
The first one takes a matrix:
cv::Mat& cv::Mat::operator= (const cv::Mat& m)
Matrix assignment is an O(1) operation. This means that no data is copied but the data is shared and the reference counter, if any, is incremented.
And the second one takes a matrix expression:
cv::Mat& cv::Mat::operator= (const cv::MatExpr& expr)
As opposite to the first form of the assignment operation, the second form can reuse already allocated matrix if it has the right size and type to fit the matrix expression result.
Hence, an expression such as
nc = nc / 2;
will update the values of nc
, since nc / 2
is a cv::MatExpr
.
However, when we assign a cv::Mat
, such as one returned from some function
cv::Mat foo(cv::Mat m);
// ...
void baz(cv::Mat input) {
cv::Mat bar(input);
bar = foo(bar); // bar now points to whatever foo returned, input is not changed
}
To solve this problem, you can use cv::Mat::copyTo
to copy the result of your function into the submatrix/view.
For example
Mat wave_trans_temp3 = Mat(wave_trans, Vhigh, Hhigh);
wavenoise(wave_trans_temp3, NoiseVar).copyTo(wave_trans_temp3);
Upvotes: 1