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)
GNU Lesser General Public License v3.0
356 stars 42 forks source link

connected_components does not labels single pixels in 2D #70

Closed blchatel closed 3 years ago

blchatel commented 3 years ago

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

william-silversmith commented 3 years ago

Thank you for the report! I will look into this. I suspect it is due to an optimization that was probably fixed in 3d but maybe I forgot to port it to 2D.

On Thu, Jul 15, 2021, 8:29 AM Bastien Chatelain @.***> wrote:

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

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/seung-lab/connected-components-3d/issues/70, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATGQSJSQINXDFMCBCKSGZTTX3IC7ANCNFSM5ANOKZRQ .

william-silversmith commented 3 years ago

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.

blchatel commented 3 years ago

Perfect ! Thanks à lot for the quick answers !

william-silversmith commented 3 years ago

Released this as version 3.2.1!