Adding some functions "ones_like" just like in numpy (Feature #3697)
Description
Sometimes when I want to get a new matrix of zeros/ones, which is of the same size and type as another matrix, my code is often as follows:
//there is already a Mat "a"
Mat b=Mat::ones(a.size(), a.type()); //or zeros
In numpy, I can easily do:
@
In [121]: a
Out121:
array([[0, 1, 2],
[3, 4, 5]])
In [122]: np.ones_like(a)
Out122:
array([[1, 1, 1],
[1, 1, 1]])
@
There are three other similar methods: np.zeros_like, np.full_like, np.empty_like. I think it's better to add some static methods to do the same job, just as simply as:
@
MatExpr Mat::ones_like(const Mat& src)
{
return Mat::ones(src.size(), src.type());
}
@
Isn't that good?
Associated revisions
Merge pull request #3697 from tomi44g:2.4
History
Updated by Chen ZHANG almost 11 years ago
Sometimes when I want to get a new matrix of zeros/ones, which is of the same size and type as another matrix, my code is often as follows:
//assuming there is already a Mat "a" Mat b=Mat::ones(a.size(), a.type()); //or zeros
In numpy, I can easily do:
In [121]: a Out[121]: array([[0, 1, 2], [3, 4, 5]]) In [122]: np.ones_like(a) Out[122]: array([[1, 1, 1], [1, 1, 1]])
There are three other similar methods: np.zeros_like, np.full_like, np.empty_like. I think it's better to add some static methods to do the same job, just as simply as:
MatExpr Mat::ones_like(const Mat& src) { return Mat::ones(src.size(), src.type()); }
Isn't that good?
Updated by Vladislav Vinogradov almost 11 years ago
Hello Chen ZHANG,
The new ones_like
function doesn't add new functionality and doesn't reduce number of code lines for code that uses it.
You can implement such helper function in your project and use it, but I see no need of such function in OpenCV.
- Status changed from Open to Cancelled