Python 在基板上插入图像

Python 在基板上插入图像,python,image,numpy,opencv,python-imaging-library,Python,Image,Numpy,Opencv,Python Imaging Library,请帮助我,我需要在基板上插入图像 基质: 它是png,在城市空白的区域中,必须从帧的边到边插入图像 问题是,我找不到一个如何将图像插入到给定基板角点的已知坐标点的示例 请帮忙 我的测试图像 进口cv2 将numpy作为np导入 从撇渣进口io frame=cv2.cvtColor(io.imread('as.png')、cv2.COLOR\u RGB2BGR) image=cv2.cvtColor(io.imread(“Vw5Rc.jpg”),cv2.COLOR\u RGB2BGR) 掩码

请帮助我,我需要在基板上插入图像

基质:

它是png,在城市空白的区域中,必须从帧的边到边插入图像

问题是,我找不到一个如何将图像插入到给定基板角点的已知坐标点的示例

请帮忙

我的测试图像


进口cv2
将numpy作为np导入
从撇渣进口io
frame=cv2.cvtColor(io.imread('as.png')、cv2.COLOR\u RGB2BGR)
image=cv2.cvtColor(io.imread(“Vw5Rc.jpg”),cv2.COLOR\u RGB2BGR)
掩码=255*np.uint8(np.all(帧==0,0,0],轴=2))
轮廓,u=cv2.查找轮廓(遮罩、cv2.RETR\u列表、cv2.链近似\u简单)
cnt=最小值(轮廓,关键点=cv2.轮廓面积)
(x,y,w,h)=cv2.boundingRect(cnt)
#将适当调整大小的图像复制到框架
帧[y:y+h,x:x+w]=cv2。调整大小(图像,(w,h))
cv2.imwrite('frame.png',frame)
我试着按颜色找到插入图像的区域,我能找到的区域的红色,如果没有颜色


静态帧的大小是恒定的。

如果我理解您的要求,这里有一种在Python/OpenCV中实现的方法

Read the substrate and trees images

Extract the alpha channel from the substrate

Extract the substrate image without the alpha channel

Use the alpha channel to color the base substrate image white where the alpha channel is black to correct a flaw in the base image

Threshold the alpha channel and invert it

Use morphology to remove the grid lines so that there is only one "outer" contour.

Extract the contour and its bounding box

Resize the trees image to the size of the bounding box.

Use numpy indexing and slicing to multiply the region of the substrate with the resized trees image.

Save the results.

Optionally, display the various images.

基板图像:

树木图片:


结果:


请注意,树木图像存在失真,因为其纵横比与对应于轮廓边界框的基板图像区域不匹配。可以更改此选项以保持纵横比,但随后需要将图像填充为白色或其他颜色,以填充边界框的剩余区域。

如果我理解您的要求,可以在Python/OpenCV中执行此操作

Read the substrate and trees images

Extract the alpha channel from the substrate

Extract the substrate image without the alpha channel

Use the alpha channel to color the base substrate image white where the alpha channel is black to correct a flaw in the base image

Threshold the alpha channel and invert it

Use morphology to remove the grid lines so that there is only one "outer" contour.

Extract the contour and its bounding box

Resize the trees image to the size of the bounding box.

Use numpy indexing and slicing to multiply the region of the substrate with the resized trees image.

Save the results.

Optionally, display the various images.

基板图像:

树木图片:


结果:


请注意,树木图像存在失真,因为其纵横比与对应于轮廓边界框的基板图像区域不匹配。这可以更改以保持纵横比,但随后需要将图像填充为白色或其他颜色,以填充边界框的剩余区域。

欢迎使用堆栈溢出。请参加本教程并阅读帮助中心()中的信息指南。如果你(1)自己展示一些研究成果,以及(2)学会如何提问,用户就更有可能提供帮助。请为您的具体问题提供一个最小、完整且可验证的示例。到目前为止你试过什么?关于您的问题,只需测量基板中网格的角。然后得到彩色图像的角点。然后使用透视扭曲通过乘法对齐和合并两个。@fmw42更新)我没有看到任何红色。@fmw42当我尝试使用红色帧时,现在帧如图1所示figure@fmw42有关红色边框的更多信息,请参阅。关于这个问题:你的基板似乎有一些透明度,正是你想嵌入你的图像。因此,使用
cv2.imread
cv2.imread\u不变的模式加载带有alpha通道的基板,使用alpha通道找到合适的ROI,然后将图像插入其中。如果卡住了,至少提供所需的输出。是否要维护插入图像上方的城市、网格等?欢迎使用堆栈溢出。请参加本教程并阅读帮助中心()中的信息指南。如果你(1)自己展示一些研究成果,以及(2)学会如何提问,用户就更有可能提供帮助。请为您的具体问题提供一个最小、完整且可验证的示例。到目前为止你试过什么?关于您的问题,只需测量基板中网格的角。然后得到彩色图像的角点。然后使用透视扭曲通过乘法对齐和合并两个。@fmw42更新)我没有看到任何红色。@fmw42当我尝试使用红色帧时,现在帧如图1所示figure@fmw42有关红色边框的更多信息,请参阅。关于这个问题:你的基板似乎有一些透明度,正是你想嵌入你的图像。因此,使用
cv2.imread
cv2.imread\u不变的模式加载带有alpha通道的基板,使用alpha通道找到合适的ROI,然后将图像插入其中。如果卡住了,至少提供所需的输出。是否要在插入的图像上方维护城市、网格等?这也是我理解的所需输出。我的解决方案看起来几乎是一样的。这也是我所理解的期望输出。我的解决方案看起来几乎是一样的。