c++ - 如何将垫子中包含的图像复制到更大的垫子上?

标签 c++ qt opencv

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

下一个代码是原始垫子。

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());

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

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

最佳答案

你可以使用

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)

关于c++ - 如何将垫子中包含的图像复制到更大的垫子上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46432168/

相关文章:

iphone - 如何在iphone项目中添加C++文件

c++ - 二进制搜索代码找不到数组的 A[0] 元素

c++ - 我如何制作一个允许所有左值引用、右值引用和 initializer_list 的模板化构造函数?

C++ "expected a declaration"

c++ - 将对象传递给多线程对象Qt

opencv - HOGDescriptor错误和断言失败

c++ - 将字节数组转换为 float

c++ - 使用 QML 的普通(桌面)用户界面控件?

c++ - cv没有成员BackgroundSubtractorMOG

python - 识别文档中的图像并自动将其删除的算法