Skip to content

New canny implementation silently fails with integer images. #6871

Closed

Description

@erykoff

Description:

The new skimage.feature.canny implementation silently fails if given an integer image. This worked on scikit-image<=0.19, and no longer works with scikit-image=0.20. The documentation says that any dtype should work:

    image : 2D array
        Grayscale input image to detect edges on; can be of any dtype.

Way to reproduce:

from skimage.feature import canny
import numpy as np

im = np.zeros((100, 100))
im[0: 50, 0: 50] = 1.0
print("Edge pixels with float input: ", canny(im, low_threshold=0, high_threshold=1).sum())
print("Edge pixels with int input: ", canny(im.astype(np.int64), low_threshold=0, high_threshold=1).sum())

This prints on new skimage (0.20):

Edge pixels with float input:  182
Edge pixels with int input:  0

And on old skimage (0.19):

Edge pixels with float input:  144
Edge pixels with int input:  144

As I write this test case I also need to ask ... why did the number of pixels change?

Version information:

3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:08:06) [GCC 11.3.0]
Linux-3.10.0-1160.88.1.el7.x86_64-x86_64-with-glibc2.17
scikit-image version: 0.20.0
numpy version: 1.23.5

Activity

  1. stefanv commented on Apr 5, 2023

    @stefanv
    Member

    Yikes, thanks for catching this!

    On latest skimage I see:

    Edge pixels with float input:  144
    Edge pixels with int input:  0
    

    So, at least the float version seems OK.

  2. stefanv commented on Apr 5, 2023

    @stefanv
    Member

    This seems to be the cause:

            low_threshold /= dtype_max
    

    The correct comparison is probably:

    from skimage.feature import canny
    import numpy as np
    
    im = np.zeros((100, 100))
    im[0: 50, 0: 50] = 1.0
    print("Edge pixels with float input: ", canny(im, low_threshold=0, high_threshold=1).sum())
    print("Edge pixels with int input: ", canny((im * 255).astype(np.uint8), low_threshold=0, high_threshold=1).sum())
    

    I think we should clarify what low_threshold and high_threshold are in the docstring too.

  3. stefanv commented on Apr 5, 2023

    @stefanv
    Member

    Note that with (im * 255).astype(np.uint8) as input, we already achieve the same result in v0.20 as with v0.19.

    @rfezzani Could you confirm whether low_threshold /= dtype_max is wanted and necessary? We'll probably have to take this out for skimage v2 anyway, since we cannot rely on the dtype being meaningful.

  4. rfezzani commented on Apr 6, 2023

    @rfezzani
    Member

    It seems that the division by dtype_max was introduced in this commit after merging #2983. It was proposed as a fix for #2981.
    This issue reminds me the #3009 and #4442 issues. It at least needs fix/clarification/warning in the documentation as pointed out by @erykoff .

  5. rfezzani commented on Apr 6, 2023

    @rfezzani
    Member

    I don't think the division by dtype_max is causing the bug... The following script gives

    import skimage
    from skimage.feature import canny
    import numpy as np
    
    im = np.zeros((100, 100))
    im[:50, :50] = 1
    
    print(f"{skimage.__version__=}")
    
    for dtype in ["float64", "float32",
                  "uint8", "uint16", "uint32", "uint64",
                  "int8", "int16", "int32", "int64"]:
        print(f"Edge pixels count with {dtype} input: ",
              np.count_nonzero(canny(im.astype(dtype),
                                     low_threshold=0, high_threshold=1)))

    Gives

    v0.19

    skimage.__version__='0.19.0'
    Edge pixels count with float64 input:  230
    Edge pixels count with float32 input:  274
    Edge pixels count with uint8 input:  190
    Edge pixels count with uint16 input:  230
    Edge pixels count with uint32 input:  230
    Edge pixels count with uint64 input:  230
    Edge pixels count with int8 input:  270
    Edge pixels count with int16 input:  230
    Edge pixels count with int32 input:  270
    Edge pixels count with int64 input:  230
    

    v0.20

    Edge pixels count with float64 input:  144
    Edge pixels count with float32 input:  183
    Edge pixels count with uint8 input:  98
    Edge pixels count with uint16 input:  102
    Edge pixels count with uint32 input:  142
    Edge pixels count with uint64 input:  0
    Edge pixels count with int8 input:  182
    Edge pixels count with int16 input:  103
    Edge pixels count with int32 input:  105
    Edge pixels count with int64 input:  0
    

    I don't understand why I see so many different values comparing with @stefanv and @erykoff results :/

  6. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    My results correspond with yours, @rfezzani.

  7. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    The reason I suspected the dtype handling of being at fault, is because the magnitude of the edges are going to be very small when you input a value of 1 in an int64 image. Either way, it's clear that something's changed between the two versions.

  8. erykoff commented on Apr 6, 2023

    @erykoff
    ContributorAuthor

    I'm actually wondering how we were using int64 ever worked given the scaling. And it's certainly surprising (to me!) that the floats get scaled relative to 1.0 and the ints get scaled to int_max.

    But the change in numbers of pixels between 0.19/0.20 and float32/float64 is even more concerning.

  9. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    The problem originated in 376d30f, #6348

  10. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    OK, found the problem: it's related to compute_bleedover: before that was not done for an unmasked image, and now it's used.

  11. stefanv commented on Apr 6, 2023

    @stefanv
    Member
            smoothed_image /= bleed_over
    
  12. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    We apply a Gaussian filter to the mask to estimate bleed-over, but we set mode='constant', which means that instead of returning mostly 1, the bleed-over varies a lot on the edges.

  13. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    So, to get results compatible with previous versions, we should skip bleed_over calculation when no mask is given.

    Ideally, however, computing bleedover should not affect the result; but I don't know what bleedover is, so I can't comment on that.

    The closest I can get is to change the bleedover calculation not to use mode='constant':

            bleed_over = gaussian(mask.astype(float_type, copy=False), sigma=1) + np.finfo(float_type).eps
  14. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    This does not resolve the issues with int64, btw. Even removing the range normalization, that thresholding still fails.

  15. stefanv commented on Apr 6, 2023

    @stefanv
    Member
    diff --git a/skimage/feature/_canny.py b/skimage/feature/_canny.py
    index 89daa09b7..8647410e5 100644
    --- a/skimage/feature/_canny.py
    +++ b/skimage/feature/_canny.py
    @@ -57,7 +57,7 @@ def _preprocess(image, mask, sigma, mode, cval):
         """
         gaussian_kwargs = dict(sigma=sigma, mode=mode, cval=cval,
                                preserve_range=False)
    -    compute_bleedover = (mode == 'constant' or mask is not None)
    +    compute_bleedover = mask is not None
         float_type = _supported_float_type(image.dtype)
         if mask is None:
             if compute_bleedover:

    Yields:

    Edge pixels count with float64 input:  230
    Edge pixels count with float32 input:  270
    Edge pixels count with uint8 input:  190
    Edge pixels count with uint16 input:  230
    Edge pixels count with uint32 input:  230
    Edge pixels count with uint64 input:  0
    Edge pixels count with int8 input:  270
    Edge pixels count with int16 input:  230
    Edge pixels count with int32 input:  270
    
  16. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    Re: difference between float32 and float64, looks like the dtype influences the calculation of the smoothed (Gaussian filtered) image, which then later impacts gradients etc.

  17. lagru commented on Apr 6, 2023

    @lagru
    Member

    I can reproduce the results in #6871 (comment). Though, I think you omitted the last line for int64, which is still 0 and not 230 like in v0.19 (see #6871 (comment)):

    skimage.__version__='0.21.0rc0.dev0+git20230406.6e51f8c7a'
    Edge pixels count with float64 input:  230
    Edge pixels count with float32 input:  270
    Edge pixels count with uint8 input:  190
    Edge pixels count with uint16 input:  230
    Edge pixels count with uint32 input:  230
    Edge pixels count with uint64 input:  0
    Edge pixels count with int8 input:  270
    Edge pixels count with int16 input:  230
    Edge pixels count with int32 input:  270
    Edge pixels count with int64 input:  0
    
  18. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    OK, here we go: we call gaussian with preserve_range=False, so the uint64 image gets normalized before filtering 👀

  19. stefanv commented on Apr 6, 2023

    @stefanv
    Member
    Edge pixels count with float64 input:  230
    Edge pixels count with float32 input:  270
    Edge pixels count with uint8 input:  230
    Edge pixels count with uint16 input:  230
    Edge pixels count with uint32 input:  230
    Edge pixels count with uint64 input:  230
    Edge pixels count with int8 input:  230
    Edge pixels count with int16 input:  230
    Edge pixels count with int32 input:  230
    Edge pixels count with int64 input:  230
    
  20. erykoff commented on Apr 6, 2023

    @erykoff
    ContributorAuthor

    There's some sleuthing! These final numbers are with some patch done? And int64 is missing.

  21. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    Woops, fixed that.

    Yes, I'll make a PR with the patch soon for review.

  22. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    The question remains of which mode we should use to calculate bleed-over; perhaps @rfezzani can answer that one.

  23. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    The question remains of which mode we should use to calculate bleed-over; perhaps @rfezzani can answer that one.

    This problem is significant:

    skimage.__version__='0.21.0rc0.dev0+git20230406.5f41b297b'
    Edge pixels count with float64 input:  230
    Masked edge pixels count with float64 input:  144
    Edge pixels count with float32 input:  270
    Masked edge pixels count with float32 input:  183
    Edge pixels count with uint8 input:  230
    Masked edge pixels count with uint8 input:  144
    Edge pixels count with uint16 input:  230
    Masked edge pixels count with uint16 input:  144
    Edge pixels count with uint32 input:  230
    Masked edge pixels count with uint32 input:  144
    Edge pixels count with uint64 input:  230
    Masked edge pixels count with uint64 input:  144
    Edge pixels count with int8 input:  230
    Masked edge pixels count with int8 input:  144
    Edge pixels count with int16 input:  230
    Masked edge pixels count with int16 input:  144
    Edge pixels count with int32 input:  230
    Masked edge pixels count with int32 input:  144
    Edge pixels count with int64 input:  230
    Masked edge pixels count with int64 input:  144
    
  24. stefanv commented on Apr 6, 2023

    @stefanv
    Member

    OK, fixed by setting mode='nearest' for bleed-over calculation. May not be ideal in all cases, but aligns the masked and non-masked implementations for a full image mask.

  25. rfezzani commented on Apr 7, 2023

    @rfezzani
    Member

    The question remains of which mode we should use to calculate bleed-over; perhaps @rfezzani can answer that one.

    I think that bleed-over has nothing to do with boundary mode...

  26. rfezzani commented on Apr 7, 2023

    @rfezzani
    Member

    @erykoff, I am really sorry to see your lsst/pipe_tasks#766 (comment) ...
    Image processing with int64 data in is absolutely not standard (again, please see #3009 and #4442)...
    The bug you reported 2 days ago (many thanks for that) is analyzed, and a patch #6874 is already close to be available ... scikit-image is maybe not bug free (sorry for that), but its community is really responsive and helpful... I hope you will nevertheless enjoy working with scikit-image...

  27. stefanv commented on Apr 7, 2023

    @stefanv
    Member

    I've concluded investigating the issue @erykoff.

    (1) skimage does not support int64 images, so the fact that that worked before was coincidence. Please see https://scikit-image.org/docs/stable/user_guide/data_types.html

    (2) There has been a change in behavior since 0.19, namely that we now are better at not returning (erroneous) canny edges on the border of images and masks. I consider this to be a bug fix.

    (3) In #6875 I prohibit 64-bit integer images as input to canny, and add tests to ensure that masked and unmasked processing of images align.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions