如何使用Python中的opencv或PIL提取融合到另一个图像中的图像

问题描述 投票:0回答:1

我有一个base图像和一个result图像。

创建“结果”图像,将“第二”图像混合到“基本”图像中。我正在寻找一种从“结果”图像中提取“第二”图像的方法。

我尝试过此代码:

from PIL import Image, ImageChops
from pathlib import Path

HOME = Path().cwd()

img1 = Image.open(HOME / 'base.png')
img2 = Image.open(HOME / 'result.png')

diff = ImageChops.difference(img2, img1)

diff.show()
diff.save(HOME / 'second.png')

[并获得这张"second"图像作为两者的差异。

不幸的是,仍然可以看到基本图像中的蓝色水平线和红色垂直线的残差。Here is an enlarged section

我如何摆脱这些?预先感谢您的帮助。

python opencv image-processing python-imaging-library
1个回答
0
投票

您可以尝试在装有订书钉的图像上使用二进制蒙版。

import cv2 

img1 = cv2.imread('AD3iT.png')
img2=cv2.imread('result.png')
diff=cv2.absdiff(img1, img2)
diff=cv2.cvtColor(diff,cv2.COLOR_BGR2GRAY)
mask=cv2.threshold(diff, 20, 255, cv2.THRESH_BINARY )[1]
mask=cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
out=cv2.bitwise_and(img2, mask)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.