Skip to content

Add no-reference perceptual blur metric. - #5420

Merged
rfezzani merged 20 commits into
scikit-image:mainfrom
mkcor:blur-effect
Jun 30, 2021
Merged

rfezzani merged 20 commits into
scikit-image:mainfrom
mkcor:blur-effect

Conversation

@mkcor

@mkcor mkcor commented Jun 8, 2021

Copy link
Copy Markdown
Member

@ladretp (Patricia Ladret) would like to submit this contribution: It's a function which estimates the strength of overall blur in an image. It is available in Matlab and it is quite popular, so we might as well have it in scikit-image!

Reference: https://doi.org/10.1117/12.702790

Description

Checklist

For reviewers

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.

@mkcor

mkcor commented Jun 9, 2021

Copy link
Copy Markdown
Member Author

I didn't notice the second failure (23b8d30) locally, because I only ran a subset of the test suite ($ pytest skimage/metrics).

I should have noticed from the first failure (c5ba0f4) that I couldn't get away from the circular import issue:
'blur_effect' under metrics would depend on 'sobel_h' and 'sobel_v' from filters but those depend on 'laplacian' from restoration which in turn depends on 'mean_squared_error' from metrics 😬

Therefore, I decided to move the new function under filters (a6bbac4) -- after all, the blur effect is often related to the use of filters, so I think it makes sense that blur_effect would fall under skimage.filters. In addition, all skimage.metrics functions take two images as inputs (measuring the distance between the two images according to the given metric), so I think our new blur_effect function belongs to a different family.

/cc @ladretp

@mkcor

mkcor commented Jun 9, 2021

Copy link
Copy Markdown
Member Author

