OpenCV:使用OpenCV合并两个图像 [英] OpenCV: Merging two images using OpenCV

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

问题描述

使框2的大小等于Box 1的最佳方法是什么,以便CvCopy();可以应用:

What is the best way to make the size of box 2 equal to Box 1 so that CvCopy(); can be applied:

编辑:

也许我没有正确提出问题。所以我正在重新措辞。鉴于Image 2(如上所示),我想将其转换为如下所示:

Perhaps I did not put the question properly. So I am re-phrasing it. Given an Image 2 (as shown above), I want to convert it as shown below:

基本上我必须在其上添加黑色边框。调整大小将无法正常工作,因为我的图像会被扭曲。换句话说,我必须在图像周围定义thikness中添加零。

Basically I have to add a black border across it. resize will not work as my image will get distorted. Putting in other words, I have to add zeros in a define thikness around the image.

此外,虽然显示为空,但这些图像(方框)内部可能有一些对象,我没有显示。

Also although shown empty, these images(boxes) may have some objects inside them, which I have not shown.

推荐答案

您可以使用 copyMakeBorder 函数来执行此操作。我不知道dotnet,下面是python中的示例代码。另请访问以上功能的文档: DOCS

You can use copyMakeBorder function to do so. I don't know dotnet, below is a sample code in python. Also visit docs for above function : DOCS

首先加载两张图片。 imb 是大图像, ims 是小图片。

First load the two images. imb is the big image, and ims is the small image.

import cv2
import numpy as np

imb = cv2.imread('messi4.jpg')
ims = cv2.imread('template.jpg')

现在让 rb,cb 是大图像的行数和列数,对于小图像,类似 rs,cs

Now let rb,cb be the number of rows and columns of big image, and similarly rs,cs for small image.

rb,cb = imb.shape[:2]
rs,cs = ims.shape[:2]

最后,要使用 copyMakeBorder 函数,需要添加行数和列数在顶部,底部,左侧和右侧。所以我们需要找到它们。

Finally, to use the copyMakeBorder function, you need the number of rows and columns to be added at top,bottom,left and right. So we need to find them.

top = (rb-rs)/2
bottom = rb - rs - top
left = (cb-cs)/2
right = cb - cs - left

最后应用该函数。

res = cv2.copyMakeBorder(ims,top,bottom,left,right,cv2.BORDER_CONSTANT)

现在看结果:

原始小图片

修改后的新图片

< img src =https://i.stack.imgur.com/niw4m.jpgalt =在此处输入图像说明>

它具有相同的大小我的大图像(没有在这里显示,认为没有必要,如果你想,我可以上传)

It has the same size of that of my big image ( hasn't shown here, thought it won't be necessary, if you want, i can upload)

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

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