Skip to content

Add example code for generating structuring elements.#4528

Merged
jni merged 8 commits intoscikit-image:masterfrom
b-z:add_example_structuring_elements
Apr 20, 2020
Merged

Add example code for generating structuring elements.#4528
jni merged 8 commits intoscikit-image:masterfrom
b-z:add_example_structuring_elements

Conversation

@b-z
Copy link
Copy Markdown
Contributor

@b-z b-z commented Mar 26, 2020

Description

Closes #4431 .
The example shows how to use functions in skimage.morphology to generate structuring elements.
The title of each plot indicates the call of the function.

Figure_1

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.
  • Consider backporting the PR with @meeseeksdev backport to v0.14.x

@sciunto sciunto added the 📄 type: Documentation Updates, fixes and additions to documentation label Mar 26, 2020
@sciunto sciunto requested a review from emmanuelle March 26, 2020 10:14
Copy link
Copy Markdown
Member

@sciunto sciunto left a comment

Choose a reason for hiding this comment

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

Great PR, I love the numbers superimposed on top of the color plot!

I'm wondering if two separate figures would be better, t get ride of the idx and also for the thumbnail. Others can provide their opinion.

=============================

This example shows how to use functions in :py:module:`skimage.morphology`
to generate structuring elements.
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.

I think this paragraph could be improved by explaining what is a structuring element and providing few examples of functions using them (like erosion, dilation, functions in rank filters)...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hello sciunto. Thank you for pointing it out : )
I felt a bit hard to explain what structuring element is (my English is bad..).
I made this as an example, which modified from the current example of dilation function:

>>> import numpy as np
>>> from skimage.morphology import dilation, square, disk
>>> bright_pixel = np.array([[0, 0, 0, 0, 0],
...                          [0, 0, 0, 0, 0],
...                          [0, 0, 1, 0, 0],
...                          [0, 0, 0, 0, 0],
...                          [0, 0, 0, 0, 0]], dtype=np.uint8)
>>> dilation(bright_pixel, square(3))
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)
>>> dilation(bright_pixel, disk(2))
array([[0, 0, 1, 0, 0],
       [0, 1, 1, 1, 0],
       [1, 1, 1, 1, 1],
       [0, 1, 1, 1, 0],
       [0, 0, 1, 0, 0]], dtype=uint8)

Not sure if it should be put into the code.

ax = fig.add_subplot(3, 3, idx)
ax.imshow(struc, cmap="Paired", vmin=0, vmax=12)
for i in range(struc.shape[0]):
for j in range(struc.shape[1]):
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.

Here we could use itertools.product to avoid nested loops, but it's optional.

@soupault
Copy link
Copy Markdown
Member

Thanks for the PR! This looks great!

I think for 2D we should try keeping the same absolute scale of the pixels across the plots. For 3D, one of these visualization methods may suit better - https://matplotlib.org/3.2.0/gallery/mplot3d/voxels.html, https://matplotlib.org/3.2.0/gallery/mplot3d/3d_bars.html .

@b-z
Copy link
Copy Markdown
Contributor Author

b-z commented Apr 1, 2020

Thanks for the comments. I will improve these in my free time.

@sciunto sciunto modified the milestones: 0.17, 0.18 Apr 8, 2020
@sciunto
Copy link
Copy Markdown
Member

sciunto commented Apr 17, 2020

@b-z Do you have a chance to work on this again?

@scikit-image/core As we will improve our API in the future (#4536), I propose to merge this PR soon at it is already good. We will have the occasion to revisit the example later on.

Copy link
Copy Markdown
Member

@rfezzani rfezzani left a comment

Choose a reason for hiding this comment

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

Thank you @b-z for this, I just have some stylistic suggestions ;-)

b-z and others added 4 commits April 19, 2020 22:48
Co-Authored-By: Riadh Fezzani <rfezzani@gmail.com>
Co-Authored-By: Riadh Fezzani <rfezzani@gmail.com>
Co-Authored-By: Riadh Fezzani <rfezzani@gmail.com>
Co-Authored-By: rfezzani <rfezzani@gmail.com>
Copy link
Copy Markdown
Member

