.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/virtual_imaging/vdf_vbf.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_examples_virtual_imaging_vdf_vbf.py: Virtual Dark/Bright Field (VDF/VBF) Imaging =========================================== This example demonstrates how to acquire a VDF/VBF image using DE API. The virtual images are created by summing or subtracting the pixel values of the input image based on the values of the virtual masks. The virtual masks are 8-bit images with values: - 0: Negative Mask, pixel values subtracted - 1: Default value, pixels not accounted for - 2: Positive Mask, pixel values added Here we will 1. Connect to the DE server 2. Set the hardware ROI 3. Acquire a single diffraction pattern for guiding the virtual imaging 4. Automatically generate the virtual birght field (VBF) and virtual dark field (VDF) images by finding the brightest disk in the diffraction pattern and measuring the extent of the disk in the diffraction pattern. 5. Acquire the VDF/VBF images and plot them. .. GENERATED FROM PYTHON SOURCE LINES 23-37 .. code-block:: Python import numpy as np from deapi import Client import time from scipy.ndimage import gaussian_filter from skimage.segmentation import flood from skimage.morphology import dilation, disk import matplotlib.pyplot as plt client = Client() client.usingMmf = False # True if on same machine as DE Server and a Windows machine client.connect(port=13241) # connect to the running DE Server .. GENERATED FROM PYTHON SOURCE LINES 38-42 Get A Single Diffraction Pattern -------------------------------- We will acquire a single diffraction pattern to guide the virtual masks. We can then use the brightest disk in the diffraction pattern to generate the VDF/VBF masks. .. GENERATED FROM PYTHON SOURCE LINES 42-51 .. code-block:: Python client.start_acquisition(1) # wait for the acquisition to finish while client.acquiring: time.sleep(1) img = client.get_result("singleframe_integrated")[0] .. GENERATED FROM PYTHON SOURCE LINES 52-53 Define a function to automatically find the bright field in the diffraction pattern .. GENERATED FROM PYTHON SOURCE LINES 53-67 .. code-block:: Python def auto_find_bf(img, threshold=0.5, sigma=10, dilation_rad=30): filtered = gaussian_filter(img, sigma=sigma) center = np.unravel_index(np.argmax(filtered), shape=filtered.shape) val = img[center] mask = img > (val * threshold) mask = flood(mask, center) # only the center mask = dilation(mask, footprint=disk(dilation_rad)) return mask mask = auto_find_bf(img) .. GENERATED FROM PYTHON SOURCE LINES 68-71 Create the Virtual Masks ------------------------ We will create the VDF and VBF masks by setting the pixel values in the masks. .. GENERATED FROM PYTHON SOURCE LINES 71-84 .. code-block:: Python client.virtual_masks[1].calculation = "Sum" client.virtual_masks[1].name = "VBF" client.virtual_masks[1][:] = 1 # Set to 1 client.virtual_masks[1][mask] = 2 # Set mask to 2 client.virtual_masks[1].plot() client.virtual_masks[2].calculation = "Sum" client.virtual_masks[2].name = "VDF" client.virtual_masks[2][:] = 2 client.virtual_masks[2][mask] = 1 client.virtual_masks[2].plot() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/virtual_imaging/images/sphx_glr_vdf_vbf_001.png :alt: vdf vbf :srcset: /examples/virtual_imaging/images/sphx_glr_vdf_vbf_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/virtual_imaging/images/sphx_glr_vdf_vbf_002.png :alt: vdf vbf :srcset: /examples/virtual_imaging/images/sphx_glr_vdf_vbf_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/deapi/deapi/deapi/data_types.py:537: UserWarning: Virtual mask shape is not set to Arbitrary. Setting to Arbitrary. warnings.warn( .. GENERATED FROM PYTHON SOURCE LINES 85-88 Acquire the Virtual Images -------------------------- We will then acquire the virtual images using the virtual masks and plot the results. .. GENERATED FROM PYTHON SOURCE LINES 88-102 .. code-block:: Python client["Frames Per Second"] = 5000 # 5000 frames per second client.scan(enable="On", size_x=128, size_y=128) client.start_acquisition() while client.acquiring: # wait for acquisition to finish and then plot the results time.sleep(1) fig, axs = plt.subplots(1, 3) for a, virt in zip(axs, ["virtual_image0", "virtual_image1", "virtual_image2"]): data, _, _, _ = client.get_result(virt) a.imshow(data) a.set_title(virt) .. image-sg:: /examples/virtual_imaging/images/sphx_glr_vdf_vbf_003.png :alt: virtual_image0, virtual_image1, virtual_image2 :srcset: /examples/virtual_imaging/images/sphx_glr_vdf_vbf_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.636 seconds) .. _sphx_glr_download_examples_virtual_imaging_vdf_vbf.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: vdf_vbf.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: vdf_vbf.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: vdf_vbf.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_