[英]How to take the standard deviation of an image
我的目標是取 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 i in range(0, len(std)): #i is row
for j in range(0, len(std[i])): #j is column
if(std[i][j] > float(threshold)).any :
avg_img[i][j] = [255.0, 0.0, 0.0]
avg_img = np.clip(avg_img, 0, 255)
等等...
結果是所有像素都變成紅色
這是正在運行的 for 循環調試
[avg_img 改為255,0,0
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')
結果是這樣的:
只是為了好玩,我對不同設置閾值的效果做了一點動畫:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.