简体   繁体   中英

OpenCV mat concatenation of a matrix with a zero matrix in C++

I need to pad a matrix vertically with zeros at the top and bottom.

But this code fails with an CV_Assert because padding is "empty", that is filled with zeros:

cv::Mat dataMat;
//...
cv::Mat padding(dataMat.rows, dataMat.cols, datumMat.type(), 0);
std::vector<cv::Mat> matrices;
matrices.push_back(padding);
matrices.push_back(dataMat);
matrices.push_back(padding);
cv::Mat resultMat;
cv::vconcat(matrices, resultMat); 

Is there a way to use vconcat to pad with zero matrices or do I have to copy-paste and hack vconcat?

You can use copyMakeBorder :

int top_padding = 3;
int bottom_padding = 3;
copyMakeBorder(dataMat, dataMat, top_padding, bottom_padding, 0, 0, BORDER_CONSTANT, Scalar(0,0,0,0));

If you want to zero-initialize a matrix without knowing the type at compile time, you should use zeros :

Mat padding = Mat::zeros(dataMat.rows, dataMat.cols, datumMat.type());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM