Search code examples
pythonmatlabscikit-image

Performance differences between bwlabeln on Matlab and skimage.measure.label on Python?


I'm currently in the process of converting a Matlab program that performs image analysis into Python. One of the specific parts of the program utilizes bwlabeln to label the various components in a 3D matrix with sequentially increasing labels. I was able to find a very similar function in the skimage.measure package called label which essentially does the exact same thing. However, I find that when using input matrices of the same size and data type between both programs, bwlabeln on Matlab runs significantly faster (almost 4 times faster) than label in Python.

I was wondering what the reason is for this and if there is any way to speed up the Python code or improve performance?

On both functions, I am using 26 - connected neighborhoods which both seem to do by default but I further specified as an argument just to be sure.


Solution

  • the scikit-image algorithm is able to label arrays of integers, ie with more values than 0 and 1. Therefore, it is a different algorithm than the Matlab algorithm which is restricted to only 0 and 1 (False and True). You can use the label algorithm from scipy.ndimage which is indeed 4x faster on the binary image defined below, so you should be able to match up Matlab's speed with this function.

    In [1]: from scipy import ndimage                                               
    
    In [2]: from skimage import measure                                             
    
    In [3]: from skimage import data                                                                                             
    
    In [5]: img = data.binary_blobs(length=256, blob_size_fraction=0.25, n_dim=3)   
    
    In [6]: %timeit labels = ndimage.label(img)                                     
    161 ms ± 2.08 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
    
    In [7]: %timeit labels = measure.label(img)                                     
    629 ms ± 6.07 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)