python - 根据两个图像的差异创建Alpha叠加图像

标签 python numpy opencv python-imaging-library

我一直很难解决这个问题。我有两个图像,我们称它们为基准



和光



(实际图像的分辨率要高得多,但是无论如何,问题都应相同)。我的目标是产生一个新图像,我们将其称为alpha,当覆盖在base上时会生成light。我已经尝试过进行各种差异/相减,缩放,亮度调节等操作,但是我似乎并没有实际产生结果(通常看起来很接近,但从不精确)。

例如,此代码与我所使用的代码差不多,而无需使用图像编辑软件来猜测和检查它是否更紧密。

from PIL import Image, ImageEnhance
import numpy
import blend_modes
import sys
import scipy


background_img_raw = Image.open(sys.argv[1])
background_img_raw.putalpha(255)
background_img = numpy.array(background_img_raw)
background_img_float = background_img.astype(float)
foreground_img_raw = Image.open(sys.argv[2])
foreground_img_raw.putalpha(255)
foreground_img = numpy.array(foreground_img_raw)
foreground_img_float = foreground_img.astype(float)
opacity = 1.0
blended_img_float = blend_modes.difference(background_img_float, foreground_img_float, opacity)
r, g, b, a = np.transpose(blended_img_float)
alpha = np.clip((r+g+b)*2, 0, 255)
r = np.clip(r + 100, 0, 255)
g = np.clip(g + 100, 0, 255)
b = np.clip(b + 100, 0, 255)
blended_img_float = np.transpose([r, g, b, alpha])
blended_img = numpy.uint8(blended_img_float)
blended_img_raw = Image.fromarray(blended_img)
blended_img_raw.save(sys.argv[3])

结果看起来像这样



有什么建议么?

编辑:在Paint.net中摆弄,如果您执行以下操作,则可以很接近:
  • Blend->区分图层
  • 在黑色
  • 上使用Grim Color Reaper
  • 最大亮度,最小对比度
  • 色相稍微偏移,使饱和度最大,并增加亮度

  • 这是大量的转换,但是结果比我以编程方式得出的结果要近得多。无论“死神”在做什么,都比我在做的更接近纠正。结果如下:

    不幸的是,这并不能解决问题,因为我需要反复进行此操作,但是它给了我希望它可以解决。

    编辑2:请参阅此帖子以获取我要完成的操作的示例:https://community.home-assistant.io/t/floorplan-with-many-lights-in-one-area-say-hello-to-transparent-png-files/90006/8

    编辑3:我现在需要解决这个问题,但是我认为与其努力工作以制作出合适的图像,我还想出了CSS方面的一种变通方法,可以使混合更接近合适的位置。现在,我只使用像这样的图像之间的差异:
    import numpy as np
    from PIL import Image
    import sys
    
    def create_mask(foreground_filename, background_filename, output_filename):
        foreground = cv2.imread(foreground_filename)
        background = cv2.imread(background_filename)
        diff = foreground - background
        diff = cv2.cvtColor(diff, cv2.COLOR_BGR2RGB)
        out_pil_image = Image.fromarray(diff)
        out_pil_image.save(output_filename)
    
    if __name__ == "__main__":
        create_mask(sys.argv[1], sys.argv[2], sys.argv[3])
    

    结果看起来像这样:https://codepen.io/anon/pen/agBqXr

    目前对我来说已经足够了。如果有人知道我想知道这个问题的正确答案!

    最佳答案

    我不太了解Opecv,但是在ImageMagick中,我将执行以下操作:

    基础:

    enter image description here

    光:

    enter image description here

    convert base.png light.png -compose minus -composite minus.png
    

    enter image description here
    convert base.png minus.png -compose plus -composite result.png
    

    enter image description here

    加成:

    这是一种替代方法,可以在原件上构成彩色透明图像。但是您将不得不稍微调整颜色。我在基础图像中测量了一个亮黄色点。
    1) turn the minus image into gray by desaturating it. The stretch the dynamic range, then apply a gamma adjustment.
    
    2) create a look-up table between black and that yellow color and apply it to the gray image with -clut
    
    3) put the gray image into the alpha channel of the colorized image
    
    4) composite the previous image over the base image
    
    
    convert minus.png -modulate 100,0,100 -auto-level -gamma 1.5 minus_gray.png
    convert minus_gray.png \( xc:black xc:"rgb(251,220,120)" +append \) -clut minus_color.png
    convert minus_color.png minus_gray.png -alpha off -compose copy_opacity -composite alpha.png
    convert base.png alpha.png -compose over -composite result3.png
    

    以下是步骤的结果:

    灰色:

    enter image description here

    彩色灰色:

    enter image description here

    透明:

    enter image description here

    结果:

    enter image description here

    ADDITION2:
    convert light.png -alpha copy -channel a -evaluate multiply 2 +channel alpha.png
    convert base.png alpha.png -compose over -composite result3.png
    

    Α:

    enter image description here

    结果:

    enter image description here

    关于python - 根据两个图像的差异创建Alpha叠加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56659030/

    相关文章:

    python - 导入numpy

    python - 对于 Numpy 的循环速度

    opencv - 动态图像大小调整的 GPU 与 CPU 端到端延迟

    Python 拥有漂亮的 UI + 玻璃效果

    python - 如何使用 django 1.6 测试客户端发出 https 请求?

    python - 是否存在字符串 `s` 使得 eval(repr(s)) 导致任意代码执行?

    python - 多维Python数组的动态访问

    python - 如何忽略列表中的高偏差

    Python:从交通摄像头获取 10 分钟视频

    java - 使用 OpenCV 和 Java 自动透视校正