Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Add columns to a matrix

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

like image 861
AMCoded Avatar asked Jan 12 '12 17:01

AMCoded


3 Answers

Use cv::hconcat:

Mat mat;
Mat cols;

cv::hconcat(mat, cols, mat);
like image 195
Boris Avatar answered Nov 13 '22 03:11

Boris


Worst case scenario: rotate the image by 90 degrees and use Mat::resize(), making columns become rows.

like image 42
karlphillip Avatar answered Nov 13 '22 05:11

karlphillip


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.

like image 2
AMCoded Avatar answered Nov 13 '22 03:11

AMCoded