.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_differentiate/07_01_differentiate_module.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. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_07_differentiate_07_01_differentiate_module.py: Differentiate Module ==================== In this example, we demonstrate how to use the differentiate module to compute different differential operations on an image. .. GENERATED FROM PYTHON SOURCE LINES 14-16 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 16-27 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import make_axes_locatable import requests from io import BytesIO from PIL import Image # Import the Differentials class from your module (adjust import path as needed) from splineops.differentiate.differentials import differentials .. GENERATED FROM PYTHON SOURCE LINES 28-33 Data Preparation ---------------- We retrieve an example color image, convert it to grayscale, and normalize its intensities to the [0,1] range. .. GENERATED FROM PYTHON SOURCE LINES 33-49 .. code-block:: Python url = 'https://r0k.us/graphics/kodak/kodak/kodim15.png' response = requests.get(url) img = Image.open(BytesIO(response.content)) image = np.array(img, dtype=np.float64) # Convert to [0,1] image_normalized = image / 255.0 # Convert to grayscale via simple weighting image_gray = ( image_normalized[:, :, 0] * 0.2989 + image_normalized[:, :, 1] * 0.5870 + image_normalized[:, :, 2] * 0.1140 ) .. GENERATED FROM PYTHON SOURCE LINES 50-52 Helper Visualization Functions ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 52-136 .. code-block:: Python def show_result_with_colorbar(title, result, units="Value", percentile_range=(5, 95)): """ Displays a 2D result with a colorbar scaled using the given percentile range. Parameters ---------- title : str Title for the plot. result : ndarray 2D array representing the image or field to display. units : str Label for the colorbar (e.g., 'Intensity', 'Radians', etc.). percentile_range : tuple or None Percentiles to use for scaling the colormap. If None, use the min and max of the data. """ h, w = result.shape aspect_ratio = h / float(w) fig_width = 6.0 fig_height = fig_width * aspect_ratio fig, ax = plt.subplots(figsize=(fig_width, fig_height)) # Determine vmin and vmax based on percentiles if provided if percentile_range is not None: pmin, pmax = np.percentile(result, percentile_range) im = ax.imshow(result, cmap='gray', aspect='equal', vmin=pmin, vmax=pmax) cbar_label = f"{units} range [{pmin:.3f}, {pmax:.3f}]" else: vmin, vmax = result.min(), result.max() im = ax.imshow(result, cmap='gray', aspect='equal', vmin=vmin, vmax=vmax) cbar_label = f"{units} range [{vmin:.3f}, {vmax:.3f}]" ax.set_title(title) ax.axis('off') # Create a colorbar with matching height using make_axes_locatable divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) cbar = plt.colorbar(im, cax=cax) cbar.set_label(cbar_label) plt.tight_layout() plt.show() def show_angle_result(title, angle_data, vmin, vmax, units="Radians"): """ Displays angle data in a cyclical color map (hsv), with vmin and vmax specifying the circular range. Parameters ---------- title : str Title for the plot. angle_data : ndarray 2D array of angles (in radians). vmin : float Minimum of angle range (e.g., 0). vmax : float Maximum of angle range (e.g., 2*pi). units : str Label for the colorbar (e.g., 'Direction (radians)', etc.). """ h, w = angle_data.shape aspect_ratio = h / float(w) fig_width = 6.0 fig_height = fig_width * aspect_ratio fig, ax = plt.subplots(figsize=(fig_width, fig_height)) # Plot with an HSV cyclical colormap im = ax.imshow(angle_data, cmap='hsv', aspect='equal', vmin=vmin, vmax=vmax) ax.set_title(title) ax.axis('off') # Add colorbar divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) cbar = plt.colorbar(im, cax=cax, ticks=[vmin, (vmin+vmax)/2, vmax]) cbar.set_label(f"{units} range [{vmin:.2f}, {vmax:.2f}]") plt.tight_layout() plt.show() .. GENERATED FROM PYTHON SOURCE LINES 137-138 Show the original grayscale image with a colorbar .. GENERATED FROM PYTHON SOURCE LINES 138-140 .. code-block:: Python show_result_with_colorbar("Original Image", image_gray, units="Intensity") .. image-sg:: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_001.png :alt: Original Image :srcset: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 141-143 Gradient Magnitude ------------------ .. GENERATED FROM PYTHON SOURCE LINES 143-149 .. code-block:: Python diff = differentials(image_gray.copy()) diff.run(differentials.GRADIENT_MAGNITUDE) grad_magnitude_result = diff.image show_result_with_colorbar("Gradient Magnitude", grad_magnitude_result, units="Value") .. image-sg:: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_002.png :alt: Gradient Magnitude :srcset: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Completed in 1.16 seconds .. GENERATED FROM PYTHON SOURCE LINES 150-152 Gradient Direction ------------------ .. GENERATED FROM PYTHON SOURCE LINES 152-168 .. code-block:: Python diff = differentials(image_gray.copy()) diff.run(differentials.GRADIENT_DIRECTION) grad_direction_result = diff.image # Shift from [-π, π] to [0, 2π] grad_direction_result_0_2pi = (grad_direction_result + 2.0*np.pi) % (2.0*np.pi) # Visualize with HSV colormap, removing percentile clipping show_angle_result( "Gradient Direction", grad_direction_result_0_2pi, vmin=0.0, vmax=2.0*np.pi, units="Direction (radians)" ) .. image-sg:: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_003.png :alt: Gradient Direction :srcset: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Completed in 1.14 seconds .. GENERATED FROM PYTHON SOURCE LINES 169-171 Laplacian --------- .. GENERATED FROM PYTHON SOURCE LINES 171-177 .. code-block:: Python diff = differentials(image_gray.copy()) diff.run(differentials.LAPLACIAN) laplacian_result = diff.image show_result_with_colorbar("Laplacian", laplacian_result, units="Value") .. image-sg:: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_004.png :alt: Laplacian :srcset: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Completed in 1.35 seconds .. GENERATED FROM PYTHON SOURCE LINES 178-180 Largest Hessian --------------- .. GENERATED FROM PYTHON SOURCE LINES 180-186 .. code-block:: Python diff = differentials(image_gray.copy()) diff.run(differentials.LARGEST_HESSIAN) largest_hessian_result = diff.image show_result_with_colorbar("Largest Hessian Eigenvalue", largest_hessian_result, units="Value") .. image-sg:: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_005.png :alt: Largest Hessian Eigenvalue :srcset: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Completed in 2.52 seconds .. GENERATED FROM PYTHON SOURCE LINES 187-189 Smallest Hessian ---------------- .. GENERATED FROM PYTHON SOURCE LINES 189-195 .. code-block:: Python diff = differentials(image_gray.copy()) diff.run(differentials.SMALLEST_HESSIAN) smallest_hessian_result = diff.image show_result_with_colorbar("Smallest Hessian Eigenvalue", smallest_hessian_result, units="Value") .. image-sg:: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_006.png :alt: Smallest Hessian Eigenvalue :srcset: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Completed in 2.45 seconds .. GENERATED FROM PYTHON SOURCE LINES 196-198 Hessian Orientation ------------------- .. GENERATED FROM PYTHON SOURCE LINES 198-209 .. code-block:: Python diff = differentials(image_gray.copy()) diff.run(differentials.HESSIAN_ORIENTATION) hessian_orientation_result = diff.image show_angle_result( "Hessian Orientation", hessian_orientation_result, vmin=-np.pi/2.0, vmax=np.pi/2.0, units="Orientation (radians)" ) .. image-sg:: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_007.png :alt: Hessian Orientation :srcset: /auto_examples/07_differentiate/images/sphx_glr_07_01_differentiate_module_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Completed in 2.50 seconds .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 13.671 seconds) .. _sphx_glr_download_auto_examples_07_differentiate_07_01_differentiate_module.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/splineops/splineops.github.io/main?urlpath=lab/tree/notebooks_binder/auto_examples/07_differentiate/07_01_differentiate_module.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/07_differentiate/07_01_differentiate_module.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 07_01_differentiate_module.ipynb <07_01_differentiate_module.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 07_01_differentiate_module.py <07_01_differentiate_module.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 07_01_differentiate_module.zip <07_01_differentiate_module.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_