Skip to content

Make Canny more consistent across different data types - #6874

Closed
stefanv wants to merge 2 commits into
scikit-image:mainfrom
stefanv:fix-canny-bleedover-non-masked
Closed

stefanv wants to merge 2 commits into
scikit-image:mainfrom
stefanv:fix-canny-bleedover-non-masked

Conversation

@stefanv

@stefanv stefanv commented Apr 6, 2023

Copy link
Copy Markdown
Member
  • When a mask is not provided, do not do bleed-over calculation; bleed-over normalization does affect the outcome a tiny bit.
  • Do not use preserve_range=True in the Gaussian filter, since it rescales the smoothed image unnecessarily.

Closes #6871

stefanv added 2 commits April 6, 2023 13:55
- When a mask is not provided, do not do bleed-over calculation;
  bleed-over normalization does affect the outcome a tiny bit.
- Do not use `preserve_range=True` in the Gaussian filter, since it
  rescales the smoothed image unnecessarily.
@stefanv

stefanv commented Apr 6, 2023

Copy link
Copy Markdown
Member Author

Test script:

import skimage
from skimage.feature import canny
import numpy as np

im = np.zeros((100, 100))
im[:50, :50] = 1
mask = np.ones_like(im, dtype=bool)

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)))
    print(f"Masked edge pixels count with {dtype} input: ",
          np.count_nonzero(canny(im.astype(dtype),
                                 low_threshold=0, high_threshold=1, mask=mask)))

Output of skimage v0.19:

Edge pixels count with float64 input:  230
Masked edge pixels count with float64 input:  144
Edge pixels count with float32 input:  274
Masked edge pixels count with float32 input:  187
Edge pixels count with uint8 input:  190
Masked edge pixels count with uint8 input:  98
Edge pixels count with uint16 input:  230
Masked edge pixels count with uint16 input:  102
Edge pixels count with uint32 input:  230
Masked edge pixels count with uint32 input:  142
Edge pixels count with uint64 input:  230
Masked edge pixels count with uint64 input:  144
Edge pixels count with int8 input:  270
Masked edge pixels count with int8 input:  182
Edge pixels count with int16 input:  230
Masked edge pixels count with int16 input:  103
Edge pixels count with int32 input:  270
Masked edge pixels count with int32 input:  105
Edge pixels count with int64 input:  230
Masked edge pixels count with int64 input:  144

Output before this patch:

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

Output after this patch:

skimage.__version__='0.21.0rc0.dev0+git20230406.f0bc1ef27'
Edge pixels count with float64 input:  230
Masked edge pixels count with float64 input:  230
Edge pixels count with float32 input:  270
Masked edge pixels count with float32 input:  270
Edge pixels count with uint8 input:  230
Masked edge pixels count with uint8 input:  230
Edge pixels count with uint16 input:  230
Masked edge pixels count with uint16 input:  230
Edge pixels count with uint32 input:  230
Masked edge pixels count with uint32 input:  230
Edge pixels count with uint64 input:  230
Masked edge pixels count with uint64 input:  230
Edge pixels count with int8 input:  230
Masked edge pixels count with int8 input:  230
Edge pixels count with int16 input:  230
Masked edge pixels count with int16 input:  230
Edge pixels count with int32 input:  230
Masked edge pixels count with int32 input:  230
Edge pixels count with int64 input:  230
Masked edge pixels count with int64 input:  230

@stefanv
stefanv requested a review from rfezzani April 6, 2023 21:08
@stefanv stefanv added this to the 0.21 milestone Apr 6, 2023
@stefanv

stefanv commented Apr 6, 2023

Copy link
Copy Markdown
Member Author

Legitimate failure after patch:

import skimage as ski

image_uint8 = ski.data.camera()
image_float = ski.img_as_float(image_uint8)

result_uint8 = ski.feature.canny(image_uint8)
result_float = ski.feature.canny(image_float)

print("delta=", (result_uint8.astype(int) - result_float.astype(int)).sum())

@rfezzani rfezzani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to change boundary mode, better do it in canny function signature, otherwise, everything seems good: running

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 mode in ['reflect', 'constant']:
    print(f"{mode=}")
    for dtype in ["float64", "float32",
                  "uint8", "uint16", "uint32", "uint64",
                  "int8", "int16", "int32", "int64"]:
        print(f"\tPixels count with {dtype} input: ",
              np.count_nonzero(canny(im.astype(dtype),
                                     low_threshold=0, high_threshold=1,
                                     mode=mode)))