@ladretp I did 74f488b because "DeprecationWarning: np.float is a deprecated alias for the builtin float. To silence this warning, use float by itself." Also, np.float64 is the default dtype (https://numpy.org/doc/stable/reference/generated/numpy.ones.html).

@grlee77

grlee77 commented Jun 9, 2021

Copy link
Copy Markdown
Contributor

I should have noticed from the first failure (c5ba0f4) that I couldn't get away from the circular import issue:
'blur_effect' under metrics would depend on 'sobel_h' and 'sobel_v' from filters but those depend on 'laplacian' from restoration which in turn depends on 'mean_squared_error' from metrics grimacing

Another way around the circular imports is to import the needed Sobel functions internally within blur_effect rather than at the top of the file.

@grlee77 grlee77 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I just started looking at this, but it seems that conceptually it should also work in n-dimensions? I left some initial review comments related to this

Comment thread skimage/filters/_blur_effect.py Resolved
Comment thread skimage/filters/_blur_effect.py Resolved
Comment thread skimage/filters/_blur_effect.py Resolved
Comment thread skimage/filters/_blur_effect.py Resolved
@mkcor

mkcor commented Jun 9, 2021

Copy link
Copy Markdown
Member Author

Another way around the circular imports is to import the needed Sobel functions internally within blur_effect rather than at the top of the file.

Oh, within the function? Makes sense, thank you!

Thank you for reviewing, @grlee77 -- I'll include your suggestions and add a little demo (gallery example) as well.

@mkcor

mkcor commented Jun 9, 2021

Copy link
Copy Markdown
Member Author

I just started looking at this, but it seems that conceptually it should also work in n-dimensions? I left some initial review comments related to this

Good point, let me check. I'll probably add channel_axis=None in the function's signature then.

@mkcor

mkcor commented Jun 9, 2021

Copy link
Copy Markdown
Member Author

I have rebased (fcabedf) because the email address I initially used for Patricia Ladret @ladretp was obsolete.

Co-authored-by: Patricia Ladret <[email protected]>
@pep8speaks

pep8speaks commented Jun 9, 2021 •

Copy link
Copy Markdown

Hello @mkcor! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 72:35: E703 statement ends with a semicolon

Line 16:25: E127 continuation line over-indented for visual indent
Line 18:25: E127 continuation line over-indented for visual indent

Comment last updated at 2021-06-24 15:21:38 UTC

@mkcor

mkcor commented Jun 10, 2021

Copy link
Copy Markdown
Member Author

@grlee77 I added 82a16cc to avoid this 7-year-old warning message:

msg = ("Images with dimensions (M, N, 3) are interpreted as 2D+RGB "
"by default. Use `multichannel=False` to interpret as "
"3D image with last dimension of length 3.")

but maybe we'll update it in a follow-up PR to #5286? Am I right in taking the approach that the input image is grayscale by default (channel_axis=None), so the user should use the channel_axis parameter explicitly whenever the image is multichannel?

I know that we also have channel_axis=-1 defaults throughout the API: My understanding is that this is the case for functions which typically apply to multichannel images, where channel_axis=-1 means that the channel dimension is the final one by default (as opposed to any other dimension, not so much as opposed to the image being single-channel). I would like to make sure I'm in line with the latest choices in terms of API / philosophy.

@mkcor
mkcor requested a review from grlee77 June 10, 2021 16:57

@grlee77 grlee77 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good to me aside from a few minor comments.

Nice use of plotly to be able to scroll through the blurred images.

The tests that are here look good. If there is a reference implementation that is commonly used in the literature that we want to ensure a close match to (I think you mentioned a Matlab implementation) we could try to save a result from that third party library for comparison to ensure we give consistent results with previously published studies.

Comment thread skimage/filters/_blur_effect.py Resolved
Comment thread skimage/filters/_blur_effect.py Resolved
Comment thread skimage/filters/_blur_effect.py Outdated
n_axes = image.ndim

if channel_axis is not None:
if not isinstance(channel_axis, int) or channel_axis >= image.ndim:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should also error on channel_axis < -image.ndim, but I'm not sure we need to check these at all or just let np.moveaxis handle it? It seems moveaxis would return a TypeError for non-integer channel_axis and a numpy.AxisError if the axis is out of range.

The main benefit of us raising the error is that we can make a more user-friendly error message that mentions channel_axis by name instead of, for example:

np.moveaxis(np.random.randn(16, 32), 1.5, -1)
Traceback (most recent call last):

  File "<ipython-input-53-0100d5f06b0f>", line 1, in <module>
    np.moveaxis(np.random.randn(16, 32), 1.5, -1)

  File "<__array_function__ internals>", line 5, in moveaxis

  File "/home/lee8rx/mambaforge/envs/skimage_dev/lib/python3.9/site-packages/numpy/core/numeric.py", line 1461, in moveaxis
    source = normalize_axis_tuple(source, a.ndim, 'source')

  File "/home/lee8rx/mambaforge/envs/skimage_dev/lib/python3.9/site-packages/numpy/core/numeric.py", line 1391, in normalize_axis_tuple
    axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])

TypeError: 'float' object is not iterable

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.

Good point! I've decided to combine both, so the error starts with a message that mentions channel_axis: 752f5d7 What do you think?

Comment thread skimage/filters/_blur_effect.py Resolved
Comment thread skimage/filters/_blur_effect.py Resolved
@ladretp

ladretp commented Jun 11, 2021 via email

Copy link
Copy Markdown
Contributor

@grlee77

grlee77 commented Jun 11, 2021

Copy link
Copy Markdown
Contributor

Thanks for the context, @ladretp! Based on this, let's not try to make a comparison to the existing Matlab implementation. I am fine with the existing tests as-is.

Comment on lines +60 to +65
except np.AxisError:
print('channel_axis must be one of the image array dimensions')
raise
except TypeError:
print('channel_axis must be an integer')
raise

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, this looks fine to me. I forget which version of NumPy first introduced np.AxisError, but I think it is earlier than our minimum supported version (indeed CI passed for the minimal dependencies case).

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.

