Python 2.7 Value Error 数学域错误

Python 2.7 Value Error math domain error

UPDATE

@peter wood 评论实际上是答案,但它是评论,所以我只是赞成它。

接受的答案表明矩阵中存在负数。 我所做的是将两个图像都转换为灰度并进行比较,不再有警告和错误。干杯,谢谢大家。


我开始学习 python 和 simplecv ,使用 simple 中比较两个图像的示例程序之一,我遇到了这个错误。它首先抛出

runtime warning: overflow encountered in int_scalars

map(lambda a,b: (a-b)**2,h1,h2))/len(h1))

然后抛出错误

Traceback (most recent call last):
  File "G:\fs\python27files\imagecompare2.py", line 29, in <module>
    compare('belt.jpg','belt4.jpg')
  File "G:\fs\python27files\imagecompare2.py", line 20, in compare
    map(lambda a,b: (a-b)**2,h1,h2))/len(h1))
ValueError: math domain error

密码是

import math, operator
from SimpleCV import *

def compare(f1,f2):


        img = Image(f1)
        img1 = Image(f2)

        h1 = img.hueHistogram()
        h2 = img1.hueHistogram()
        #print h1
        #print h2
        print len(h1)
        print len(h2)
        f = map(lambda a,b: (a-b)**2,h1,h2)
        print f

        rms = math.sqrt(reduce(operator.add,
                            map(lambda a,b: (a-b)**2,h1,h2))/len(h1))
        #print rms


        #print h1.huePeaks()
        #print h2.huePeaks()


if __name__=='__main__':
   compare('belt.jpg','belt4.jpg')

如您所见,我尝试在变量 f 中单独打印地图,但遇到了同样的警告。

最后我在 SO 中看到了一个相关问题,它告诉我如何设置

dType(datatype) 会解决这个问题,但那是关于 numpy 的,我读过

那个 simplecv 也合并了 numpy。有什么帮助吗?

EDIT

我已经粘贴了显示结果直方图的映射函数输出的图像,我在其中看到了一个负值。

突出显示的部分显示负数。 那么如何克服呢?

您正在计算负数的平方根。您需要在中间步骤中检查您的数据和值。

最小示例:

Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.sqrt(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error
>>>

尝试在别处加载图片。如果这没有失败。尝试使用图像 class examples or examples 的标准 simpleCV 函数测试它们。如果这也适用于您的图片,则图片不是原因。

这个异常说明map(lambda a,b: (a-b)**2,h1,h2))/len(h1))发生了溢出。那可能是你不是数字。 h1 或 h2 不是线性序列或长度相同。 map in python or map in python.

检查a-b是否不能平方。例如列表中的列表等..