gpt4 book ai didi

python - 总结所有图像的平均像素值 : TypeError: 'numpy.float64' object is not iterable

转载 作者:太空宇宙 更新时间:2023-11-03 21:02:44 27 4
gpt4 key购买 nike

我必须根据像素值设置阈值才能删除文件夹中的某些图像,并且我需要知道像素值的标准偏差。因此,我需要总结所有平均像素值。

以下是我尝试过的

以下代码演示了 np.mean() 的输出如何

import os,glob
from PIL import Image
from skimage import io
import numpy as np
from statistics import stdev

path = "/Users/Xin/Desktop/SVM-Image-Classification-master/test"
# Delete images with the low pixel value
for filename in os.listdir(path):
images = Image.open(os.path.join(path,filename))
print(np.mean(images))
#if np.mean(images) < 20:
#os.remove(os.path.join(path, filename))
#print(len(os.listdir(path)))

输出如下,值在0~255之间。值越小,图像越黑。

12.685516357421875
14.462142944335938
12.24658203125
9.507644653320312
18.701019287109375
10.004150390625
18.128433227539062
12.625930786132812

以下代码是我尝试过的

path = "/Users/Xin/Desktop/SVM-Image-Classification-master/test"
# Delete images with the low pixel value
for filename in os.listdir(path):
images = Image.open(os.path.join(path,filename))
L = list[round(np.mean(images),2)]
totalvalue = sum(L)
print(totalvalue)
#if np.mean(images) < 20:
#os.remove(os.path.join(path, filename))
#print(len(os.listdir(path)))

错误提示如下

TypeError: list indices must be integers or slices, not numpy.float64

有人可以帮我吗?非常感谢!

最佳答案

此函数可以代替标准差。

def function(image):
(R, G, B)= function_for_rgb(image)
std_dev = statistics.stdev([R, G, B]) #standard deviation
return round(std_dev,2)

关于python - 总结所有图像的平均像素值 : TypeError: 'numpy.float64' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55620493/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com