我正在用C++编写代码,并试图将图像2调整为与图像1相同的尺寸,但是我不想拉伸(stretch)图像。我试图将image2复制到填充的矩阵(在0,0点)。正在收到错误:



代码如下。提前致谢

    Mat padded;
    padded.setTo(cv::Scalar::all(0));
    padded.create(image1.rows,image1.cols, image2.type());
    image2.copyTo(padded(Rect(0, 0, image2.rows, image2.cols)));

最佳答案

您可以使用OpenCV函数 copyMakeBorder 填充图像:

若要实现您想要的,您可以尝试以下方法:

cv::Mat padded;

//Assuming that dimensions of image1 are larger than that of image2
//Calculate padding amount so that total size after padding is equal to image1's size
int rowPadding = image1.rows - image2.rows;
int colPadding = image1.cols - image2.cols;

cv::copyMakeBorder(image2, padded, 0, rowPadding, 0, colPadding, cv::BORDER_CONSTANT, cv::Scalar::all(0));

09-27 16:51