gives

skimage.__version__='0.21.0rc0.dev0'
mode='reflect'
	Pixels count with float64 input:  185
	Pixels count with float32 input:  185
	Pixels count with uint8 input:  185
	Pixels count with uint16 input:  185
	Pixels count with uint32 input:  185
	Pixels count with uint64 input:  185
	Pixels count with int8 input:  185
	Pixels count with int16 input:  185
	Pixels count with int32 input:  185
	Pixels count with int64 input:  185
mode='constant'
	Pixels count with float64 input:  270
	Pixels count with float32 input:  270
	Pixels count with uint8 input:  270
	Pixels count with uint16 input:  270
	Pixels count with uint32 input:  270
	Pixels count with uint64 input:  270
	Pixels count with int8 input:  270
	Pixels count with int16 input:  270
	Pixels count with int32 input:  270
	Pixels count with int64 input:  270

Comment thread skimage/feature/_canny.py
# pixel data that's due to significant points)
bleed_over = gaussian(mask.astype(float_type, copy=False),
**gaussian_kwargs) + np.finfo(float_type).eps
sigma=1, mode='nearest') + np.finfo(float_type).eps

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing mode from constant to nearest is not necessary to fix this issue, and is better suited in canny function signature.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it, the results with a full mask do not match the unmasked version.

@rfezzani

rfezzani commented Apr 7, 2023

Copy link
Copy Markdown
Member

Setting use_quantile to True make the results more stable:

import skimage
from skimage.feature import canny
import numpy as np

print(f"{skimage.__version__=}")

for l, h in [(0, 1), (0.01, 0.99), (0.1, 0.9)]:

    print(f"low_threshold={l}, high_threshold={h}")

    for name in ["camera", "moon", "coins"]:
        print(f"Test image: {name}")
        for mode in ['reflect', 'constant']:
            im = getattr(skimage.data, name)()
            print(f"\t{mode=}")
            for dtype in ["uint8", "uint16", "uint32", "uint64",
                          "int16", "int32", "int64"]:
                print(f"\t\tPixels count with {dtype} input: ",
                      np.count_nonzero(canny(im.astype(dtype),
                                             low_threshold=l, high_threshold=h,
                                             mode=mode, use_quantiles=True)))

            im = skimage.img_as_float(im)
            for dtype in ["float64", "float32"]:
                        print(f"\t\tPixels count with {dtype} input: ",
                      np.count_nonzero(canny(im.astype(dtype),
                                             low_threshold=l, high_threshold=h,
                                             mode=mode, use_quantiles=True)))

gives

low_threshold=0, high_threshold=1
	Test image: camera
		mode='reflect'
			Pixels count with uint8 input:  785
			Pixels count with uint16 input:  785
			Pixels count with uint32 input:  785
			Pixels count with uint64 input:  785
			Pixels count with int16 input:  785
			Pixels count with int32 input:  785
			Pixels count with int64 input:  785
			Pixels count with float64 input:  785
			Pixels count with float32 input:  785
		mode='constant'
			Pixels count with uint8 input:  2417
			Pixels count with uint16 input:  2417
			Pixels count with uint32 input:  2417
			Pixels count with uint64 input:  2417
			Pixels count with int16 input:  2417
			Pixels count with int32 input:  2417
			Pixels count with int64 input:  2417
			Pixels count with float64 input:  2417
			Pixels count with float32 input:  2417
	Test image: moon
		mode='reflect'
			Pixels count with uint8 input:  75
			Pixels count with uint16 input:  75
			Pixels count with uint32 input:  75
			Pixels count with uint64 input:  75
			Pixels count with int16 input:  75
			Pixels count with int32 input:  75
			Pixels count with int64 input:  75
			Pixels count with float64 input:  75
			Pixels count with float32 input:  75
		mode='constant'
			Pixels count with uint8 input:  75
			Pixels count with uint16 input:  75
			Pixels count with uint32 input:  75
			Pixels count with uint64 input:  75
			Pixels count with int16 input:  75
			Pixels count with int32 input:  75
			Pixels count with int64 input:  75
			Pixels count with float64 input:  75
			Pixels count with float32 input:  75
	Test image: coins
		mode='reflect'
			Pixels count with uint8 input:  128
			Pixels count with uint16 input:  128
			Pixels count with uint32 input:  128
			Pixels count with uint64 input:  128
			Pixels count with int16 input:  128
			Pixels count with int32 input:  128
			Pixels count with int64 input:  128
			Pixels count with float64 input:  128
			Pixels count with float32 input:  128
		mode='constant'
			Pixels count with uint8 input:  128
			Pixels count with uint16 input:  128
			Pixels count with uint32 input:  128
			Pixels count with uint64 input:  128
			Pixels count with int16 input:  128
			Pixels count with int32 input:  128
			Pixels count with int64 input:  128
			Pixels count with float64 input:  128
			Pixels count with float32 input:  128
