Repository navigation
Description
Description:
The morphological closing must be extensive, and the opening anti-extensive. This means that, applying the closing to an image, the result is either equal or larger than the input; for the opening it's either equal or smaller.
scikit-image's operators do not satisfy these properties because the kernel is not mirrored in between the dilation and erosion operations that compose the closing and opening.
Working on #6665, I ran into the following comment:
# Inside ndi.grey_dilation, the footprint is inverted,
# e.g. `footprint = footprint[::-1, ::-1]` for 2D [1]_, for reasons unknown
# to this author (@jni). To "patch" this behaviour, we invert our own
# footprint before passing it to `ndi.grey_dilation`.This worried me, because the reason for this mirroring is exactly so that the composition of erosion and dilation lead to a correct closing and opening. Looking at the code for the opening and closing, it indeed does not mirror the structuring element.
There are two different definitions for morphological operators:
- The dilation and erosion are implemented as in ndimage, where one of the two operators mirrors the structuring element. The closing and opening are defined as the composition of the two using the same structuring element.
- The dilation and erosion are implemented as in skimage, where neither operator mirrors the structuring element. The closing and opening are defined as the composition of the two, with one of them using the mirrored structuring element.
scikit-image, by undoing the mirroring in ndimage's dilation, puts itself in definition 2, but implements the closing and opening as in definition 1, leading to incorrect operators.
There are two possible solutions:
- remove the mirroring in the dilation, or
- add a mirroring in the opening and the closing.
Note that the binary version of these functions is correct. Picking solution 1 would make the gray-scale and binary versions of the dilation consistent (currently one mirrors the structuring element and the other doesn't).
Way to reproduce:
import skimage
import numpy as np
# A test image
img = np.random.rand(300, 300)
img = skimage.filters.gaussian(img, 5)
img = (img > 0.5).astype(np.uint8)
img *= 255
# A non-symmetric SE
se = np.array([[0,0,1],[0,1,1],[1,1,1]])
# Closing must be extensive, but it isn't
closing = skimage.morphology.closing(img, se)
assert(np.all(closing >= img))
# Opening must be anti-extensive, but it isn't
opening = skimage.morphology.opening(img, se)
assert(np.all(img >= opening))
# Again for the binary case
img = img > 0
# Closing must be extensive, it is
closing = skimage.morphology.binary_closing(img, se)
assert(np.all(closing >= img))
# Opening must be anti-extensive, it is
opening = skimage.morphology.binary_opening(img, se)
assert(np.all(img >= opening))Version information:
3.10.9 (main, Dec 15 2022, 10:44:50) [Clang 14.0.0 (clang-1400.0.29.202)]
macOS-12.6.2-arm64-arm-64bit
scikit-image version: 0.19.3
numpy version: 1.23.5
Activity
stefanv commentedon Jan 19, 2023
My gut feel is option (2): introduce mirroring in opening and closing. Also, document this everywhere: erosion, dilation, opening, and closing.
stefanv commentedon Jan 19, 2023
Also, thank you very much @crisluengo for this analysis; that is really helpful!
crisluengo commentedon Jan 19, 2023
@stefanv The simplest solution is to remove all traces of the mirroring in this file. This will also make the functions equivalent to the binary operators. I find it awkward that we get a different result with
dilation()than withbinary_dilation()for the same image and footprint.That said, I found out that to make these two functions truly equivalent, we also need to set
shift_xandshift_ytoTrueindilation(). I'm at a loss as to what these options were created for, I don't understand the need. The functions would be simpler without them, and we'd be able to get true parity between the binary and gray-value versions of these operators.Of course removing parameters from these functions is not easy, we don't want to break existing code.
stefanv commentedon Jan 20, 2023
I'm all for improving simplicity. We can deprecate
shift_xandshift_y. It will take a while, and we'll need to warn for two releases, but it's not the end of the world.stefanv commentedon Jan 20, 2023
Agreed that dilation and binary_dilation should do the same things. BTW, are you able to work on this? It would be fantastic to have someone knowledgeable tackle the issue.
crisluengo commentedon Jan 20, 2023
I am working on it, but I’m not very familiar with the project and so do not know which choices to make and so on. Thank you for the feedback!
If you think deprecating these two input arguments is OK, I’m happy to do so. Is there an example of deprecated functionality somewhere, so I can see how it’s done in this project?
I will make the functions in gray.py produce identical results (with default input arguments) as the ones in binary.py, if given a binary image to work on. I think consistency is important. It should be relatively easy if the ndimage functions are well behaved.
This will cause changes in the output compared to current ones, but only in the case of non-mirror-symmetric footprints.
Removed "fix" in dilation(). Fixes scikit-image#6676.
stefanv commentedon Jan 20, 2023
It's annoying, but we can't easily change output silently. We can potentially detect when the kernel is non-symmetric, and raise an error unless a certain flag has been set. And then in skimage 2.0 (when we redesign the API anyway), we can just do The Right Thing by default.
You can read more about our deprecation cycle here: https://scikit-image.org/docs/stable/contribute.html#deprecation-cycle
Perhaps @lagru has a good recent example.
Removed "fix" in dilation(). Fixes scikit-image#6676.
lagru commentedon Jan 24, 2023
I added this to the skimage2 milestone right now. From #6676 (comment) and #6695 I gather that we settled on keeping this bug for now and fixing it in skimage2?
crisluengo commentedon Jan 24, 2023
@lagru Thanks for the review! I will fix those things if have time later this week.
I wouldn't be keen on keeping a bug in the code. You don't want to change output silently, but I'm sure you want to fix buggy outputs? Currently, the closing with a non-symmetric structuring element (including an even-sized one) is not a closing at all.
From https://scikit-image.org/docs/stable/contribute.html#deprecation-cycle:
Note that the behaviour doesn't change for symmetric structuring elements, which is probably 99.9% of the use cases (i.e. all default footprints). I made the PR such that currently correct behavior doesn't change. But I'm happy to change it to the way I had it initially, where behavior does change and becomes more consistent and simple (including making the binary and gray-scale versions of the operators equal), and we keep it unmerged until work for skimage2 starts.
lagru commentedon Jan 25, 2023
Well, there is benefit in merging #6695 while keeping the status quo even if we haven't decided yet whether we want to post-pone this bug fix for skimage2.
If find your argument convincing that most users probably use symmetric structuring elements but there is really no easy way to know how large the impact would be. And it wouldn't be something users could easily debug. So unless there is an way to detect asymmetric ones without to much performance cost I'd rather wait for skimage2. Otherwise I'm happy to start a deprecation circle now (could be another PR following #6695).
stefanv commentedon Jan 30, 2023
Yes, probably worth pretending skimage2 (where we can break API) isn't happening, even though we know it is.
samyblusseau commentedon May 10, 2023
Hi, thank you for trying to fix this bug!
Right now it's not clear to me if some fix exists in a recent release, by either reading this issue or #6695.
It seems that the problem remains in version 0.20.0, and no 'mirror' or 'mirror_fingerprint' keyword argument is available.
Thanks again!
crisluengo commentedon May 10, 2023
@samyblusseau Indeed, the PR I submitted to fix this has never been taken. I think there were a few things left to fix to appease the maintainers, but I didn’t have time, and then I forgot about this whole thing (I dont use skimage nearly as often as I use DIPlib).
Take a look at the PR referenced here, you can fetch it and use it yourself locally if you wish.
[sorry for closing the issue, I fat-fingered the close button.]
stefanv commentedon May 10, 2023
@crisluengo if you are willing to dig back in a little bit, I'd be happy to personally coax these changes through.
crisluengo commentedon May 10, 2023
@stefanv I'll try to find some time later this week or next week.
Fix bugs and clean up grayscale morphology operators (scikit-image#6695)
Undo (double) mirroring in `ski2.morphology.dilation` (#8060)