首页 > 解决方案 > 使用 OpenCV 用另一个图像替换纯绿色区域

问题描述

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

在此处输入图像描述

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

在此处输入图像描述

这里有什么好的方法?

标签: pythonopencv

解决方案


这是使用 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


源代码:

在此处输入图像描述

夏令时:

在此处输入图像描述

#!/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:

在此处输入图像描述

倒置掩码:

在此处输入图像描述

masked_dst:

在此处输入图像描述

结果:

在此处输入图像描述

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

注意:如果 src 的纵横比与绿色矩形的纵横比不匹配,那么 src 会被这种方法扭曲。


推荐阅读