OpenCV-如何合并两个图像 [英] Opencv - how to merge two images

查看:440
本文介绍了OpenCV-如何合并两个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是opencv的新手,我在互联网上搜索了如何合并两个图像的示例,但没有找到任何有用的方法来帮助我.有人可以帮我一些指示或小代码来帮助我理解吗?预先感谢

I'm new to opencv and i searched on the internet if there is an example of how to merge two images, but didin't found anything good to help me. Can someone help me with some indications or a small code to understand ? thanks in advance

推荐答案

从评论到问题,您说:

我不想将第一张图片中的一半与第二张图片中的另一半融合在一起.我只是想打印两张图像,一张紧挨着另一张

I dont want to blend half from the first picture with the other half from the second. I just waint to print both images, one near the other one

因此,从这些图像开始:

So, starting from these images:

您想要这个结果吗?

请注意,如果两个图像的高度相同,则不会看到黑色背景.

Note that if both images have the same height, you won't see the black background.

代码:

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    // Load images
    Mat3b img1 = imread("path_to_image_1");
    Mat3b img2 = imread("path_to_image_2");

    // Get dimension of final image
    int rows = max(img1.rows, img2.rows);
    int cols = img1.cols + img2.cols;

    // Create a black image
    Mat3b res(rows, cols, Vec3b(0,0,0));

    // Copy images in correct position
    img1.copyTo(res(Rect(0, 0, img1.cols, img1.rows)));
    img2.copyTo(res(Rect(img1.cols, 0, img2.cols, img2.rows)));

    // Show result
    imshow("Img 1", img1);
    imshow("Img 2", img2);
    imshow("Result", res);
    waitKey();

    return 0;
}

这篇关于OpenCV-如何合并两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