Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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 - Fatal编程技术网

Python 如何获取图像的标准偏差

Python 如何获取图像的标准偏差,python,image-processing,Python,Image Processing,我的目标是取200多张图像的平均值,然后找出平均值的标准偏差。要求用户输入阈值,然后将阈值与标准偏差进行比较。如果阈值小于该特定像素的标准偏差,则将其更改为红色[255,0,0]。我的问题在于获取像素的标准偏差。任何帮助都将不胜感激 allimg = [] avg_img=[] path = glob.glob('podatlamar/*.jpg') for x in path: img = Image.open(x) img = np.float32(img) all

我的目标是取200多张图像的平均值,然后找出平均值的标准偏差。要求用户输入阈值,然后将阈值与标准偏差进行比较。如果阈值小于该特定像素的标准偏差,则将其更改为红色[255,0,0]。我的问题在于获取像素的标准偏差。任何帮助都将不胜感激

allimg = []
avg_img=[]
path = glob.glob('podatlamar/*.jpg')

for x in path:
    img = Image.open(x)
    img = np.float32(img)
    allimg.append(img)
avg_img = np.average(allimg, axis=0)

std = [0,0,0]
for img in allimg :
    std += (img-avg_img) ** 2
    std = np.sqrt(std / (len(allimg) - 1))
现在:

等等

结果是

下面是正在运行的for循环调试

[平均值更改为

std看起来像什么的片段

[[0.19792126 0.05137325 0.03966657]
  [0.09997863 0.06348856 0.07472634]
  [0.0367469  0.18667144 0.21834356]
  ...
  [0.02421235 0.02454335 0.14083997]
  [0.02319027 0.02351524 0.13969136]
  [0.02285284 0.02317629 0.13930877]]

 [[0.03304812 0.06428259 0.04262938]
  [0.0978733  0.02841616 0.04049174]
  [0.09566899 0.02877731 0.0357872 ]
  ...
  [0.08500231 0.03502595 0.12032651]
  [0.08347222 0.03630779 0.1217759 ]
  [0.08385488 0.03598539 0.12141356]]

由于您未提供任何输入数据,我将此动画的各个帧用作18个输入帧,以平均:

我使用ImageMagick提取了它们:

magick animation.gif -coalesce frame-%02d.jpg
我提出的代码如下所示:

#!/usr/bin/env python3

import glob
import numpy as np
from PIL import Image

# Generate list of image names
names = glob.glob('frame-*.jpg')

# Load all images into list
images = []
for filename in names:
    im = Image.open(filename)
    images.append(np.array(im))

# Generate average image, where each pixel is the average of that pixel position across all images
average = np.mean(images, axis=0)
Image.fromarray(average.astype(np.uint8)).save('tmp-average.png')    # DEBUG

# Generate stdev image, where each pixel is the stdev of that pixel position across all images
stdev = np.std(images, axis=0)
Image.fromarray(stdev.astype(np.uint8)).save('tmp-stdev.png')        # DEBUG

threshold = 80
average[np.any(stdev>threshold, axis=2)] = [255,0,0]
Image.fromarray(average.astype(np.uint8)).save(f'result.png')
结果是:

只是为了好玩,我制作了一个不同设置阈值效果的动画:


我不清楚您想在这里做什么。您似乎是在每个像素位置取200个样本的平均值,因此有效地通过200个图像的z轴,但您是在平均图像的x、y维度上取标准偏差。对吗?无论如何,您确实不想在内存中保存200个图像马上,因为图像很大。您只需要一个float64或int64图像,将所有200个输入图像相加,最后除以200。您的最终计算应该使用Numpy进行向量化,否则您将整天都在那里。尝试避免使用Python图像处理的
for
循环。
。任何
都会让我担心让我想知道你是否有RGB图像而不是灰度图像?@MarkSetchell jpg图像是RGB图像而不是灰度图像。目前,通过将uint8字节转换为float64,你已经使每个图像都比需要的大8倍,并且你已经在一个列表中的内存中存储了200个这样的图像,因此你浪费了16000倍的内存。我们可以修复这个问题吗第一?
#!/usr/bin/env python3

import glob
import numpy as np
from PIL import Image

# Generate list of image names
names = glob.glob('frame-*.jpg')

# Load all images into list
images = []
for filename in names:
    im = Image.open(filename)
    images.append(np.array(im))

# Generate average image, where each pixel is the average of that pixel position across all images
average = np.mean(images, axis=0)
Image.fromarray(average.astype(np.uint8)).save('tmp-average.png')    # DEBUG

# Generate stdev image, where each pixel is the stdev of that pixel position across all images
stdev = np.std(images, axis=0)
Image.fromarray(stdev.astype(np.uint8)).save('tmp-stdev.png')        # DEBUG

threshold = 80
average[np.any(stdev>threshold, axis=2)] = [255,0,0]
Image.fromarray(average.astype(np.uint8)).save(f'result.png')