cv::Mat + cv::Range does not work as expected for column assignment (Bug #1686)
Description
I have the following code that performs a column assignment to a matrix, but does not work as expected
1#include <iostream>
2#include <opencv2/core/core.hpp>
3
4using namespace std;
5
6int main()
7{
8 cv::Mat X = cv::Mat::zeros(3,4,CV_64F);
9 cv::Mat A = cv::Mat::ones(3,1,CV_64F);
10
11 // This does not work as expected
12 X(cv::Range::all(), cv::Range(3,4)) = A;
13 cout << X << endl;
14
15 cout << endl;
16
17 // This works, but why?
18 X(cv::Range::all(), cv::Range(3,4)) = A*1;
19 cout << X << endl;
20
21 return 0;
22}
The output is:
[0, 0, 0, 0;
0, 0, 0, 0;
0, 0, 0, 0]
[0, 0, 0, 1;
0, 0, 0, 1;
0, 0, 0, 1]
History
Updated by Marina Kolpakova almost 13 years ago
- Description changed from I have the following code that performs a column assignment to a matrix, but ... to I have the following code that performs a column assignment to a matrix, but ... More
Updated by Marina Kolpakova almost 13 years ago
This behavior is expected and documented.
Firstly, X(cv::Range::all(), cv::Range(3,4))
is a sub-matrix with its own header (size, type information and so on) and data referred to original X
.
In following line
1X(cv::Range::all(), cv::Range(3,4)) = A;
the first signature of assignment operator is used
Mat& Mat::operator=(const Mat& m)
. Header and data reference of temporal sub-matrix are updated.
In the second case
1X(cv::Range::all(), cv::Range(3,4)) = A * 1;
the second signature
Mat& Mat::operator=(const MatExpr_Base& expr)
cause A * 1
returned MatExpr. So you actually copy values of A * 1
in current data storrade of matrix X
For more information see at opencv documentation Mat::operator =
- Status changed from Open to Cancelled
- Target version set to 2.4.0
- Assignee set to Marina Kolpakova
Updated by Nghia Ho almost 13 years ago
Thanks. Maybe worth mentioning in the document. Feels like an easy 'gotcha'.
Updated by Kirill Kornyakov almost 13 years ago
It is actually documented: http://opencv.itseez.com/trunk/modules/core/doc/intro.html#automatic-memory-management. But if you feel that some other place should be updated, feel free to point us. If you think that operator= documentation should be updated, please provide your version of the text...