Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “关于生成”的问题;“差异图像”;_Python_Image Processing_Pixel - Fatal编程技术网

Python “关于生成”的问题;“差异图像”;

Python “关于生成”的问题;“差异图像”;,python,image-processing,pixel,Python,Image Processing,Pixel,我正在尝试从.avi视频文件生成一些信息丰富的图像。我想知道是否有一种方法可以拍摄两张灰度图像,并生成一张图像,每一个像素值都是两张选定图像中像素值的差值。即,如果第一个图像中的像素为2%(黑色(0%)和白色(100%),且同一像素处的最后一个图像具有25%,则该像素处生成的图像将为23% 如果您熟悉python,那么它非常简单 import numpy as np img1 = #first grayscale image img2 = #second grayscale image dif

我正在尝试从.avi视频文件生成一些信息丰富的图像。我想知道是否有一种方法可以拍摄两张灰度图像,并生成一张图像,每一个像素值都是两张选定图像中像素值的差值。即,如果第一个图像中的像素为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")

以下是我迄今为止的代码:
import numpy作为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 0 0 0]][[0 0 0 0][0 0 0 0][0 0 0]…理想情况下,我希望diff是一个.jpg图像而不是一个数组,如何进行此转换?