Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 ubyte_标量中遇到运行时警告溢出_Python_Python 2.7_Numpy_Image Recognition - Fatal编程技术网

Python ubyte_标量中遇到运行时警告溢出

Python ubyte_标量中遇到运行时警告溢出,python,python-2.7,numpy,image-recognition,Python,Python 2.7,Numpy,Image Recognition,我得到的错误是: from PIL import Image import numpy as np import matplotlib.pyplot as plt def threshold(imageArray): balanceAr = [] newAr = imageArray for eachRow in imageArray: for eachPix in eachRow: avgNum = reduce(lambda

我得到的错误是:

from PIL import Image
import numpy as np 
import matplotlib.pyplot as plt

def threshold(imageArray):
    balanceAr = []
    newAr = imageArray
    for eachRow in imageArray:
        for eachPix in eachRow:
            avgNum = reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3])
            balanceAr.append(avgNum)
    balance = reduce(lambda x, y: x + y, balanceAr) / len(balanceAr)
    for eachRow in newAr:
        for eachPix in eachRow:
            if reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3]) > balance:
                eachPix[0] = 255
                eachPix[1] = 255
                eachPix[2] = 255
                eachPix[3] = 255
            else:
                eachPix[0] = 0
                eachPix[1] = 0
                eachPix[2] = 0
                eachPix[3] = 255
    return newAr




i = Image.open('images/numbers/0.1.png')
iar = np.asarray(i)
3iar = threshold(iar)

i2 = Image.open('images/numbers/y0.4.png')
iar2 = np.asarray(i2)
#iar2 = threshold(iar2)

i3 = Image.open('images/numbers/y0.5.png')
iar3 = np.asarray(i3)
#iar3 = threshold(iar3)

i4 = Image.open('images/sentdex.png') 
iar4 = np.asarray(i4)
#iar4 = threshold(iar4)

threshold(iar3)
fig = plt.figure()
ax1 = plt.subplot2grid((8,6), (0,0), rowspan = 4, colspan = 3)
ax2 = plt.subplot2grid((8,6), (4,0), rowspan = 4, colspan = 3)
ax3 = plt.subplot2grid((8,6), (0,3), rowspan = 4, colspan = 3)
ax4 = plt.subplot2grid((8,6), (4,3), rowspan = 4, colspan = 3)

ax1.imshow(iar)
ax2.imshow(iar2)
ax3.imshow(iar3)
ax4.imshow(iar4)

plt.show()
警告(来自警告模块):
文件“C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image\u Recognition\imagerec.py”,第11行
avgNum=reduce(λx,y:x+y,eachPix[:3])/len(eachPix[:3])
RuntimeWarning:ubyte_标量中遇到溢出
警告(来自警告模块):
文件“C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image\u Recognition\imagerec.py”,第16行
如果减少(λx,y:x+y,每个pIx[:3])/len(每个pIx[:3])>余额:
RuntimeWarning:ubyte_标量中遇到溢出
回溯(最近一次呼叫最后一次):
文件“C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image\u Recognition\imagerec.py”,第47行,在
阈值(iar3)
文件“C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image\u Recognition\imagerec.py”,第17行,在阈值中
eachPix[0]=255
ValueError:分配目标为只读
关于溢出运行时警告:
您不必担心这些,它们本质上告诉您的是,
uint_8
(无符号整数)
类型(通常用于图像文件)的范围已超出其可接受范围

从提供的链接来看,
uint_8
类型具有以下范围:

无符号整数(0到255)

numpy
只是发出警告,通知您溢出。谢天谢地,它会自动将结果调整为可接受范围的值

例如:

Warning (from warnings module):
  File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 11
    avgNum = reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3])
RuntimeWarning: overflow encountered in ubyte_scalars

Warning (from warnings module):
  File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 16
    if reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3]) > balance:
RuntimeWarning: overflow encountered in ubyte_scalars

Traceback (most recent call last):
  File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 47, in <module>
    threshold(iar3)
  File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 17, in threshold
    eachPix[0] = 255
ValueError: assignment destination is read-only

关于实际错误:
您的错误
值错误:将值分配给
numpy
数组
newAr
时,分配目标为只读
。 它是信息性的,它告诉你的是数组是只读的;内容为只读:您可以访问它们,但不能修改它们

所以像这样的行动:

from PIL import Image
import numpy as np

img = Image.open("/path/to/image.png")
img_array = np.asarray(img) # array values are of type uint_8 (!)

print img_array[0][0] # prints [ 12,  21,  56, 255] 

uint8_1 = img_array[0][0][3] # = 255
uint8_2 = img_array[0][0][2] # = 56

uint8_3 = uint8_1 + uint8_2 

# When executed raises a RuntimeWarning of overflow ubyte_scalars
# But! The result 'rolls over' to the acceptable range. So,
print uint8_3  # prints 55
将引发一个
ValueError

谢天谢地,通过为数组设置标志参数,可以轻松绕过此问题:

# using img_array from previous snippet.
img_array[0][0][0] = 200 

正在抑制溢出运行时警告:
最后一句话:你总是可以的,尽管这通常不是最好的主意。(控制台中的几个警告很烦人,但它们可以让您更清楚地了解情况)

为此,只需在导入numpy后添加以下内容:

# using img_array from the previous snippet
# make it writeable 
img_array.setflags(write=True)

# Values of img_array[0][0] are, as before: [ 12,  21,  56, 255]
# changing the values for your array is possible now!
img_array[0][0][0] = 200

print img_array[0][0] # prints [ 200,  21,  56, 255]
i1=Image.open('images/numbers/0.1.png')) iar1=np.数组(i1)


使用数组而不是数组方法尝试用y:int(x)+int(y)替换y:x+y

import numpy as np
np.seterr(over='ignore')