【问题标题】:Question about generating "difference images"关于生成“差异图像”的问题
【发布时间】:2020-02-07 03:38:10
【问题描述】:

我正在尝试从 .avi 视频文件生成一些信息丰富的图像。我想知道是否有一种方法可以拍摄两张灰度图像 - 并生成一个图像,每个像素值都是两个选定图像中像素值的差异。 IE。如果第一个图像中的像素为 2%(黑色 (0%) 和白色 (100%)),并且同一像素的最后一个图像具有 25%,则该像素处生成的图像将为 23%。

【问题讨论】:

    标签: python image-processing pixel


    【解决方案1】:

    如果你熟悉 python,那就很简单了。

    import numpy as np
    img1 = #first grayscale image
    img2 = #second grayscale image
    
    diff = np.abs(img1.astype(np.uint) - img2.astype(np.uint)).astype(np.uint8)
    #diff has the required difference data
    #here is the code to save an image (simply chage the extension at "filename.***" to save in the required format)
    cv2.imwrite("filename.jpg",diff)
    

    【讨论】:

    • 这是我目前的代码:import numpy as np img1 = cv2.imread('img1.jpg)' img2 = cv2.imread('img2.jpg') diff = np.abs(img1.astype(np.uint) - img2.astype(np.uint)).astype(np.uint8) print(diff) 但是,即使图像不同,我也会得到以下输出:`[[[0 0 0] [0 0 0] [0 0 0] .. . [0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0] ... [0 0 0] [0 0 0] [0 0 0 ]] [[0 0 0] [0 0 0] [0 0 0] ...
    • 理想情况下,我希望 diff 是 .jpg 图像而不是数组,我该如何进行这种转换?
    【解决方案2】:

    这是最终工作的代码:

    import numpy as np
    from PIL import Image
    
    img1 = cv2.imread('img1.jpg')
    img2 = cv2.imread('img2.jpg')
    
    diff = np.abs(img1.astype(np.uint) - img2.astype(np.uint)).astype(np.uint8)
    
    img = Image.fromarray(diff)
    img.save("diff.png")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多