Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto masking tool #46

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/tutorials/image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,43 @@ Diffraction patterns can come in a variety of exotic file formats. Scikit-ued ha
* All other file formats supported by `scikit-image`_.

The :func:`diffread` function will transparently distinguish between those formats and dispatch to the right functions.
.. _automatic_mask_generation:

Automatic generation of a mask as needed for the autocenter() function
======================================================================

Automatic mask generation
-------------------------
A mask can be used for the :func: `autocenter` function to exclude the beam stopper from the analysis and only keep valuable
information. Such a mask can automatically be generated using this function; given a diffraction pattern, the mask will be created to block
the darkest regions of the image (default blocks about 10% of the darker values).
Here is an example:

.. plot::

import matplotlib.pyplot as plt
from skued import diffread, auto_masking

image = diffread('data/Cr_1.tif')
mask = auto_masking(image, threshold = 0.1)

# Reduce size of images because of memory usage of ReadTheDocs
image = image[::3, ::3]
mask = mask[::3, ::3]

fig , (ax1, ax2) = plt.subplots(1,2, figsize = (9,3))
ax1.imshow(image, vmin=0, vmax=150, cmap='inferno')
ax2.imshow(mask, vmin=0, vmax=1, cmap='inferno')

for ax in (ax1, ax2):
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)

ax1.set_title('Cr_1')
ax2.set_title('Mask')

plt.tight_layout()
plt.show()
.. _alignment:

Automatic center-finding
Expand Down
1 change: 1 addition & 0 deletions skued/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from .image import (
align,
autocenter,
auto_masking,
azimuthal_average,
brillouin_zones,
combine_masks,
Expand Down
2 changes: 1 addition & 1 deletion skued/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .alignment import align, ialign, itrack_peak
from .brillouin import brillouin_zones
from .calibration import detector_scattvectors, powder_calq
from .center import autocenter
from .center import autocenter, auto_masking
from .indexing import bragg_peaks, bragg_peaks_persistence
from .metrics import (
combine_masks,
Expand Down
26 changes: 26 additions & 0 deletions skued/image/center.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,29 @@ def _center_of_intensity(im, mask=None):
r_ = np.average(rr, weights=weights)
c_ = np.average(cc, weights=weights)
return int(r_), int(c_)


def auto_masking(im, threshold=0.1):
"""
Generate a mask based on the darkest fraction of an image

Parameters
----------
im : floats, ndarrays of shape (N,M)
image used to generate a mask
threshold: float, optional
fraction of the lowest values to be masked, default = 15%

Yields
------
mask : boolean, ndarrays of shape (N,M)
Mask that evaluates to True on valid pixels.

"""
# Find the median of the highest intensity value of the image to avoid hot spots
max_median = np.median([max(x) for x in np.real(im)])
# Set the threshold value
lower_limit = threshold * max_median
# generate a mask
mask = im >= lower_limit
return mask
Loading