low_threshold=0.01, high_threshold=0.99
	Test image: camera
		mode='reflect'
			Pixels count with uint8 input:  5147
			Pixels count with uint16 input:  5147
			Pixels count with uint32 input:  5147
			Pixels count with uint64 input:  5147
			Pixels count with int16 input:  5147
			Pixels count with int32 input:  5147
			Pixels count with int64 input:  5147
			Pixels count with float64 input:  5147
			Pixels count with float32 input:  5147
		mode='constant'
			Pixels count with uint8 input:  6775
			Pixels count with uint16 input:  6775
			Pixels count with uint32 input:  6775
			Pixels count with uint64 input:  6775
			Pixels count with int16 input:  6775
			Pixels count with int32 input:  6775
			Pixels count with int64 input:  6775
			Pixels count with float64 input:  6775
			Pixels count with float32 input:  6775
	Test image: moon
		mode='reflect'
			Pixels count with uint8 input:  2841
			Pixels count with uint16 input:  2841
			Pixels count with uint32 input:  2841
			Pixels count with uint64 input:  2841
			Pixels count with int16 input:  2841
			Pixels count with int32 input:  2841
			Pixels count with int64 input:  2841
			Pixels count with float64 input:  2841
			Pixels count with float32 input:  2841
		mode='constant'
			Pixels count with uint8 input:  3157
			Pixels count with uint16 input:  3157
			Pixels count with uint32 input:  3157
			Pixels count with uint64 input:  3157
			Pixels count with int16 input:  3157
			Pixels count with int32 input:  3157
			Pixels count with int64 input:  3157
			Pixels count with float64 input:  3157
			Pixels count with float32 input:  3157
	Test image: coins
		mode='reflect'
			Pixels count with uint8 input:  2492
			Pixels count with uint16 input:  2492
			Pixels count with uint32 input:  2492
			Pixels count with uint64 input:  2492
			Pixels count with int16 input:  2492
			Pixels count with int32 input:  2492
			Pixels count with int64 input:  2492
			Pixels count with float64 input:  2492
			Pixels count with float32 input:  2492
		mode='constant'
			Pixels count with uint8 input:  2492
			Pixels count with uint16 input:  2492
			Pixels count with uint32 input:  2492
			Pixels count with uint64 input:  2492
			Pixels count with int16 input:  2492
			Pixels count with int32 input:  2492
			Pixels count with int64 input:  2492
			Pixels count with float64 input:  2492
			Pixels count with float32 input:  2492
low_threshold=0.1, high_threshold=0.9
	Test image: camera
		mode='reflect'
			Pixels count with uint8 input:  27244
			Pixels count with uint16 input:  27244
			Pixels count with uint32 input:  27244
			Pixels count with uint64 input:  27244
			Pixels count with int16 input:  27244
			Pixels count with int32 input:  27244
			Pixels count with int64 input:  27244
			Pixels count with float64 input:  27244
			Pixels count with float32 input:  27244
		mode='constant'
			Pixels count with uint8 input:  23813
			Pixels count with uint16 input:  23813
			Pixels count with uint32 input:  23813
			Pixels count with uint64 input:  23813
			Pixels count with int16 input:  23813
			Pixels count with int32 input:  23813
			Pixels count with int64 input:  23813
			Pixels count with float64 input:  23813
			Pixels count with float32 input:  23813
	Test image: moon
		mode='reflect'
			Pixels count with uint8 input:  28775
			Pixels count with uint16 input:  28775
			Pixels count with uint32 input:  28775
			Pixels count with uint64 input:  28775
			Pixels count with int16 input:  28775
			Pixels count with int32 input:  28775
			Pixels count with int64 input:  28775
			Pixels count with float64 input:  28775
			Pixels count with float32 input:  28775
		mode='constant'
			Pixels count with uint8 input:  22544
			Pixels count with uint16 input:  22544
			Pixels count with uint32 input:  22544
			Pixels count with uint64 input:  22544
			Pixels count with int16 input:  22544
			Pixels count with int32 input:  22544
			Pixels count with int64 input:  22544
			Pixels count with float64 input:  22544
			Pixels count with float32 input:  22544
	Test image: coins
		mode='reflect'
			Pixels count with uint8 input:  8978
			Pixels count with uint16 input:  8978
			Pixels count with uint32 input:  8978
			Pixels count with uint64 input:  8978
			Pixels count with int16 input:  8978
			Pixels count with int32 input:  8978
			Pixels count with int64 input:  8978
			Pixels count with float64 input:  8978
			Pixels count with float32 input:  8978
		mode='constant'
			Pixels count with uint8 input:  9764
			Pixels count with uint16 input:  9764
			Pixels count with uint32 input:  9764
			Pixels count with uint64 input:  9764
			Pixels count with int16 input:  9764
			Pixels count with int32 input:  9764
			Pixels count with int64 input:  9764
			Pixels count with float64 input:  9764
			Pixels count with float32 input:  9764

