我编写了一个代码,以取平均多个图像来检索背景,这基本上删除了图像中的移动对象。我一直在尝试在平均之前降低异常值,以便仅获得背景而不是获得褪色的对象。我尝试了一些技巧,最近的一个是:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
from scipy import stats
# Load images
im0 = np.array(Image.open('test1/1.jpg'))
im1 = np.array(Image.open('test1/2.jpg'))
im2 = np.array(Image.open('test1/3.jpg'))
# Stack the 3 images into a 4d sequence
sequence = np.stack((im0, im1, im2), axis=3)
mean = np.mean(sequence, axis=3)
sd = np.std(sequence, axis=3)
finalSequence = [x for x in sequence if (x > mean - 2 * sd)]
finalSequence = [x for x in finalSequence if (x < mean + 2 * sd)]
# Repace each pixel by mean of the sequence
result = np.mean(finalSequence, axis=3).astype(np.uint8)
# Save to disk
Image.fromarray(result).save('result.png')
这给了我一个错误:
final_list = [x for x in sequence if (x > mean - 2 * sd)]
ValueError: operands could not be broadcast together with shapes (474,3,3) (266,474,3)
任何帮助或解决方法,将不胜感激。谢谢!
答案 0 :(得分:0)
没有安装软件包很难调试。 您的操作无效,因为数组的形状its header不同。 您可以通过以下方式查看它们:
print(x.shape)
print(mean.shape)
print(sd.shape)
您还可以将均值/标准差乘以np.ones(sequence.shape) 然后比较整个数组,而不是遍历它们 (应该会更有效率)
mean_vec = mean * np.ones(sequence.shape)
sd_vec = sd * np.ones(sequence.shape)
sequence > (mean_vec - 2 * sd_vec)