如何使用x,y坐标在另一个图像上添加图像? [英] How to add an image over another image using x,y coordinates?

查看:57
本文介绍了如何使用x,y坐标在另一个图像上添加图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用openCV和python在xyz.jpg上添加图像'abc.jpg'.我有坐标x,y,我必须在该坐标上添加图像,并且还要调整我的"abc.jpg"的大小,以使其适合图像.现在如何添加它?

I want to add image 'abc.jpg' on xyz.jpg using openCV and python. I have got the coordinates x,y on which I have to add the image and also resized my 'abc.jpg' so that it will fit on the image. Now how can I add it?

推荐答案

在计算机上,图像只是一个数字网格.有几种加"数字网格的方法.在这个答案中,我将解释在图像"xyz"上添加图像"abc"的三种方法.这是一个非常简单的任务a + b = c.但是,这仅在图像具有相同形状的情况下才有效.要处理不同形状的图像,仅应使用代码 image [y:y + height,x:x + width] 修改图像的某些部分.

To computers, images are just a grid of numbers. There are a few ways to 'add' a grid of numbers. In this answer, I will explain three ways to add image 'abc' on image 'xyz'. This is a very simple task a + b = c. But, that only works if the images are the same shape. To work with images of different shapes, only certain parts of the images should be modified using the code image[y: y+height, x: x+width].

首先,让我们看一下我创建的示例图像.图像 xyz 具有垂直条,形状为600,600.条形为颜色123(0为黑色,255为白色).

To start, let's take a look at the sample images I created. Image xyz has vertical bars and a shape of 600,600. The bars are the color 123 (where 0 is black and 255 is white).

接下来,我创建了另一个图像,添加到图像 xyz 的顶部.此图像称为图像 abc .它的形状为300,300.水平条也是颜色123:

Next, I created another image to add on top of image xyz. This image is called image abc. It has a shape of 300,300. The horizontal bars are also the color 123:

您可以通过将 xyz 图像中的像素替换为 abc 图像中的像素来添加"图像:

You can 'add' the images by replacing the pixels in the xyz image with the pixels in the abc image:

x,y = 123,123
replace = xyz.copy()
replace[y: y + abc_size, x: x + abc_size] = abc
cv2.imshow('replace', replace)

您可以通过对数组求和来添加"图像.这将导致图像的位置比任何一个源图像都亮.如果值超出范围(0,255),则求和将产生奇怪的结果.

You can 'add' the images by summing the arrays. This will result in an image that is brighter in places than either of the source images. Summing will produce odd results if the values go out of the range (0, 255).

x,y = 123,123
added = xyz.copy()
added[y: y + abc_size, x: x + abc_size] += abc
cv2.imshow('added', added)

如果要平均图像中的像素,可以使用cv2.addWeighted()函数.

If you want to average the pixels in the images you can use the cv2.addWeighted() function.

background = np.zeros_like(xyz)
x,y = 123,123
background[y: y + abc_size, x: x + abc_size] = abc
add_weighted = cv2.addWeighted(background, .5, xyz, .5, 1)
cv2.imshow('add_weighted', add_weighted)

这篇关于如何使用x,y坐标在另一个图像上添加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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