gpt4 book ai didi

python - 使用OpenCV用另一个图像替换纯绿色区域

转载 作者:行者123 更新时间:2023-12-02 16:46:23 31 4
gpt4 key购买 nike

下图有一个绿色区域,我希望将其替换为其他任何图像。它的观点没有必要匹配。

enter image description here

我已经能够创建一个面具。但是,在调整另一个图像的大小并使其与绿色区域对齐方面并没有真正成功。我在网上找到的大多数资源都提到了两张图片都需要相同的尺寸,但是我只想调整新图片的大小以适合绿色矩形,而不是让两个方形图片与其中一个带有切口的方形图片重叠。

enter image description here

这里有什么好的方法?

最佳答案

这是使用Python OpenCV的一种解决方案。

Read both images.

Measure and enter 4 corresponding sets of x,y control points.

Compute homography (perspective coefficients)

Warp the source image using the homography -- the background will be black

Create a binary mask from the dst image using the green color range.

Invert the mask.

Apply the inverted mask to the dst image to blacken the inside of the region of interest (where the src will go)

Add the warped src to the masked dst to form the result

src:

enter image description here

dst:

enter image description here
#!/python3.7

import cv2
import numpy as np


# Read source image.
src = cv2.imread('original.jpg')

# Four corners of source image
# Coordinates are in x,y system with x horizontal to the right and y vertical downward
# listed clockwise from top left
pts_src = np.float32([[0, 0], [325, 0], [325, 472], [0, 472]])


# Read destination image.
dst = cv2.imread('green_rect.png')

# Four corners of destination image.
pts_dst = np.float32([[111, 59], [206, 60], [216, 215], [121, 225]])

# Calculate Homography if more than 4 points
# h = forward transformation matrix
#h, status = cv2.findHomography(pts_src, pts_dst)

# Alternate if only 4 points
h = cv2.getPerspectiveTransform(pts_src,pts_dst)


# Warp source image to destination based on homography
# size argument is width x height, so have to reverse shape values
src_warped = cv2.warpPerspective(src, h, (dst.shape[1],dst.shape[0]))


# Set BGR color ranges
lowerBound = np.array([0, 255, 0]);
upperBound = np.array([0, 255, 0]);

# Compute mask (roi) from ranges in dst
mask = cv2.inRange(dst, lowerBound, upperBound);

# Dilate mask, if needed, when green border shows
kernel = np.ones((3,3),np.uint8)
mask = cv2.dilate(mask,kernel,iterations = 1)

# Invert mask
inv_mask = cv2.bitwise_not(mask)

# Mask dst with inverted mask
dst_masked = cv2.bitwise_and(dst, dst, mask=inv_mask)

# Put src_warped over dst
result = cv2.add(dst_masked, src_warped)

# Save outputs
cv2.imwrite('warped_src.jpg', src_warped)
cv2.imwrite('inverted_mask.jpg', inv_mask)
cv2.imwrite('masked_dst.jpg', dst_masked)
cv2.imwrite('perspective_composite.jpg', result)

warped_src:

enter image description here

reverse_mask:

enter image description here

masked_dst:

enter image description here

结果:

enter image description here

我将它留给读者以过滤多余的绿色边框或编辑dst图像中的控制点以使感兴趣的区域更大。

注意:如果src的长宽比与绿色矩形的长宽比不匹配,则此方法会使src变形。

关于python - 使用OpenCV用另一个图像替换纯绿色区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57262520/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com