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

'slic' crash with spacing #1693

Closed
Borda opened this issue Sep 3, 2015 · 15 comments · Fixed by #5974
Closed

'slic' crash with spacing #1693

Borda opened this issue Sep 3, 2015 · 15 comments · Fixed by #5974

Comments

@Borda
Copy link
Contributor

Borda commented Sep 3, 2015

sigma division does not seems correct since sigma is a number and spacing is array_like

to Reproduce:

import numpy as np
from skimage import segmentation, io

image = io.imread('drosophila.png')

image = image[:,:,1]
image = image / float(np.max(image))
print image.shape
seg = segmentation.slic(image, n_segments=500, compactness=0.2, multichannel=False, spacing=(1.,1.)) 

drosophila

@jni
Copy link
Member

jni commented Sep 3, 2015

@Borda, the error message is obscure, I'll grant, =), but the docstring does specify that spacing must be length 3!

We don't currently support spacing in 2D images because it's rare to have different voxel spacing in just 2D images. (I have never come across this.) Do you have a use-case for 2D spacing? If so, I can add support for that. If not, I'll add a ValueError that explains what went wrong better than the current message.

Thanks for the report!

@Borda
Copy link
Contributor Author

Borda commented Sep 3, 2015

Well, I need segment 3D image but I wanted to start with simpler case (2D) a and give a try to the parameters I will need for 3D such as spacing... btw, what is the explanation of these numbers, 1 mean that distance between neighboring positions is 1? Thanks

@jni
Copy link
Member

jni commented Sep 3, 2015

@Borda the spacing is meant to be relative. (5, 1, 1) means that the distance between adjacent voxels in the leading dimension (e.g. planes) is 5 times greater than the distance between voxels in the row / column dimensions.

@stefanv
Copy link
Member

stefanv commented Sep 6, 2015

@jni How about a sentence in the user guide summarizing your comments here? We can perhaps also show how to address @Borda's user case (perhaps it requires adding a single dimension to make a 2D slice 3D).

@JDWarner
Copy link
Contributor

JDWarner commented Sep 6, 2015

I'd argue that we should be as general as possible when we can be. 2D anisotropic is a strange use case, but I think it's entirely possible to relax the length-3 requirement in favor of an iterable of length image.ndim. In fact, I'd recommend any algorithm which accepts a spacing kwarg should behave in that manner.

@Borda
Copy link
Contributor Author

Borda commented Sep 7, 2015

I can provide even code for 3D with interactive visualization in iPython if you like:

import libtiff
from skimage import segmentation

im = libtiff.TiffFile('drosophila.tiff') .get_tiff_array() 
img = np.empty(im.shape)
for i in range(img.shape[0]) :
    img[i,:,:] = np.array(im[i])
img = img / float(np.max(img))

seg = segmentation.slic(img, n_segments=5000, compactness=0.2, multichannel=False, spacing=(5.,1.,1.))

def showImageSegment(z):
    plt.figure(figsize=(12,8))
    _= plt.subplot(1,2,1), plt.imshow(img[z], cmap=plt.cm.gray), plt.axis('off')
    _= plt.subplot(1,2,2), plt.imshow(seg[z]), plt.axis('off')
    display()

interact( showImageSegment, z=widgets.IntSliderWidget(min=0,max=im.shape[0]-1,step=1,value=im.shape[0]/2) )

@jni
Copy link
Member

jni commented Sep 16, 2015

@JDWarner I agree, but it's a bit finicky to deal with all the possible cases (multichannel = True/False, sigma being a scalar or an array-like). A quick glance at the code didn't suggest an immediate solution. PRs welcome, as always! ;)

Happy to keep this open as a reminder to do that?

@stefanv
Copy link
Member

stefanv commented Dec 11, 2015

/ping @jni @JDWarner

Can we close this with an addition to the docstring, or is something more involved needed?

@emmanuelle
Copy link
Member

A case for 2-D anisotropic images is the case of spatio-temporal diagrams (used for example in chaos and non-linear systems).

@Borda
Copy link
Contributor Author

Borda commented Feb 17, 2019

any update here? :)

@JDWarner
Copy link
Contributor

Stand by my earlier comment that we should generalize spacing, here and elsewhere, to match the spatial dimensionality of the image.

I won't be able to dive into the code this week as I am on 12 hour overnight call.

@hmaarrfk
Copy link
Member

i gave the code a quick scan, i think the real challenge is coming up with tests. PRs would help :D.

@jni
Copy link
Member

jni commented Feb 17, 2019

I agree with @JDWarner, just haven't had the bandwidth to deal with this.

@Borda 👋 😊 maybe you'd like to tackle this? imho the right way to do it would be to normalise both spacing and sigma to be the same size array early on in the function.

@Borda
Copy link
Contributor Author

Borda commented Aug 15, 2019

@jni not sure what you meant by "tackle this"? 😃

@rfezzani
Copy link
Member

  • spacing can be a 1-element array like, or a 3-element array like, it crashes otherwise.
  • sigma can only be a scalar, a 1-element array like or a 3-element array like.

We need to start a deprecation cycle if we want to Change this behavior as it will introduce breaking changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants