.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\region_of_interest\ExtractLineProfile.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_region_of_interest_ExtractLineProfile.py: Extract line profile from image interactively ============================================= Interactively extract a line profile (with a certain width) from an image using :py:class:`~.api.roi.Line2DROI`. Use :func:`~.api.plot.plot_spectra` to plot several line profiles on the same figure. Save a profile data as ``msa`` file. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. figure:: ../../_static/interactive_profiles.gif :align: center :width: 1024 :alt: Interactive example gif of region of interest and associated profile plots. Extracting line profiles and interactive plotting. .. GENERATED FROM PYTHON SOURCE LINES 20-21 Initialize image data as HyperSpy signal: .. GENERATED FROM PYTHON SOURCE LINES 21-26 .. code-block:: Python import hyperspy.api as hs import holospy as holo im0 = holo.data.Fe_needle_reference_hologram() im1 = holo.data.Fe_needle_hologram() .. GENERATED FROM PYTHON SOURCE LINES 27-29 Intialize Line-ROI from position (400,250) to position (220,600) of width 5 in calibrated axes units (in the current example equal to the image pixels): .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python line_roi = hs.roi.Line2DROI(400, 250, 220, 600, 5) .. GENERATED FROM PYTHON SOURCE LINES 32-34 Extract data along the ROI as new signal by "slicing" the signal and plot the profile: .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python profile = line_roi(im0) profile.plot() .. image-sg:: /auto_examples/region_of_interest/images/sphx_glr_ExtractLineProfile_001.png :alt: Vacuum reference hologram Signal :srcset: /auto_examples/region_of_interest/images/sphx_glr_ExtractLineProfile_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-42 Slicing of the signal is not interactive. If you want to modify the line along which the profile is extracted, you can plot the image and display the ROI interactively (creates a new signal object). You can even display the same ROI on a second image to make sure that a profile is well placed on both images: .. GENERATED FROM PYTHON SOURCE LINES 42-47 .. code-block:: Python im0.plot() profile1 = line_roi.interactive(im0, color='green') im1.plot() profile2 = line_roi.interactive(im1, color='green') .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/region_of_interest/images/sphx_glr_ExtractLineProfile_002.png :alt: Vacuum reference hologram Signal :srcset: /auto_examples/region_of_interest/images/sphx_glr_ExtractLineProfile_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/region_of_interest/images/sphx_glr_ExtractLineProfile_003.png :alt: Hologram of Fe needle Signal :srcset: /auto_examples/region_of_interest/images/sphx_glr_ExtractLineProfile_003.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 48-52 You can then drag and drop the ends of the ROI to adjust the placement. If you want to later update the ROI initialization with the modified parameters, you can print these: .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python print(tuple(line_roi)) .. rst-class:: sphx-glr-script-out .. code-block:: none (400.0, 250.0, 220.0, 600.0, 5.0) .. GENERATED FROM PYTHON SOURCE LINES 55-57 You can now directly access the data of the profile objects, e.g. to plot both profiles in a single plot: .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python hs.plot.plot_spectra([profile1, profile2]) # Choose the fourth figure as gallery thumbnail: # sphinx_gallery_thumbnail_number = 4 .. image-sg:: /auto_examples/region_of_interest/images/sphx_glr_ExtractLineProfile_004.png :alt: ExtractLineProfile :srcset: /auto_examples/region_of_interest/images/sphx_glr_ExtractLineProfile_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 62-64 Since the profile is a signal object, you can use any other functionality provided by hyperspy, e.g. to save a profile as `.msa` text file: .. GENERATED FROM PYTHON SOURCE LINES 64-65 .. code-block:: Python profile1.save('extracted-line-profile.msa', format='XY', overwrite=True) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.896 seconds) .. _sphx_glr_download_auto_examples_region_of_interest_ExtractLineProfile.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ExtractLineProfile.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ExtractLineProfile.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_