@stefanv

stefanv commented Apr 7, 2023

Copy link
Copy Markdown
Member Author

@rfezzani The bleed over is incorrect for the full mask case; this should already be alarming. We cannot just change to the quantile version, that would be backward incompatible.

Priority here is to figure out why your change modified the output, address that change (even if that means reverting that and all subsequent changes to canny); thereafter, we need to fix up the masked case.

You say the image mode and the bleedover modes should be tied together, but look at what happens: the bleedover mask is divided into the image, and since those values are never close to 1 for a full image mask, this is completely broken.

@stefanv

stefanv commented Apr 7, 2023

Copy link
Copy Markdown
Member Author

We cannot just change to the quantile version, that would be backward incompatible.

If you meant this as advice for the end-user, then yes agreed---it does make the result more stable.

@stefanv

stefanv commented Apr 7, 2023

Copy link
Copy Markdown
Member Author

Given that uint64 is not a supported data-type, I think I will catch that situation, throw an error, and change preserve_range=False. That should recover behavior from 0.19, as well as align the edge detection between masked and non-masked modes.

@stefanv

stefanv commented Apr 7, 2023 •

Copy link
Copy Markdown
Member Author

Phew, after some more headscratching, I figured out another element of the problem: the masked behavior is better than the non-masked behavior. A long time ago, we made a poor choice of default mode='constant'. This means that canny finds an edge at the border of every image.

Looking at the difference between v0.20 and v0.19, the border may account for the large reduction in values seen (will confirm).

The only change necessary then is to prevent uint64 images from being processed; this "worked" in the past, but it was never guaranteed.

If, indeed, the border behavior changed between v0.19 and v0.20, we may have to reintroduce that 😬 and fix it in skimage v2.

@lagru

lagru commented Apr 7, 2023

Copy link
Copy Markdown
Member

Should #6874 (comment) go into API changes for skimage2 - HackMD?

@stefanv

stefanv commented Apr 7, 2023

Copy link
Copy Markdown
Member Author

Should #6874 (comment) go into API changes for skimage2 - HackMD?

I already added it to the v1 issue, #5439; if that's no longer where you're tracking the v2 changes, let's unify the two documents and add links between them.

@stefanv

stefanv commented Apr 7, 2023

Copy link
Copy Markdown
Member Author

A lesson from long ago that I've apparently forgotten: always look at the output.

@stefanv

stefanv commented Apr 7, 2023

Copy link
Copy Markdown
Member Author

v0 20
v0 19

@stefanv

stefanv commented Apr 7, 2023

Copy link
Copy Markdown
Member Author

I'd consider the v0.20 behavior to be more correct than the v0.19 behavior.

Changes proposed then: prohibit 64-bit integer images as input; add test to ensure masked and unmasked outputs are equal.

@stefanv stefanv closed this Apr 7, 2023
@lagru lagru added the 🩹 type: Bug fix Fixes unexpected or incorrect behavior label Apr 7, 2023
@jarrodmillman jarrodmillman removed this from the 0.21 milestone Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🩹 type: Bug fix Fixes unexpected or incorrect behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New canny implementation silently fails with integer images.

4 participants