求多幅图像之间的差分图像

2024-04-19 01:48:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我从校园里的一个摄像头上拍了多张截图,我平均拍了300张截图,它给了我一张有很多鬼魂的人的照片。我想弄清楚如何得到图像之间的差异图像,这样我就可以将差异改为红色以显示得更清楚。我只是需要一些帮助,弄清楚从哪里开始。我知道我需要用第二个图像减去第一个图像,然后再减去第三个图像,但是我不确定在Python中有什么方法可以做到这一点。在

import os, os.path, time
import matplotlib.pyplot as mplot
from PIL import Image
import numpy as np

files=os.listdir('./images')
print files

image=[]

for file in files:
    img=Image.open('./images/'+file)
    img=np.float32(img)
    image.append(img)

avg_img=[]

for img in image:
    try:
        avg_img+=img
    except:
        avg_img=img

avg_img/=len(image)
avg_img=np.clip(avg_img, 0, 255)
avg_img=np.uint8(avg_img)
mplot.imshow(avg_img)
mplot.show()

Tags: 图像imageimportimgforosasnp
1条回答
网友
1楼 · 发布于 2024-04-19 01:48:20

有很多方法可以做到这一点,但我将首先保持最接近你目前所拥有的。然后我将展示一种更紧凑的方式。在

import os, os.path, time
import matplotlib.pyplot as mplot
from PIL import Image
import numpy as np

files=os.listdir('./images')
print files

image=[]

for file in files:
    img=Image.open('./images/'+file)
    img=np.float32(img)
    image.append(img)

avg_img=np.zeros_like(image[0])

for img in image:
    avg_img += img

avg_img/=len(image)
avg_img=np.clip(avg_img, 0, 255)
avg_img=np.uint8(avg_img)
mplot.imshow(avg_img)
mplot.show()

# get series of differences
differences = []
for img0, img1 in zip(image[:-1], image[1:]):
    differences.append(img1 - img0)

这里有一个使用numpy的简单方法。在

^{pr2}$

我怀疑你的摄像头图像存储浮动,但可能是这样。在

相关问题 更多 >