You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
New canny implementation silently fails with integer images. #6871
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
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.
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 .
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.
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.
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.
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.
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
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.
@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...
(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.
Activity
stefanv commentedon Apr 5, 2023
Yikes, thanks for catching this!
On latest skimage I see:
So, at least the float version seems OK.
stefanv commentedon Apr 5, 2023
This seems to be the cause:
The correct comparison is probably:
I think we should clarify what
low_thresholdandhigh_thresholdare in the docstring too.stefanv commentedon Apr 5, 2023
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_maxis wanted and necessary? We'll probably have to take this out for skimage v2 anyway, since we cannot rely on the dtype being meaningful.rfezzani commentedon Apr 6, 2023
It seems that the division by
dtype_maxwas 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 .
rfezzani commentedon Apr 6, 2023
I don't think the division by
dtype_maxis causing the bug... The following script givesGives
v0.19
v0.20
I don't understand why I see so many different values comparing with @stefanv and @erykoff results :/
stefanv commentedon Apr 6, 2023
My results correspond with yours, @rfezzani.
stefanv commentedon Apr 6, 2023
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
1in an int64 image. Either way, it's clear that something's changed between the two versions.erykoff commentedon Apr 6, 2023
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.
stefanv commentedon Apr 6, 2023
The problem originated in 376d30f, #6348
stefanv commentedon Apr 6, 2023
OK, found the problem: it's related to
compute_bleedover: before that was not done for an unmasked image, and now it's used.stefanv commentedon Apr 6, 2023
stefanv commentedon Apr 6, 2023
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.stefanv commentedon Apr 6, 2023
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':stefanv commentedon Apr 6, 2023
This does not resolve the issues with int64, btw. Even removing the range normalization, that thresholding still fails.
stefanv commentedon Apr 6, 2023
Yields:
stefanv commentedon Apr 6, 2023
Re: difference between
float32andfloat64, looks like the dtype influences the calculation of the smoothed (Gaussian filtered) image, which then later impacts gradients etc.lagru commentedon Apr 6, 2023
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)):
stefanv commentedon Apr 6, 2023
OK, here we go: we call
gaussianwithpreserve_range=False, so the uint64 image gets normalized before filtering 👀stefanv commentedon Apr 6, 2023
erykoff commentedon Apr 6, 2023
There's some sleuthing! These final numbers are with some patch done? And
int64is missing.stefanv commentedon Apr 6, 2023
Woops, fixed that.
Yes, I'll make a PR with the patch soon for review.
stefanv commentedon Apr 6, 2023
The question remains of which
modewe should use to calculate bleed-over; perhaps @rfezzani can answer that one.stefanv commentedon Apr 6, 2023
This problem is significant:
stefanv commentedon Apr 6, 2023
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.rfezzani commentedon Apr 7, 2023
I think that bleed-over has nothing to do with boundary mode...
rfezzani commentedon Apr 7, 2023
@erykoff, I am really sorry to see your lsst/pipe_tasks#766 (comment) ...
Image processing with
int64data 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-imageis maybe not bug free (sorry for that), but its community is really responsive and helpful... I hope you will nevertheless enjoy working withscikit-image...stefanv commentedon Apr 7, 2023
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.