seung-lab / connected-components-3d

Connected components on discrete and continuous multilabel 3D & 2D images. Handles 26, 18, and 6 connected variants; periodic boundaries (4, 8, & 6)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

connected_components does not labels single pixels in 2D

blchatel opened this issue · comments

Dear all,

Using cc3d for a while in python I just noticed single pixels are not labeled. At the beginning I though cc3d did not catch isolated pixels but the behavior is a little bit stranger :

    import cc3d
    import numpy as np

    # NOT WORKING
    binary_img = np.zeros((3, 3), dtype=np.uint8)
    binary_img[1, 1] = 1
    labels = cc3d.connected_components(binary_img, connectivity=4)
    print(f'{binary_img}\n => \n', labels)
    
    binary_img = np.zeros((5, 5), dtype=np.uint8)
    binary_img[1, 1] = 1
    labels = cc3d.connected_components(binary_img, connectivity=4)
    print(f'{binary_img}\n => \n', labels)
    
    # WORKING
    binary_img = np.zeros((5, 5), dtype=np.uint8)
    binary_img[1, 1] = 1
    binary_img[3, 3] = 1
    labels = cc3d.connected_components(binary_img, connectivity=4)
    print(f'{binary_img}\n => \n', labels)

Corresponding outputs

    [[0 0 0]
     [0 1 0]
     [0 0 0]]
     => 
     [[0 0 0]
     [0 0 0]
     [0 0 0]]
    
    [[0 0 0 0 0]
     [0 1 0 0 0]
     [0 0 0 0 0]
     [0 0 0 0 0]
     [0 0 0 0 0]]
     => 
     [[0 0 0 0 0]
     [0 0 0 0 0]
     [0 0 0 0 0]
     [0 0 0 0 0]
     [0 0 0 0 0]]
    
    [[0 0 0 0 0]
     [0 1 0 0 0]
     [0 0 0 0 0]
     [0 0 0 1 0]
     [0 0 0 0 0]]
     => 
     [[0 0 0 0 0]
     [0 1 0 0 0]
     [0 0 0 0 0]
     [0 0 0 2 0]
     [0 0 0 0 0]]

This issue may looks minor in most of the case but I use sometimes cc3d with very small 2D patches and this situation is happening.

Notice this issue seams not reproducible in 3D with third dimension bigger than 1

Best regards

I found the bug. C-order arrays are affected but not F order arrays. I'll work on releasing the fix, but in the meantime just do array = np.asfortranarray(array) and it should work.

Perfect ! Thanks à lot for the quick answers !

Released this as version 3.2.1!