Right, our minimum supported version for NumPy is 1.17.0 and AxisError was already available in 1.13.0: https://numpy.org/doc/stable/release/1.13.0-notes.html?highlight=axiserror#compatibility-notes

@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.

Thank you @mkcor, I left few suggestions 😉 .

Comment thread skimage/filters/_blur_effect.py Resolved
Comment thread skimage/filters/_blur_effect.py Resolved
Comment thread skimage/filters/_blur_effect.py Resolved
Comment thread skimage/filters/_blur_effect.py Outdated
M2 = np.sum(T[tuple(slices)])
B.append(np.abs((M1 - M2)) / M1)

return np.max(B), B

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.

What about introducing a metric parameter which is a reduction function (with default to np.max), to be applied to B?
This allows to use what ever reduction to B (mean, std...), to return plain B when it is set to None and avoids returning B along with its maximum as it is made here?

Suggested change
return np.max(B), B
return B if metric is None else metric(B)

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.

Plain B is available as the second returned value, so the user can always access it and aggregate it:

_, plain_B = blur_effect(my_image)
my_blur_metric = np.mean(plain_B)

for example. The 'default' to np.max is based on the referenced paper: https://hal.archives-ouvertes.fr/hal-00232709/document where the metric is validated against human perception.

@rfezzani rfezzani Jun 23, 2021 •

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.

Yes, I see that plain B is returned, and that's what I find not elegant! Most users will use only one of the two returned values...
So I though about something similar to the reduce_func argument in skimage.measure.profile_line....

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.

Ok, I see! I admit that returning multiple values isn't the most elegant... Let me add an optional reduce_func then!

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.

Excellent, thank you @mkcor 😉

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.

Here it is: 62dd792 🙂
Thank you for the thorough review, @rfezzani, it's been really helpful! 💪

Comment thread skimage/filters/_blur_effect.py Resolved

@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.

Thank you @mkcor, the code looks find to me, but I wonder if this function mustn't take place in skimage.measure instead of skimage.filter...

@mkcor

mkcor commented Jun 24, 2021

Copy link
Copy Markdown
Member Author

Thank you @mkcor, the code looks fine to me, but I wonder if this function mustn't take place in skimage.measure instead of skimage.filter...

Oh, good question! We are measuring the strength of blur indeed... Somehow I didn't think of skimage.measure when moving away from skimage.metrics! I'm totally open to moving the blur metric function under skimage.measure; do you think the gallery example can still fall under doc/examples/filters/?

@rfezzani

rfezzani commented Jun 24, 2021 •

Copy link
Copy Markdown
Member

Sorry, I didn't noticed the move you made from metrics to filters 😕... But yes, I think that measure is a better place for this function 😉
If you decide to move blur_effect from filter to measure, there is no point to keep the gallery example in the doc/examples/filters directory since the filters module will no more be imported in the script.... But this is not a big deal :)

@mkcor

mkcor commented Jun 24, 2021

Copy link
Copy Markdown
Member Author

If you decide to move blur_effect from filter to measure, there is no point to keep the gallery example in the doc/examples/filters directory since the filters module will no more be imported in the script.... But this is not a big deal :)

Well, I would tend to have a more 'semantic' approach to the gallery organization. After all, there is no strict match (bijection) between the skimage submodules and the gallery's subdirectories: https://github.com/scikit-image/scikit-image/tree/main/doc/examples

I don't think this example could fall under applications/ nor features_detection/... definitely not edges/ nor segmentation/... Most gallery examples importing measure seem to be found under transform/: Do you think it would make sense there?

@rfezzani

Copy link
Copy Markdown
Member

It is really not a big deal, the gallery example can stay without any hurt in doc/examples/filters 😉

@mkcor

mkcor commented Jun 24, 2021

Copy link
Copy Markdown
Member Author

Another way around the circular imports is to import the needed Sobel functions internally within blur_effect rather than at the top of the file.

In the end, that's what I did in 766eb18 ;)

@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.

Thank you @mkcor, merging 🎉

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants