Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory error performing a binary_erosion #954

Closed
plooney opened this issue Mar 22, 2014 · 10 comments
Closed

Memory error performing a binary_erosion #954

plooney opened this issue Mar 22, 2014 · 10 comments

Comments

@plooney
Copy link

plooney commented Mar 22, 2014

Using the following image

http://ubuntuone.com/0unwk6BVwTn5q9zGJR54vq

It's a pickled numpy array. When I use values on the order of a few hundred I get a memory error

@jni
Copy link
Member

jni commented Mar 23, 2014

@plooney please include a minimal code snipped to reproduce the error, and the exact error printout you are getting.

@JDWarner
Copy link
Contributor

I was going to play with this but it appears the file is Pickle protocol 3 thus Python 3.x only, whereas my environments are standardized to Python 2.x.

For a platform- and Python version-agnostic way to share NumPy arrays in the future, consider the *.npy format available through the functions np.save and np.load.

@plooney
Copy link
Author

plooney commented Mar 23, 2014

I have output the binary image as @JDWarner suggests. New file here

http://ubuntuone.com/5rToLHpQdlXzHdfOOhD1OK

Minimal code is

im = np.load(file_above)
binary_erosion(im,square(300))

Error is

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/scipy/ndimage/morphology.py", line 404, in binary_erosion
output, border_value, origin, 0, brute_force)
  File "/usr/lib/python3/dist-packages/scipy/ndimage/morphology.py", line 260, in _binary_erosion
border_value, origin, invert, cit, 0)
MemoryError

@plooney
Copy link
Author

plooney commented Mar 23, 2014

It looks like the error is in scipy.

Using

scipy.ndimage.morphology.binary_erosion

binary_erosion(im,square(300))

I get the same error.

Should the following be equal

binary_erosion(im,square(50),iterations = 6)

and

binary_erosion(im,square(300))

?

The second crashes but the first is ok.

@jni
Copy link
Member

jni commented Nov 4, 2014

Hey @plooney,

Sorry that we have taken so long to get back to you. It looks like your issue got lost in the stack. It also looks like UbuntuOne got shut down, so if you want me to have another look at this you'll have to share your file somewhere else!

As to your questions, you (usually) want an odd side length for your structuring elements, so that they are symmetric about a central pixel. So, binary_erosion(im, square(51), iterations=6) is indeed equal to binary_erosion(im, square(301)). But binary_erosion(im, square(50), iterations=6) will be approximately equal to binary_erosion(im, square(295)), but not exactly because the first call will result in six asymmetrical operations.

How big is your image? I'm not sure why it should result in a memory error...

@jni
Copy link
Member

jni commented Jan 22, 2015

@plooney Could you post your test image somewhere?

@soupault
Copy link
Member

Ok. I guess this is the case...
I've taken random large image from the web (http://www.hawaiisummersalsafest.com/wp-content/uploads/2015/11/free-b.jpg (3750x5000x3)), then:

import numpy as np
import skimage
from skimage import io, color, morphology

img = io.imread('free-b.jpg')

img_gray = color.rgb2gray(img)
img_bin = img_gray.copy()
img_bin[img_bin < 0.5] = 0
img_bin[img_bin >= 0.5] = 1

tmp = morphology.binary_erosion(img_bin, np.ones((300, 300), dtype=np.bool))

---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
<ipython-input-20-55b1b7efe510> in <module>()
----> 1 tmp = morphology.binary_erosion(img_bin, np.ones((300, 300), dtype=np.bool))

/usr/lib/python3.5/site-packages/skimage/morphology/misc.py in func_out(image, selem, *args, **kwargs)
35         if selem is None:
36             selem = _default_selem(image.ndim)
---> 37         return func(image, selem=selem, *args, **kwargs)
38 
39     return func_out

/usr/lib/python3.5/site-packages/skimage/morphology/binary.py in binary_erosion(image, selem, out)
40     if out is None:
41         out = np.empty(image.shape, dtype=np.bool)
---> 42     nd.binary_erosion(image, structure=selem, output=out)
43     return out
44 

/usr/lib/python3.5/site-packages/scipy/ndimage/morphology.py in binary_erosion(input, structure, iterations, mask, output, border_value, origin, brute_force)
373     """
    374     return _binary_erosion(input, structure, iterations, mask,
--> 375                            output, border_value, origin, 0, brute_force)
    376 
    377 

/usr/lib/python3.5/site-packages/scipy/ndimage/morphology.py in _binary_erosion(input, structure, iterations, mask, output, border_value, origin, invert, brute_force)
    244     if iterations == 1:
    245         _nd_image.binary_erosion(input, structure, mask, output,
--> 246                                      border_value, origin, invert, cit, 0)
    247         return return_value
    248     elif cit and not brute_force:

MemoryError: 

I suppose, the erosion kernel is too large.

I'd suggest to close the issue and discuss this behaviour with numpy/scipy devs.

@soupault soupault added the bug label Jan 30, 2016
@soupault
Copy link
Member

soupault commented Apr 6, 2016

@ev-br Hi! Do you think if it is worth reporting to scipy?

@ev-br
Copy link
Contributor

ev-br commented Apr 7, 2016

Reporting it on the scipy tracker does no harm, I suppose. The blocker would be that someone actually looks into the ndimage sources and comes up with a fix. Is that you maybe? :-)

@soupault
Copy link
Member

soupault commented Apr 7, 2016

Closing as not a bug of skimage.
For further discussion and proposals, please, use scipy/scipy#6042 and scipy/scipy#4878 .

@soupault soupault closed this as completed Apr 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants