Search code examples
pythonscikit-imagesuperpixels

Expected 'Py_ssize_t' but got 'long'


I'm reimplementing SLIC for Image Segmentation just for fun. But I'm lazy and I do not want to write a function to make all cluster connected so I decide to use _enforce_label_connectivity_cython() from skimage but I get an error of Buffer dtype mismatch and I don't now how to solve it

enter image description here.


Solution

  • The data type of np.expand_dims(boundaries, axis=0), that is, the type of the elements inside the array, is not what the Cython function expects. It expects Py_ssize_t which is a type that depends on your platform, but it is getting an array of type long which means np.int64. To get the right type, I think you can do:

    labels = _enforce_label_connectivity_cython(
        np.expand_dims(boundaries, axis=0).astype(np.intp),
        min_size,
        max_size,
    )
    

    If that doesn't work, try .astype(np.int32). Again, the exact answer depends on your OS and Python version, but if I remember correctly, np.intp should be the correct type to match Py_ssize_t.