python - 将图像插入基材

标签 python image numpy opencv python-imaging-library

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

基质:

enter image description here

它为png,并且在城市空白的区域中,必须从边框的另一边插入图像。

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

请帮助))

我的测试图片

enter image description here


import cv2
import numpy as np
from skimage import io      

frame = cv2.cvtColor(io.imread('as.png'), cv2.COLOR_RGB2BGR)
image = cv2.cvtColor(io.imread("Vw5Rc.jpg"), cv2.COLOR_RGB2BGR)

mask = 255 * np.uint8(np.all(frame == [0, 0, 0], axis=2))

contours, _ = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnt = min(contours, key=cv2.contourArea)
(x, y, w, h) = cv2.boundingRect(cnt)

# Copy appropriately resized image to frame
frame[y:y+h, x:x+w] = cv2.resize(image, (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.

基材图像:

enter image description here

树木图片:

enter image description here
import cv2
import numpy as np

# load substrate with alpha channel
substrate = cv2.imread("substrate.png", cv2.IMREAD_UNCHANGED)
hh, ww, cc = substrate.shape

# load colored image
trees = cv2.imread("trees.jpg")

# make img white where alpha is black to merge the alpha channel with the image
alpha = substrate[:,:,3]
img = substrate[:,:,0-2]
img[alpha==0] = 255
img = cv2.merge((img,img,img))

# threshold the img
ret, thresh = cv2.threshold(alpha,0,255,0)

# invert thresh
thresh = 255 - thresh

# make grid lines white in thresh so will get only one contour
kernel = np.ones((9,9), np.uint8)
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

# find one outer contour
cntrs = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]

# get bounding box of contour of white rectangle in thresh
for c in cntrs:
    x,y,w,h = cv2.boundingRect(c)
    #cv2.rectangle(img, (x,y), (x+w,y+h),(0, 0, 255), 2)

# resize trees
trees = cv2.resize(trees,(w,h),0,0)

# generate result
result = img.copy()
result[y:y+h, x:x+w] = img[y:y+h, x:x+w]/255 * trees

# write result to disk
cv2.imwrite("substrate_over_trees.jpg", result)

cv2.imshow("ALPHA", alpha)
cv2.imshow("IMG", img)
cv2.imshow("THRESH", thresh)
cv2.imshow("TREES", trees)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

enter image description here

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

关于python - 将图像插入基材,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58600556/

相关文章:

python - 使用 opencv 或创建线性渐变蒙版

python - 删除不必要的内部标签

python - 如何过滤复合键?

javascript - 使用带有变量的javascript更改图像src

css - 透明可扩展的包装div,怎么样?

python - Numpy 数组与 Python 数组

python - 为什么这不是用 python 打印的?

python - 按包含字符串的最长元素过滤列表

android - Android中的随机图像?

python - 从 3 维数组中选择一个 2 维数组