如何在 OpenCV 中将图像复制和粘贴为画中画 [英] How to copy and paste of image as Picture-In-Picture in OpenCV

查看:210
本文介绍了如何在 OpenCV 中将图像复制和粘贴为画中画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import cv2
import numpy as np


img = cv2.imread('C:\Users\SHUSHMA-PC\Desktop\sample\dumb.jpeg',cv2.IMREAD_COLOR)


img[100,100]=[255,255,255]
px=img[100,100]


img[500:550,500:550]=[0,0,0]


dumb_face= img[37:111,108:195]
img[0:74,0:87]=dumb_face

imS = cv2.resize(img, (250, 250))
cv2.imshow("output", imS)

cv2.waitKey()
cv2.destroyAllWindows()

没有错误,只是图像没有复制并显示为正常输出

There is no error, it is just that the image is not copying and appears as normal output

实际上,我想复制整个图像并将其粘贴到图像本身(较小的)上.喜欢图像上的图像.

Actually, I want to copy the entire image and paste it on the image itself(smaller one). Like image on a image.

推荐答案

我认为您正在尝试裁剪图像并将裁剪部分保存为新图像.下面是执行此操作的示例代码.

I think you are trying to crop the image and save the cropped portion as a new image. Below is the sample code to do that.

import cv2
import numpy as np

img = cv2.imread(r'.\dump.png', 1)  # '1' read as color image
h,w = img.shape[:2]
print 'image height and width = %d x %d' % (h, w)  # 318 * 348 pixels

img = img[50:250,50:280]  # crop image at [h1:h2, w1:w2]
cv2.imwrite(r'.\dump_resized.png',img)

这是裁剪和保存的图像.

Here is cropped and saved image.

这是你打算做的吗?

更新:

调整图像大小并将其作为画中画.您可以先调整图像大小,例如 1/10.

To resize the image and put it as a picuture-in-picture one. You may resize the image first, by 1/10 for instance.

resized_image = cv2.resize(img, (h/8, w/8))
h1, w1 = resized_image.shape[:2]

然后将调整后的图像放入原始图像中.

Then put the resized one into the original image.

#set top left position of the resized image
pip_h = 10
pip_w = 10
img[pip_h:pip_h+h1,pip_w:pip_w+w1] = resized_image  # make it PIP
cv2.imwrite(r'.\dump_pip.png',img)

这是生成的 PIP 图像.

Here is the resulted PIP image.

这篇关于如何在 OpenCV 中将图像复制和粘贴为画中画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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