【问题标题】:Sum up average pixel values of all images: TypeError: 'numpy.float64' object is not iterable总结所有图像的平均像素值:TypeError: 'numpy.float64' object is not iterable
【发布时间】:2019-04-11 06:16:37
【问题描述】:

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

以下是我尝试过的

以下代码演示了 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

谁能帮帮我? 非常感谢!

【问题讨论】:

  • 是 list() 不是 list[]
  • list[round(np.mean(images),2)] 告诉 python 给我列表listround(np.mean(images),2)th 元素。这没有意义,因为它是一个浮动

标签: python numpy scikit-image


【解决方案1】:

这个函数可以代替标准差。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 2019-04-12
    • 2018-02-01
    • 2016-02-18
    • 1970-01-01
    相关资源
    最近更新 更多