如何通过移除移动物体从多张图像中获取背景? [英] How to get the background from multiple images by removing moving objects?
问题描述
我用固定的相机拍摄了同一场景的多张图像,其中有移动的物体.我不明白如何通过删除所有移动的对象在Python中使用这些图像来检索背景图像.
任何帮助将不胜感激.谢谢!
图像附在下面:
在这种情况下,我希望最终的图像没有任何帮助.
image1:
image2:
image3:
更新后的答案
我想出了如何在Python中做下面的建议-但也许有更好的方法-我仍然是Python初学者!
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Load images
im0 = np.array(Image.open('1.jpg'))
im1 = np.array(Image.open('2.jpg'))
im2 = np.array(Image.open('3.jpg'))
# Stack the 3 images into a 4d sequence
sequence = np.stack((im0, im1, im2), axis=3)
# Repace each pixel by mean of the sequence
result = np.median(sequence, axis=3).astype(np.uint8)
# Save to disk
Image.fromarray(result).save('result.png')
原始答案
最简单的方法是获取3幅图像中每个像素的中值,因为没有手的2张图像的值彼此接近,用手的图像将是离群值,并且中值滤镜会去除离群值.
因此,我并不是说您要查看每个图像的3x3或5x5区域并计算中位数.相反,我的意思是看图1、2和3中的pixel[0,0]
并获取这三个值的中位数-通过将这三个值按顺序排序并选择三个已排序像素的中间值作为输出值来实现.然后在所有3张图像中查看pixel[0,1]
并重复该过程.
这是结果:
我不是写Python的,我只是在Terminal中使用 ImageMagick 做了完全一样的事情,
convert 1.jpg 2.jpg 3.jpg -evaluate-sequence median result.jpg
因此,如果您计算每个位置的3个像素的平均值/平均值而不是中位数,您就可以看到我在做什么,而手将只显示其原始密度的1/3:>
I have taken multiple images of the same scene with a fixed camera which has moving objects in it. I don't understand how can I use these images in Python to retrieve the background image by removing all the moving objects.
Any help would be appreciated. Thanks!
Images have been attached below:
In this case, I would expect the final image to be without any hands in it.
image1:
image2:
image3:
Updated Answer
I worked out how to do what I suggested below in Python - but there may be better ways - I am still a Python beginner!
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Load images
im0 = np.array(Image.open('1.jpg'))
im1 = np.array(Image.open('2.jpg'))
im2 = np.array(Image.open('3.jpg'))
# Stack the 3 images into a 4d sequence
sequence = np.stack((im0, im1, im2), axis=3)
# Repace each pixel by mean of the sequence
result = np.median(sequence, axis=3).astype(np.uint8)
# Save to disk
Image.fromarray(result).save('result.png')
Original Answer
The easiest way is to take the median of each pixel across the 3 images because the 2 images without the hand will have values near each other and the one with the hand will be the outlier and the median filter removes outliers.
So, I don't mean you look at a 3x3 or 5x5 area of each image and calculate the median. Rather, I mean look at pixel[0,0]
in image 1, 2 and 3 and take the median of those three values - you do that by sorting the 3 values into order and picking the middle value of the three sorted pixels as your output value. Then look at pixel[0,1]
in all 3 images and repeat the process.
This is the result:
I didn't write the Python, I just did exactly the same thing with ImageMagick in Terminal like this:
convert 1.jpg 2.jpg 3.jpg -evaluate-sequence median result.jpg
Just so you can see what I am doing, if I calculate the mean/average of the 3 pixels at each location, rather than the median, the hands will show up at just 1/3 of their original density:
这篇关于如何通过移除移动物体从多张图像中获取背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!