@rfezzani rfezzani left a comment

Choose a reason for hiding this comment

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

Thank you @b-z, just two small suggestions more ;-)

@b-z
Copy link
Copy Markdown
Contributor Author

b-z commented Apr 19, 2020

Thanks for the PR! This looks great!

I think for 2D we should try keeping the same absolute scale of the pixels across the plots. For 3D, one of these visualization methods may suit better - https://matplotlib.org/3.2.0/gallery/mplot3d/voxels.html, https://matplotlib.org/3.2.0/gallery/mplot3d/3d_bars.html .

Hello @soupault , thanks for your advice!
I tried to change them to same scale. As for the 3D visualization method proposed, I prefer the voxel plot, and also used same scale.
Do you like the following result better?
image

Co-Authored-By: Riadh Fezzani <rfezzani@gmail.com>
@jni
Copy link
Copy Markdown
Member

jni commented Apr 20, 2020

@b-z I love your latest plots! Could you push the code for those to your branch? It is still showing the dots and the different element sizes.

I also like @rfezzani's suggestions for iterating over the values. However, since you are not going to access the dictionary, it might be better to revert back to a list of tuples! Sorry about the back and forth there...!

Anyway, all that is optional, but the shaded voxels look super nice and we should get that code in since you worked it out!

@rfezzani
Copy link
Copy Markdown
Member

However, since you are not going to access the dictionary,

I did not get the point. Sorry @jni, can you elaborate more on this? 🙂

@sciunto
Copy link
Copy Markdown
Member

sciunto commented Apr 20, 2020

Me too. @rfezzani suggestion looks appropriate to me.

@jni
Copy link
Copy Markdown
Member

jni commented Apr 20, 2020

@rfezzani

Currently we are doing:

structs2d = {'square(5)': square(5), ...}
...
for title, struc in structs2d.items():
    ...

I suggest:

structs2d = [('square(5)', square(5)), ...]
...
for title, struc in structs2d:
    ...

@rfezzani
Copy link
Copy Markdown
Member

I understood that @jni, I just still don't get your objection:

However, since you are not going to access the dictionary,

If it is simply a matter of code style, no problem... Everybody has its own preferences ;-)

b-z and others added 2 commits April 20, 2020 22:35
Modify visulization parameters;
change 3D plot method;
apply @rfezzani 's  suggestions.

Co-Authored-By: rfezzani <rfezzani@gmail.com>
@b-z
Copy link
Copy Markdown
Contributor Author

b-z commented Apr 20, 2020

@rfezzani

Currently we are doing:

structs2d = {'square(5)': square(5), ...}
...
for title, struc in structs2d.items():
    ...

I suggest:

structs2d = [('square(5)', square(5)), ...]
...
for title, struc in structs2d:
    ...

The newest code is using the style rfezzani suggested.
I'm new to open source communities, and cannot decide which to use.
Thanks 😃

@sciunto sciunto self-requested a review April 20, 2020 13:57
@jni
Copy link
Copy Markdown
Member

jni commented Apr 20, 2020

@rfezzani what I meant is that we are not using the mapping feature of the dictionary. We stuff things into a dictionary then immediately unpack it into tuples. So we might as well just use the tuples.

@b-z Don’t worry, this is indeed just a suggestion. It’s common for maintainers to have disagreements, and it’s something you get used to over time — you eventually develop a sense for what are requirements and what are minor suggestions. Either way is minor with this dict/tuple question. Thank you so much for your work!

Copy link
Copy Markdown
Member

@jni jni left a comment

Choose a reason for hiding this comment

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

This is ready to go as far as I’m concerned. Remaining issues are minor and can be addressed in future PRs. Thank you for bearing with us @b-z and thanks everyone for their reviews so far!

@jni jni merged commit d28bc61 into scikit-image:master Apr 20, 2020
@soupault
Copy link
Copy Markdown
Member

@b-z thanks a lot for the updates! This looks amazing - #4528 (comment)! So happy to have this work merged!

@b-z b-z deleted the add_example_structuring_elements branch April 21, 2020 01:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📄 type: Documentation Updates, fixes and additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add examples to docstring for structuring elements to visualize the shape

5 participants