关于生成 "difference images" 的问题

Question about generating "difference images"

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

如果您熟悉 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
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")