OpenCV Add columns to a matrix
Solution 1
Use cv::hconcat
:
Mat mat;
Mat cols;
cv::hconcat(mat, cols, mat);
Solution 2
Worst case scenario: rotate the image by 90 degrees and use Mat::resize()
, making columns become rows.
Solution 3
Since OpenCV, stores elements of matrix rows sequentially one after another there is no direct method to increase column size but I bring up myself two solutions for the above matter, First using the following method (the order of copying elements is less than other methods), also you could use a similar method if you want to insert some rows or columns not specifically at the end of matrices.
void resizeCol(Mat& m, size_t sz, const Scalar& s)
{
Mat tm(m.rows, m.cols + sz, m.type());
tm.setTo(s);
m.copyTo(tm(Rect(Point(0, 0), m.size())));
m = tm;
}
And the other one if you are insisting not to include even copying data order into your algorithms then it is better to create your matrix with the big number of rows and columns and start the algorithm with the smaller submatrix then increase your matrix size by Mat::adjustROI
method.
AMCoded
Updated on August 17, 2020Comments
-
AMCoded over 3 years
in OpenCV 2 and later there is method
Mat::resize
that let's you add any number of rows with the default value to your matrix is there any equivalent method for the column. and if not what is the most efficient way to do this. Thanks -
AMCoded over 12 yearsOk but that is worst case, what if my Mat contains CV_64FC1 and the size is too big (500*500) and i have to this for a big number of matrices, so I don't want to include the process of rotating or transposing twice such big matrices in the algorithm.
-
karlphillip over 12 yearsAs I said, worst case. If I knew the best case I would have shared it with you.
-
Vladimir Obrizan over 9 yearsAnd don't try to find docs for this function. It is undocumented.
-
Adi Shavit over 9 yearsHeader code is here: github.com/Itseez/opencv/blob/master/modules/core/include/… . For completeness, vertical concatination can be done with
vconcat()
and alsocv::Mat::push_back()
-
I L over 7 yearsDocumentation: docs.opencv.org/trunk/d2/de8/group__core__array.html