Repository navigation
Add no-reference perceptual blur metric. - #5420
Conversation
|
I didn't notice the second failure (23b8d30) locally, because I only ran a subset of the test suite ( I should have noticed from the first failure (c5ba0f4) that I couldn't get away from the circular import issue: Therefore, I decided to move the new function under /cc @ladretp |
|
@ladretp I did 74f488b because "DeprecationWarning: |
Another way around the circular imports is to import the needed Sobel functions internally within |
grlee77
left a comment
There was a problem hiding this comment.
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
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. |
Good point, let me check. I'll probably add |
Co-authored-by: Patricia Ladret <[email protected]>
Co-authored-by: Patricia Ladret <[email protected]>
|
Hello @mkcor! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:
Comment last updated at 2021-06-24 15:21:38 UTC |
|
@grlee77 I added 82a16cc to avoid this 7-year-old warning message: scikit-image/skimage/filters/_gaussian.py Lines 113 to 115 in 9072919 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 |
Co-authored-by: Patricia Ladret <[email protected]>
grlee77
left a comment
There was a problem hiding this comment.
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.
| n_axes = image.ndim | ||
|
|
||
| if channel_axis is not None: | ||
| if not isinstance(channel_axis, int) or channel_axis >= image.ndim: |
There was a problem hiding this comment.
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 iterableThere was a problem hiding this comment.
Good point! I've decided to combine both, so the error starts with a message that mentions channel_axis: 752f5d7 What do you think?
|
Dear Gregory
I'm one of the author of this paper and I propose this function here
because now I'm using python scikit-image, -learn etc...
The matlab file is here:
https://fr.mathworks.com/matlabcentral/fileexchange/24676-image-blur-metric?s_tid=srchtitle
BUT and that's why I wanted to rewrite the function, this one was not
written by one of us (the authors) but by a nice unknown person who did
not even warn us (we discovered the function here by chance). This
function does not include the treatment of the edges of the image, and
especially the derivative is calculated very roughly, that's why I
wanted to use Sobel. Moreover, and this is one of the justified
criticisms of some users, one cannot change the size of the filter
(size_h) inside the function and when the image has very important blur
it does not give convincing results...
I'm very sorry Marianne is implementing for me in scikit-image this
function because I don't know the github process ..., thanks to her!
Patricia
Le 10/06/2021 à 22:28, Gregory R. Lee a écrit :
…
***@***.**** requested changes on this pull request.
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.
------------------------------------------------------------------------
In doc/examples/filters/plot_blur_effect.py
<#5420 (comment)>:
> +# Let us blur this image increasingly, by applying it a uniform filter with
+# increasing size.
⬇️ Suggested change
-# Let us blur this image increasingly, by applying it a uniform filter with
-# increasing size.
+# Let us blur this image with a series of uniform filters of increasing size.
------------------------------------------------------------------------
In skimage/filters/_blur_effect.py
<#5420 (comment)>:
> +
+ Parameters
+ ----------
+ image : ndarray
+ RGB or grayscale nD image. The input image is converted to grayscale
+ before computing the blur metric.
+ h_size : int, optional
+ Size of the re-blurring filter. Default is 11.
+ channel_axis : int or None, optional
+ If None, the image is assumed to be grayscale (single-channel).
+ Otherwise, this parameter indicates which axis of the array
+ corresponds to color channels.
+
+ Returns
+ -------
+
⬇️ Suggested change
-
------------------------------------------------------------------------
In skimage/filters/_blur_effect.py
<#5420 (comment)>:
> + channel_axis : int or None, optional
+ If None, the image is assumed to be grayscale (single-channel).
+ Otherwise, this parameter indicates which axis of the array
+ corresponds to color channels.
+
+ Returns
+ -------
+
+ blur : float (0 to 1)
+ Blur metric: the maximum of blur metrics along all axes.
+ blur_table : list of floats
+ The i-th element is the blur metric along the i-th axis.
+
+ Notes
+ -----
+
⬇️ Suggested change
-
------------------------------------------------------------------------
In skimage/filters/_blur_effect.py
<#5420 (comment)>:
> + towards an asymptote.
+
+ References
+ ----------
+ .. [1] Frederique Crete, Thierry Dolmiere, Patricia Ladret, and Marina
+ Nicolas "The blur effect: perception and estimation with a new
+ no-reference perceptual blur metric" Proc. SPIE 6492, Human Vision and
+ Electronic Imaging XII, 64920I (2007)
+ https://hal.archives-ouvertes.fr/hal-00232709
+ :DOI:`10.1117/12.702790`
+ """
+ # number of non-channel axes
+ n_axes = image.ndim
+
+ if channel_axis is not None:
+ if not isinstance(channel_axis, int) or channel_axis >= image.ndim:
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
------------------------------------------------------------------------
In skimage/filters/_blur_effect.py
<#5420 (comment)>:
> + if not isinstance(channel_axis, int) or channel_axis >= image.ndim:
+ raise ValueError('channel_axis value is invalid')
+ else:
+ n_axes = n_axes - 1
+ # ensure color channels are in the final dimension
+ image = np.moveaxis(image, channel_axis, -1)
+ image = rgb2gray(image)
+ image = img_as_float(image)
+ shape = image.shape
+ B = []
+
+ for a in range(n_axes):
+ filt_im = ndi.uniform_filter1d(image, h_size, axis=a)
+ im_sharp = np.abs(sobel(image, axis=a))
+ im_blur = np.abs(sobel(filt_im, axis=a))
+ T = np.fmax(np.zeros_like(image), im_sharp - im_blur)
⬇️ Suggested change
- T = np.fmax(np.zeros_like(image), im_sharp - im_blur)
+ T = np.maximum(0, im_sharp - im_blur)
This way avoids creation of a separate zeros array
------------------------------------------------------------------------
In skimage/filters/_blur_effect.py
<#5420 (comment)>:
> + raise ValueError('channel_axis value is invalid')
+ else:
+ n_axes = n_axes - 1
+ # ensure color channels are in the final dimension
+ image = np.moveaxis(image, channel_axis, -1)
+ image = rgb2gray(image)
+ image = img_as_float(image)
+ shape = image.shape
+ B = []
+
+ for a in range(n_axes):
+ filt_im = ndi.uniform_filter1d(image, h_size, axis=a)
+ im_sharp = np.abs(sobel(image, axis=a))
+ im_blur = np.abs(sobel(filt_im, axis=a))
+ T = np.fmax(np.zeros_like(image), im_sharp - im_blur)
+ slices = [slice(2, shape[a] - 1) for a in range(n_axes)]
⬇️ Suggested change
- slices = [slice(2, shape[a] - 1) for a in range(n_axes)]
+ slices = [slice(2, shape[ax] - 1) for ax in range(n_axes)]
|a| was already used for the outer for loop variable, so it seems
safer to use a separate name here
Where does this slice excluding a single edge voxel come from? Is it
due to the "radius" of the sobel kernel?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5420 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJUVJ3LISJIR2LCNEY4HS4DTSEN6VANCNFSM46KRTBCA>.
|
|
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. |
Co-authored-by: Gregory R. Lee <[email protected]>
Co-authored-by: Gregory R. Lee <[email protected]>
| 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 |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
| M2 = np.sum(T[tuple(slices)]) | ||
| B.append(np.abs((M1 - M2)) / M1) | ||
|
|
||
| return np.max(B), B |
There was a problem hiding this comment.
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?
| return np.max(B), B | |
| return B if metric is None else metric(B) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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....
There was a problem hiding this comment.
Ok, I see! I admit that returning multiple values isn't the most elegant... Let me add an optional reduce_func then!
Co-authored-by: Riadh Fezzani <[email protected]>
Co-authored-by: Riadh Fezzani <[email protected]>
Co-authored-by: Riadh Fezzani <[email protected]>
Oh, good question! We are measuring the strength of blur indeed... Somehow I didn't think of |
|
Sorry, I didn't noticed the move you made from |
Well, I would tend to have a more 'semantic' approach to the gallery organization. After all, there is no strict match (bijection) between the I don't think this example could fall under |
|
It is really not a big deal, the gallery example can stay without any hurt in |
In the end, that's what I did in 766eb18 ;) |
@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
./doc/examples(new features only)./benchmarks, if your changes aren't covered by anexisting benchmark
For reviewers
later.
__init__.py.doc/release/release_dev.rst.