如何将这两个图像与python numpy和opencv合并? [英] how can I merge this two image with python numpy and opencv?
本文介绍了如何将这两个图像与python numpy和opencv合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个二进制图像.第一个是这样的:
I have two binary images. The first is like this:
最后一个是这样的:
它们没有相同大小的曲线.我想将黑色区域中包含的第二个区域的两个白色区域添加到第一个区域的黑色区域中.
They dont have the same sized curve. I want to add the second one's two white zones contained in the black zone to the first one's black zone.
我的代码是这样运行的,但这是一个错误的答案:
My code runs like this,but this a wrong answer:
问题是这样的,我想获得我用最终最终 图像绘制的最终图像:
The question is like this,and I want get the the finally image which I draw in picture with the the final image:
我如何完成这项任务?
推荐答案
我认为您想要这样做:
#!/usr/local/bin/python3
from PIL import Image,ImageDraw, ImageColor, ImageChops
# Load images
im1 = Image.open('im1.jpg')
im2 = Image.open('im2.jpg')
# Flood fill white edges of image 2 with black
seed = (0, 0)
black = ImageColor.getrgb("black")
ImageDraw.floodfill(im2, seed, black, thresh=127)
# Now select lighter pixel of image1 and image2 at each pixel location and save it
result = ImageChops.lighter(im1, im2)
result.save('result.png')
如果您喜欢 OpenCV ,它可能看起来像这样:
If you prefer OpenCV, it might look like this:
#!/usr/local/bin/python3
import cv2
# Load images
im1 = cv2.imread('im1.jpg', cv2.IMREAD_GRAYSCALE)
im2 = cv2.imread('im2.jpg', cv2.IMREAD_GRAYSCALE)
# Threshold, because JPEG is dodgy!
ret, im1 = cv2.threshold(im1, 127, 255, cv2.THRESH_BINARY)
ret, im2 = cv2.threshold(im2, 127, 255, cv2.THRESH_BINARY)
# Flood fill white edges of image 2 with black
h, w = im2.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
cv2.floodFill(im2, mask, (0,0), 0)
# Now select lighter of image1 and image2 and save it
result = np.maximum(im1, im2)
cv2.imwrite('result.png', result)
这篇关于如何将这两个图像与python numpy和opencv合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文