pythonpython-2.7dictionarysimplecv

Python 2.7 Value Error math domain error


UPDATE

@peter wood comment is actually answer but it is comment so i just upvoted it.

The accepted answer reveals the presence of negative number in the matrix. What I have done is converted both image to grayscale and compared, no more warnings and error. Cheers and Thanks guys.


I am started to learn python and simplecv , using one of the example program in simple which is comparing two images, i am encountering this error. It first throws

runtime warning: overflow encountered in int_scalars

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

Then throws the error

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

The code is

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')

as you can see i tried to print map alone in variable f, and encounters the same warning.

Finally i saw one related question here in SO which tells that setting up

dType(datatype) will remove the issue , but that is pertaining to numpy, I read

that simplecv is also incorporating numpy. any help regarding this?

EDIT

i have pasted image showing the output of map function of resulting histograms, and i see one negative value in it.

enter image description here

the highlighted portion is showing the negative number. so how to overcome this?


Solution

  • You are computing square root of negative number. You need to check your data and values in intermediate steps.

    Minimal example:

    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
    >>>