pyimgsaliency icon indicating copy to clipboard operation
pyimgsaliency copied to clipboard

Error for images that have completely black background

Open yilangpeng opened this issue 8 years ago • 4 comments

Both cases are about mdb.

(1) fruit3-fr

Traceback (most recent call last): File ".../demo.py", line 54, in mbd = psal.get_saliency_mbd(filename).astype('uint8') File ".../saliency_mbd.py", line 169, in get_saliency_mbd cov_bottom = np.linalg.inv(cov_bottom) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 526, in inv ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 90, in _raise_linalgerror_singular raise LinAlgError("Singular matrix") numpy.linalg.linalg.LinAlgError: Singular matrix

(2) Another example is a white background image: sushi6-st copy

raises warning /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/skimage/io/_io.py:132: UserWarning: sushi6-ST copy-mbd.png is a low contrast image warn('%s is a low contrast image' % fname)

The saliency map is also strange (completely black). sushi6-st copy-mbd

However, if I add some elements to its background, it works fine and results in a good saliency map. sushi6-st

sushi6-st-mbd

yilangpeng avatar Jul 06 '17 22:07 yilangpeng

please help me ImportError: No module named pyimgsaliency

RoseLii avatar Nov 08 '17 03:11 RoseLii

I have same errors and it is very critical, your module would be very useful but we cannot use your work until this is fixed.

################ versions ##########################

python 3.5.2 numpy 1.13.3 scipy 1.0.0 sklearn 0.19.1

################ codes ##########################

image_path = "chem.png" # also tried with jpg version : image_path = "chem.jpg"

mbd = psal.get_saliency_mbd(image_path).astype('uint8') pi_mbd = ProcessedImage(mbd) pi_mbd.image.show()

######################## image #################

chem

########## error ########################

--> 513 ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj) 514 return wrap(ainv.astype(result_t, copy=False)) 515 ~/tfenv/lib/python3.5/site-packages/numpy/linalg/linalg.py in _raise_linalgerror_singular(err, flag) 88 89 def _raise_linalgerror_singular(err, flag): ---> 90 raise LinAlgError("Singular matrix") 91 92 def _raise_linalgerror_nonposdef(err, flag):

LinAlgError: Singular matrix

tymdgit avatar Feb 27 '18 09:02 tymdgit

A simple work-around that worked for me is to use the pseudo inverse in these cases. I.e., for each one:

        try:
            cov_left = np.linalg.inv(cov_left)
        except:
            cov_left = np.linalg.pinv(cov_left)

brian36 avatar Jun 15 '18 04:06 brian36

Is normal to have errors in flat color image with saliency methods due to the need to calculate the inverse matrix of image pieces, is something discussed even in literature, a solid fix is to add some noise to the image :

//untested
noise_mat = np.random.randint(4, size=img.size) - 2
img = np.clip(img + noise_mat , 0, 255) // avoid to have colors outside the valid range

this should do the work.

angelinimattia avatar Sep 16 '20 11:09 angelinimattia