C++ OpenCV 向矩阵添加列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8839769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:11:36  来源:igfitidea点击:

OpenCV Add columns to a matrix

c++visual-c++opencv

提问by AMCoded

in OpenCV 2 and later there is method Mat::resizethat 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

在 OpenCV 2 及更高版本中,有一种方法Mat::resize可以让您将具有默认值的任意数量的行添加到您的矩阵中,是否有任何等效的列方法。如果不是,那么最有效的方法是什么。谢谢

回答by Boris

Use cv::hconcat:

使用cv::hconcat

Mat mat;
Mat cols;

cv::hconcat(mat, cols, mat);

回答by karlphillip

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

最坏情况:将图像旋转 90 度并使用Mat::resize(),使列变成行。

回答by AMCoded

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.

由于OpenCV,一个接一个地存储矩阵行的元素,没有直接增加列大小的方法,但我针对上述问题提出了两种解决方案,首先使用以下方法(复制元素的顺序少于其他方法) ,如果您想在矩阵末尾插入一些行或列,您也可以使用类似的方法。

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::adjustROImethod.

另一个如果您坚持不将甚至将数据顺序复制到您的算法中,那么最好创建具有​​大量行和列的矩阵,并使用较小的子矩阵启动算法,然后通过Mat::adjustROI方法增加矩阵大小。