【问题标题】:How can I copy an image contained in a mat, on a bigger mat?如何在更大的垫子上复制垫子中包含的图像?
【发布时间】:2017-09-26 17:06:33
【问题描述】:

我正在尝试复制一个较小的图像,包含在一个垫子中,另一个较大的图像包含在另一个垫子中。

下一个代码是原始垫子。

cv::Mat mat(image.height(),image.width()+750,
            CV_8UC3,image.bits(), image.bytesPerLine());

这就是我想在前一个垫子中复制的内容:

QImage your_image;
your_image.load(":/Sombrero.png");

your_image = your_image.convertToFormat(
    QImage::Format_RGBA8888, Qt::ThresholdDither|Qt::AutoColor);

cv::Mat mat_im(your_image.height(),your_image.width(),CV_8UC3,your_image.bits(),
               your_image.bytesPerLine());

如您所见,更改图像的格式,使其与存储在原始图像中的图像相同,但它不起作用。

这个问题是不同的,因为我不想将图像放在常规图像上,就像在其他问题中一样,我想将图像的垫子放在另一个垫子图像上......

【问题讨论】:

标签: c++ qt opencv


【解决方案1】:

你可以使用

Mat(Rect) 指定一个 ROI 并复制它们。喜欢

Mat big, small; 
cv::Rect rect;

small.copyTo(big(Rect));

必须初始化大垫子和小垫子。 Rect 必须是小垫子的宽度和高度,x 和 y 是大垫子的原点。

您必须检查垫子的大小(如果较小的垫子适合原点处的大垫子)并且垫子应该具有相同的位深度。

编辑: 完整示例

QImage src;
src.load(":/Sombrero.png"); // load to QImage

src = src.convertToFormat(QImage::Format::Format_RGB888); //convert to get sure the format
cv::Mat small(src.height(), src.width(), CV_8UC3, src.scanLine(0)); // convert the QImage to Mat

cv::Mat big(small.rows + 10, small.cols + 10, small.type(), cv::Scalar(0, 0, 0)); // generate a (10,10)px bigger mat
small.copyTo(big(cv::Rect(5, 5, small.cols, small.rows))); // copy the small to the big, you get a 5px black boarder

QImage result = QImage(big.data, big.cols, big.rows, 3 * big.cols, QImage::Format_RGB888); // if you want it back as QImage (3 is the count of channels)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-05
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2021-04-05
    相关资源
    最近更新 更多