【发布时间】:2020-02-07 03:38:10
【问题描述】:
我正在尝试从 .avi 视频文件生成一些信息丰富的图像。我想知道是否有一种方法可以拍摄两张灰度图像 - 并生成一个图像,每个像素值都是两个选定图像中像素值的差异。 IE。如果第一个图像中的像素为 2%(黑色 (0%) 和白色 (100%)),并且同一像素的最后一个图像具有 25%,则该像素处生成的图像将为 23%。
【问题讨论】:
标签: python image-processing pixel
我正在尝试从 .avi 视频文件生成一些信息丰富的图像。我想知道是否有一种方法可以拍摄两张灰度图像 - 并生成一个图像,每个像素值都是两个选定图像中像素值的差异。 IE。如果第一个图像中的像素为 2%(黑色 (0%) 和白色 (100%)),并且同一像素的最后一个图像具有 25%,则该像素处生成的图像将为 23%。
【问题讨论】:
标签: python image-processing pixel
如果你熟悉 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] ...
这是最终工作的代码:
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")
【讨论】: