scikit-image / scikit-image

Image processing in Python
https://scikit-image.org
Other
6.1k stars 2.24k forks source link

Slic Superpixels on 3D Images #1055

Closed nmoya closed 10 years ago

nmoya commented 10 years ago

Hello,

Description

I ran into an issue while executing segmentation.slic method on a volumetric image. The supervoxels are returing as a perfect grid of cubes. Here is what I did: I read an integer array from a file, shifted them between [0, 1], reshaped to (z size, y size, x size) and called the slic method.

Parameters

All of those attempts yielded to the same result. The current result can be seen here: slic

Here is my label image at Z slice 50: label50

Here is my code segment: image

I ran with similar parameters in a 2D image and the issue did not occur.

Am I missing something? Any help would be appreciated. Thanks in advance!

jni commented 10 years ago

Hey @nmoya,

Are you at scipy? If so, come talk to me e.g. after this afternoon's tutorial!

Either way, it's strange problem. My only guess right now is that you might be reshaping your array incorrectly, which is a common problem when converting to and from certain formats. In that case, you would lose all spatial correlations in the color and SLIC would only be able to use spatial information to cluster. Without the test image, I can't tell whether this is the problem.

nmoya commented 10 years ago

Hey @jni,

Unfortunately I am not at Scipy :( To make sure that the conversion from my format to the numpy array was successful I saved the slice at position z size / 2.

Slice 58 saved from the numpy array. slicemiddle

This looks very similar to the output that I get on my visualization program. So I am assuming that it is correctly reshaped.

Sadly, it's a medical image and I need to ask permission to another person to post the full image here, that's why I only posted one slice.

jni commented 10 years ago

Ah, too bad about scipy. =)

Can you reproduce the same behaviour on a shareable image?

nmoya commented 10 years ago

Yes, sure.

I just sent you an email (the one in your github profile) with a link to the sample image.

nmoya commented 10 years ago

Hi @jni,

Did you get a chance to look at the image I sent you? I wrote this piece of code that loads a .npy file, execute slic an save a boundary image as output. I am still getting the same cubic grid :-(

#!/usr/bin/python2.7
import sys
import numpy
from skimage import segmentation
from skimage import io

def main():
    if len(sys.argv) != 5:
        print('usage: slic3d [INPUT] [OUTPUT] [NREGIONS] [COMPACTNESS]\n' + 
              '\tINPUT: input file \n' +
              '\tOUTPUT: output file \n' + 
              '\tNREGIONS: number of regions\n' + 
              '\tCOMPACTNESS : region compactness (default: 20)')
        sys.exit()

    input = sys.argv[1]
    output = sys.argv[2]

    nregions = int(sys.argv[3])
    compactness = int(sys.argv[4])

    image = numpy.load(input)
    zsize, ysize, xsize = image.shape
    io.imsave("middle_z.pgm", image[zsize/2])

    label = segmentation.slic(image, n_segments = nregions, compactness = compactness, multichannel = False) + 1

    #for i in range(zsize):  # For all Z slices
    boundaries = segmentation.mark_boundaries(image[zsize/2], label[zsize/2], color=(1,1,0))
    io.imsave("boundaries.ppm", boundaries)

    label = numpy.array(label, dtype=numpy.int32)
    label.reshape(-1).tofile(output)

if __name__ == "__main__":
    main()
jni commented 10 years ago

Hi @nmoya,

Sorry for the delay responding: SciPy was busy!

Anyway, I played with your example image, and it was just a matter of trying much lower compactness! 0.04 game me decent-looking results. I don't understand why this is the case but there it is, give it a go!

I'm closing this; reopen it if it doesn't fix your problem!

nmoya commented 10 years ago

Hi!

Yes! This solved my problem. Thank you very much!