Python 3:使用Reduce(lambda x,y:x + y,eachPix [:3])/ len(eachPix [:3])vs mean(eachPix [:3])得到不同的数字

时间:2018-03-18 05:57:04

标签: python-3.x

我已从互联网上下载图像以学习图像识别。

我正在尝试计算每一行的图像像素的平均值,但我无法理解计算,因为它与减少(lambda)与平均值不同。任何人都可以向我解释输出如何给出两个不同的数字?例如,第一行的输出:使用lambda和reduce我得到80.3,而使用mean,我得到165

我已经粘贴了整个代码并输出了它。 代码:

import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import PIL
from PIL import Image
from functools import reduce

i=Image.open("F:/images/numbers/y0.5.png")
iar=np.array(i)

print(iar)

from statistics import mean
for eachRow in iar:
        for eachPix in eachRow:
            avgNum=reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3])
            avgNum1 = mean(eachPix[:3])

            print(eachPix),  print(avgNum),print(avgNum1)

输出:

  [[[255 242   0 255]
  [255 242   0 255]
  [239 228 176 255]
  [239 228 176 255]
  [239 228 176 255]
  [239 228 176 255]
  [255 242   0 255]
  [255 242   0 255]]

 [[255 242   0 255]
  [239 228 176 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [239 228 176 255]
  [255 242   0 255]]

 [[239 228 176 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [239 228 176 255]]

 [[239 228 176 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [239 228 176 255]]

 [[239 228 176 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [239 228 176 255]]

 [[239 228 176 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [239 228 176 255]]

 [[255 242   0 255]
  [239 228 176 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [255 242   0 255]
  [239 228 176 255]
  [255 242   0 255]]

 [[255 242   0 255]
  [255 242   0 255]
  [239 228 176 255]
  [239 228 176 255]
  [239 228 176 255]
  [239 228 176 255]
  [255 242   0 255]
  [255 242   0 255]]]
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[239 228 176 255]
43.6666666667
214
[239 228 176 255]
43.6666666667
214
[239 228 176 255]
43.6666666667
214
[239 228 176 255]
43.6666666667
214
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[239 228 176 255]
43.6666666667
214
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[239 228 176 255]
43.6666666667
214
[255 242   0 255]
80.3333333333
165
[239 228 176 255]
43.6666666667
214
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[239 228 176 255]
43.6666666667
214
[239 228 176 255]
43.6666666667
214
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[239 228 176 255]
43.6666666667
214
[239 228 176 255]
43.6666666667
214
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[239 228 176 255]
43.6666666667
214
[239 228 176 255]
43.6666666667
214
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[239 228 176 255]
43.6666666667
214
[255 242   0 255]
80.3333333333
165
[239 228 176 255]
43.6666666667
214
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[239 228 176 255]
43.6666666667
214
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165
[239 228 176 255]
43.6666666667
214
[239 228 176 255]
43.6666666667
214
[239 228 176 255]
43.6666666667
214
[239 228 176 255]
43.6666666667
214
[255 242   0 255]
80.3333333333
165
[255 242   0 255]
80.3333333333
165

1 个答案:

答案 0 :(得分:0)

您的问题是由此行引起的:

iar = np.array(i)

Numpy将您的图片值导入0到255之间作为uint8数据,我们可以从print(i.dtype)reduce看到,在此数据类型中执行添加。如果在此数据类型中添加255和254,则会出现溢出问题。 将此行更改为

iar = np.array(i, dtype = int)

,您的问题就会消失,因为现在numpyreduce使用int64号码进行操作,即-9223372036854775808和9223372036854775807之间的数字。Read more about numpy data types here.