### Install dtcwt via pip Source: https://github.com/rjw57/dtcwt/blob/master/docs/gettingstarted.rst Installs the dtcwt library using the pip package manager, which is the easiest and recommended way to get started. ```console $ pip install dtcwt ``` -------------------------------- ### Install dtcwt from source using setup.py Source: https://github.com/rjw57/dtcwt/blob/master/docs/gettingstarted.rst Installs the dtcwt library from a local source checkout using the standard Python setup.py script. This method is useful for development or installing specific versions not available via pip. ```console $ python setup.py install ``` -------------------------------- ### Install test requirements and run tests Source: https://github.com/rjw57/dtcwt/blob/master/docs/gettingstarted.rst Installs the necessary dependencies for the test suite and then executes the tests using py.test to verify the library's functionality. This also writes test-coverage information. ```console $ pip install -r tests/requirements.txt $ py.test ``` -------------------------------- ### Build Sphinx documentation for dtcwt Source: https://github.com/rjw57/dtcwt/blob/master/docs/gettingstarted.rst Builds the project's documentation locally using Sphinx, generating HTML files that can be found in the build/docs/html/ directory. ```console $ python setup.py build_sphinx ``` -------------------------------- ### Install dtcwt on Ubuntu Source: https://github.com/rjw57/dtcwt/blob/master/README.rst Instructions for installing the dtcwt library and its documentation on Ubuntu 15.10 and later using the apt-get package manager. ```bash sudo apt-get install python-dtcwt python-dtcwt-doc ``` -------------------------------- ### Install dtcwt from source Source: https://github.com/rjw57/dtcwt/blob/master/README.rst Steps to install the dtcwt library from a checked-out source repository using Python's setuptools. This method is useful for developers or those wanting the latest in-development version. ```bash python setup.py install ``` -------------------------------- ### Install dtcwt using pip Source: https://github.com/rjw57/dtcwt/blob/master/README.rst The easiest way to install the dtcwt library on various operating systems using the pip package manager. ```bash pip install dtcwt ``` -------------------------------- ### DTCWT Setup for Single Small Image (64x64) Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Initializes input data for a single 64x64 image, sets up both Numpy and TensorFlow 2D DTCWT transforms, and prepares TensorFlow placeholders and a session for execution. This block defines the common setup for small image tests. ```python # Create the input h, w = 64, 64 in_ = np.random.randn(1,h,w) # Set up the transforms nlevels = 3 tf.reset_default_graph() fwd = dtcwt.Transform2d() # Numpy Transform fwd_tf = dtcwt.tf.Transform2d() # Tensorflow Transform in_placeholder = tf.placeholder(tf.float32, [None, h, w]) out_tf = fwd_tf.forward(in_placeholder, nlevels=nlevels) out_fft = tf.fft2d(tf.cast(in_placeholder, tf.complex64)) sess = tf.Session() sess.run(tf.global_variables_initializer()) ``` -------------------------------- ### Full Example: Display Initial Unregistered Images Source: https://github.com/rjw57/dtcwt/blob/master/docs/registration.rst This complete script loads image frames and displays them combined in red and green channels to visually identify misregistration before any processing. It serves as a self-contained example for initial data visualization. ```python from pylab import * import datasets ref, src = datasets.regframes('traffic') figure() imshow(np.dstack((ref, src, np.zeros_like(ref)))) title('Registration input images') ``` -------------------------------- ### Import Core Libraries for Image Processing Source: https://github.com/rjw57/dtcwt/blob/master/docs/registration.rst This snippet imports essential Python libraries: `pylab` for numerical operations and plotting, and `datasets` for convenient access to example image data. ```python from pylab import * import datasets ``` -------------------------------- ### Run dtcwt test suite Source: https://github.com/rjw57/dtcwt/blob/master/README.rst Commands to install necessary test requirements and then execute the py.test suite to verify the dtcwt library's functionality and generate test coverage information. ```bash pip install -r tests/requirements.txt py.test ``` -------------------------------- ### Import Libraries for 3D DTCWT Transform Source: https://github.com/rjw57/dtcwt/blob/master/docs/3dtransform.rst Imports essential libraries: 'matplotlib.pylab' for plotting, 'numpy' for numerical operations, and 'dtcwt' for the Discrete Wavelet Transform functionalities. ```python from matplotlib.pylab import * import dtcwt ``` -------------------------------- ### Initial Setup and Library Imports Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Imports necessary libraries for numerical operations (Numpy), plotting (Matplotlib), DTCWT implementations (dtcwt, dtcwt.tf), and TensorFlow. It also configures Matplotlib style and GPU usage for performance testing. ```python %matplotlib inline import matplotlib.pyplot as plt import datasets import dtcwt import dtcwt.tf import tensorflow as tf from time import time import numpy as np import os import py3nvml plt.style.use('seaborn') py3nvml.grab_gpus(1); ``` -------------------------------- ### DTCWT Setup for Single Large Image (512x512) Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Initializes input data for a single 512x512 image, sets up both Numpy and TensorFlow 2D DTCWT transforms, and prepares TensorFlow placeholders and a session for execution. This mirrors the small image setup but for larger dimensions. ```python # Create the input h, w = 512, 512 in_ = np.random.randn(1,h,w) # Set up the transforms nlevels = 3 tf.reset_default_graph() fwd = dtcwt.Transform2d() # Numpy Transform fwd_tf = dtcwt.tf.Transform2d() # Tensorflow Transform in_placeholder = tf.placeholder(tf.float32, [None, h, w]) out_tf = fwd_tf.forward(in_placeholder, nlevels=nlevels) out_fft = tf.fft2d(tf.cast(in_placeholder, tf.complex64)) sess = tf.Session() sess.run(tf.global_variables_initializer()) ``` -------------------------------- ### TensorFlow DTCWT Performance on CPU Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Demonstrates setting up and timing the forward pass of a DTCWT on a CPU. It includes examples for both single image and batch processing to evaluate performance characteristics. ```python # Create the input h, w = 512, 512 in_ = np.random.randn(1,h,w) # Set up the transforms nlevels = 3 tf.reset_default_graph() with tf.device("/cpu:0"): fwd_tf = dtcwt.tf.Transform2d() # Tensorflow Transform in_placeholder = tf.placeholder(tf.float32, [None, h, w]) out_tf = fwd_tf.forward(in_placeholder, nlevels=nlevels) sess = tf.Session() sess.run(tf.global_variables_initializer()) large_tf_cpu = %timeit -o sess.run(out_tf.lowpass_op, {in_placeholder: in_}) ``` ```python in_ = np.random.randn(100,h,w) large_tf_batch_cpu = %timeit -o sess.run(out_tf.lowpass_op, {in_placeholder: in_}) ``` -------------------------------- ### Python Packages for Documentation Build Source: https://github.com/rjw57/dtcwt/blob/master/tests/docs-requirements.txt This snippet lists the specific Python packages necessary to compile the project's documentation. These packages include Sphinx for documentation generation, `sphinx-rtd-theme` for styling, and other utilities like `docutils`, `ipython`, and `matplotlib` which might be used in examples or for rendering. ```Python Requirements docutils ipython matplotlib sphinx sphinx-rtd-theme ``` -------------------------------- ### Perform 2D Dual-Tree Complex Wavelet Transform on Image Source: https://github.com/rjw57/dtcwt/blob/master/docs/2dtransform.rst This Python script demonstrates the application of a 2D Dual-Tree Complex Wavelet Transform (DTCWT) to the standard 'mandrill' image. It loads the image, performs a two-level forward transform using `dtcwt.Transform2d`, and then visualizes the absolute values and phase angles of the high-pass coefficients from the second level. The script utilizes `matplotlib.pyplot` for plotting and `numpy` for numerical operations. Ensure `dtcwt`, `matplotlib`, and `numpy` are installed. ```python # Load the mandrill image mandrill = datasets.mandrill() # Show mandrill figure(1) imshow(mandrill, cmap=cm.gray, clim=(0,1)) import dtcwt transform = dtcwt.Transform2d() # Compute two levels of dtcwt with the defaul wavelet family mandrill_t = transform.forward(mandrill, nlevels=2) # Show the absolute images for each direction in level 2. # Note that the 2nd level has index 1 since the 1st has index 0. figure(2) for slice_idx in range(mandrill_t.highpasses[1].shape[2]): subplot(2, 3, slice_idx) imshow(np.abs(mandrill_t.highpasses[1][:,:,slice_idx]), cmap=cm.spectral, clim=(0, 1)) # Show the phase images for each direction in level 2. figure(3) for slice_idx in range(mandrill_t.highpasses[1].shape[2]): subplot(2, 3, slice_idx) imshow(np.angle(mandrill_t.highpasses[1][:,:,slice_idx]), cmap=cm.hsv, clim=(-np.pi, np.pi)) ``` -------------------------------- ### TensorFlow DTCWT and Convolution Setup (GPU/Default) Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Initializes a TensorFlow graph for Discrete Wavelet Transform (DTCWT) and applies a 2D convolution to its high-pass output. This snippet sets up the computation graph and times its execution, typically on a GPU if available. ```python tf.reset_default_graph() in_placeholder = tf.placeholder(tf.float32, [None, h, w]) fwd_tf = dtcwt.tf.Transform2d() out_tf = fwd_tf.forward(in_placeholder, nlevels=3) p = tf.abs(out_tf.highpasses_ops[2]) weights = tf.get_variable('weights', shape=(5,5,6,64)) out = tf.nn.conv2d(p, weights, strides=[1,1,1,1], padding='SAME') sess = tf.Session() sess.run(tf.global_variables_initializer()) ``` ```python large_tf_conv = %timeit -o sess.run(out, {in_placeholder: in_}) ``` -------------------------------- ### Safely Manage dtcwt Backend with `preserve_backend_stack` Source: https://github.com/rjw57/dtcwt/blob/master/docs/backends.rst This example demonstrates the safer way to temporarily switch `dtcwt` backends using `dtcwt.preserve_backend_stack` with a `with` statement. The backend is automatically restored to its original state upon exiting the `with` block, preventing unintended side effects and ensuring clean state management. ```python with dtcwt.preserve_backend_stack(): dtcwt.push_backend('opencl') my_benchmarking_function() ``` -------------------------------- ### Full Example: Display Warped Image After Registration Source: https://github.com/rjw57/dtcwt/blob/master/docs/registration.rst This comprehensive script performs image loading, DTCWT transformation, registration estimation, and image warping. It then displays the reference and warped source images to show the effect of successful registration, demonstrating the full alignment workflow. ```python from pylab import * import datasets ref, src = datasets.regframes('traffic') import dtcwt transform = dtcwt.Transform2d() ref_t = transform.forward(ref, nlevels=6) src_t = transform.forward(src, nlevels=6) import dtcwt.registration as registration reg = registration.estimatereg(src_t, ref_t) warped_src = registration.warp(src, reg, method='bilinear') figure() imshow(np.dstack((ref, warped_src, np.zeros_like(ref)))) title('Source image warped to reference') ``` -------------------------------- ### Switch to TensorFlow Backend in dtcwt Source: https://github.com/rjw57/dtcwt/blob/master/docs/backends.rst This example shows how to configure `dtcwt` to use the TensorFlow backend. After calling `dtcwt.push_backend('tf')`, all `dtcwt` operations will leverage TensorFlow for computations. ```python dtcwt.push_backend('tf') xfm = Transform2d() # ... Transform2d, etc now use Tensorflow ... ``` -------------------------------- ### Inspect Shapes of 3D DTCWT Transform Output Source: https://github.com/rjw57/dtcwt/blob/master/docs/3dtransform.rst Demonstrates how to access and print the shapes of the lowpass and highpass coefficients obtained from the 3D DTCWT forward transform. This illustrates the hierarchical decomposition and the dimensions of the resulting arrays. ```python print(sphere_t.lowpass.shape) for highpasses in sphere_t.highpasses: print(highpasses.shape) ``` -------------------------------- ### Initialize and Perform 2D DT-CWT with NumPy Backend Source: https://github.com/rjw57/dtcwt/blob/master/docs/backends.rst This Python example demonstrates how to initialize a `Transform2d` object from the `dtcwt.numpy` module and perform a 4-level Discrete Wavelet Transform (DT-CWT) on an input array `X`. It then shows how to access and display the low-pass and high-pass components of the transformed output `Y`. ```python from dtcwt.numpy import Transform2d trans = Transform2d() # You may optionally specify which wavelets to use here Y = trans.forward(X, nlevels=4) # Perform a 4-level transform of X imshow(Y.lowpass) # Show coarsest scale low-pass image imshow(Y.highpasses[-1][:,:,0]) # Show first coarsest scale subband ``` -------------------------------- ### Full Example: Plot Velocity Field as Quiver Map Source: https://github.com/rjw57/dtcwt/blob/master/docs/registration.rst This comprehensive script performs image registration and then calculates and visualizes the velocity field as a quiver map overlaid on the reference image. It demonstrates the complete process from raw images to estimated and visualized motion. ```python from pylab import * import datasets ref, src = datasets.regframes('traffic') import dtcwt transform = dtcwt.Transform2d() ref_t = transform.forward(ref, nlevels=6) src_t = transform.forward(src, nlevels=6) import dtcwt.registration as registration reg = registration.estimatereg(src_t, ref_t) warped_src = registration.warp(src, reg, method='bilinear') vxs, vys = registration.velocityfield(reg, ref.shape[:2], method='bilinear') vxs = vxs * ref.shape[1] vys = vys * ref.shape[0] figure() X, Y = np.meshgrid(np.arange(ref.shape[1]), np.arange(ref.shape[0])) imshow(ref, cmap=cm.gray, clim=(0,1)) step = 8 quiver(X[::step,::step], Y[::step,::step], vxs[::step,::step], vys[::step,::step], color='g', angles='xy', scale_units='xy', scale=0.25) title('Estimated velocity field (x4 scale)') ``` -------------------------------- ### Full Example: Plot Velocity Field Magnitude Source: https://github.com/rjw57/dtcwt/blob/master/docs/registration.rst This complete script performs image registration and velocity field calculation, then visualizes the magnitude of the velocity field using a heatmap. It provides a self-contained demonstration of identifying and visualizing areas of significant motion. ```python from pylab import * import datasets ref, src = datasets.regframes('traffic') import dtcwt transform = dtcwt.Transform2d() ref_t = transform.forward(ref, nlevels=6) src_t = transform.forward(src, nlevels=6) import dtcwt.registration as registration reg = registration.estimatereg(src_t, ref_t) warped_src = registration.warp(src, reg, method='bilinear') vxs, vys = registration.velocityfield(reg, ref.shape[:2], method='bilinear') vxs = vxs * ref.shape[1] vys = vys * ref.shape[0] figure() imshow(np.abs(vxs + 1j*vys), cmap=cm.hot) title('Velocity field magnitude') ``` -------------------------------- ### Prepare Input Data for Small Image Batch (100x64x64) Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Generates a batch of 100 random 64x64 images for subsequent performance testing of DTCWT implementations on multiple inputs. This setup is crucial for evaluating batch processing efficiency. ```python in_ = np.random.randn(100,h,w) ``` -------------------------------- ### Setup for DTCWT with Post-Convolution on Large Image Batch Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Initializes input data for a batch of 100 512x512 images and sets up a TensorFlow graph for a 2D convolution operation on the highpass outputs of the DTCWT. This demonstrates a chained GPU operation where data remains on the device. ```python h, w = 512, 512 in_ = np.random.randn(100, h, w) fwd = dtcwt.Transform2d() tf.reset_default_graph() sess = tf.Session() highs = tf.placeholder(tf.float32, [None, h>>3, w>>3, 6]) weights = tf.get_variable('weights', shape=(5,5,6,64)) step = tf.nn.conv2d(highs, weights, strides=[1,1,1,1], padding='SAME') sess.run(tf.global_variables_initializer()) ``` -------------------------------- ### dtcwt.Pyramid Class Structure Source: https://github.com/rjw57/dtcwt/blob/master/docs/3dtransform.rst Documents the structure of the `dtcwt.Pyramid` class, which is returned by the forward 3D DTCWT transform. It contains the lowpass approximation and a tuple of highpass coefficients for each decomposition level. ```APIDOC dtcwt.Pyramid: lowpass: numpy.ndarray The lowpass image or signal, representing the coarse approximation. highpasses: tuple of numpy.ndarray A tuple containing complex highpass coefficients for each level of decomposition. Each element is a NumPy array. ``` -------------------------------- ### Generate 3D Sphere and Perform Forward DTCWT Transform Source: https://github.com/rjw57/dtcwt/blob/master/docs/3dtransform.rst Generates a 3D spherical array using NumPy for demonstration. It then initializes a 'dtcwt.Transform3d' object and applies the forward 3D Discrete Wavelet Transform to the sphere, specifying 2 decomposition levels. ```python GRID_SIZE = 64 SPHERE_RAD = int(0.45 * GRID_SIZE) + 0.5 grid = np.arange(-(GRID_SIZE>>1), GRID_SIZE>>1) X, Y, Z = np.meshgrid(grid, grid, grid) r = np.sqrt(X*X + Y*Y + Z*Z) sphere = 0.5 + 0.5 * np.clip(SPHERE_RAD-r, -1, 1) trans = dtcwt.Transform3d() sphere_t = trans.forward(sphere, nlevels=2) ``` -------------------------------- ### Perform 1D DWT Transform and Reconstruction with dtcwt Source: https://github.com/rjw57/dtcwt/blob/master/docs/1dtransform.rst This Python script generates two 1D random walks, applies a 5-level 1D Discrete Wavelet Transform (DWT) using `dtcwt.Transform1d`, visualizes intermediate coefficients (highpass and lowpass), and then reconstructs the original signals using the inverse transform. It also calculates and displays the reconstruction error, demonstrating the accuracy of the transform. ```python from matplotlib.pylab import * import dtcwt # Generate a 300x2 array of a random walk vecs = np.cumsum(np.random.rand(300,2) - 0.5, 0) # Show input figure() plot(vecs) title('Input') # 1D transform, 5 levels transform = dtcwt.Transform1d() vecs_t = transform.forward(vecs, nlevels=5) # Show level 2 highpass coefficient magnitudes figure() plot(np.abs(vecs_t.highpasses[1])) title('Level 2 wavelet coefficient magnitudes') # Show last level lowpass image figure() plot(vecs_t.lowpass) title('Lowpass signals') # Inverse vecs_recon = transform.inverse(vecs_t) # Show output figure() plot(vecs_recon) title('Output') # Show error figure() plot(vecs_recon - vecs) title('Reconstruction error') print('Maximum reconstruction error: {0}'.format(np.max(np.abs(vecs - vecs_recon)))) ``` -------------------------------- ### Visualize Directional Sensitivity of 3D DTCWT Coefficients Source: https://github.com/rjw57/dtcwt/blob/master/docs/3dtransform.rst This snippet visualizes the directional sensitivity of the 3D DTCWT. It first plots a 2D slice of the input sphere. Then, it creates multiple 3D subplots to show the spatial distribution of large magnitude highpass wavelet coefficients, highlighting the transform's ability to capture directional features. ```python from mpl_toolkits.mplot3d import Axes3D figure() imshow(sphere[:,:,GRID_SIZE>>1], interpolation='none', cmap=cm.gray) title('2d slice from input sphere') # Plot large magnitude wavelet coefficients' position in 3D. figure(figsize=(16,9)) Yh = sphere_t.highpasses nplts = Yh[-1].shape[3] nrows = np.ceil(np.sqrt(nplts)) ncols = np.ceil(nplts / nrows) W = np.max(Yh[-1].shape[:3]) for idx in range(Yh[-1].shape[3]): C = np.abs(Yh[-1][:,:,:,idx]) ax = gcf().add_subplot(nrows, ncols, idx+1, projection='3d') ax.set_aspect('equal') good = C > 0.2*C.max() x,y,z = np.nonzero(good) ax.scatter(x, y, z, c=C[good].ravel()) ax.auto_scale_xyz((0,W), (0,W), (0,W)) tight_layout() ``` -------------------------------- ### Prepare Input Data for Large Image Batch (100x512x512) Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Generates a batch of 100 random 512x512 images for performance testing of DTCWT implementations on multiple large inputs. This setup is designed to highlight TensorFlow's natural advantage in handling batches on the GPU. ```python in_ = np.random.randn(100,512,512) ``` -------------------------------- ### Standard 2D DT-CWT Transform and Perfect Reconstruction Source: https://github.com/rjw57/dtcwt/blob/master/docs/variant.rst This example illustrates the application of the standard 2-D Dual-Tree Complex Wavelet Transform, demonstrating its perfect reconstruction property. It initializes `dtcwt.Transform2d` with 'near_sym_b' and 'qshift_b' wavelets, performs a forward transform on an image from the `datasets.mandrill()` and then an inverse transform. The resulting error signal, shown via `imshow(Z-image)`, is minimal, indicating reconstruction limited only by floating-point precision. ```python import dtcwt # Use the standard 2-D DTCWT transform = dtcwt.Transform2d(biort='near_sym_b', qshift='qshift_b') # Forward transform image = datasets.mandrill() image_t = transform.forward(image) # Inverse transform Z = transform.inverse(image_t) # Show the error imshow(Z-image, cmap=cm.gray) colorbar() ``` -------------------------------- ### Perform Inverse 3D DTCWT Transform and Check Reconstruction Error Source: https://github.com/rjw57/dtcwt/blob/master/docs/3dtransform.rst Applies the inverse 3D Discrete Wavelet Transform to reconstruct the original signal from its coefficients. It then calculates and prints the maximum absolute difference between the reconstructed and original signals to verify perfect reconstruction, expecting a value close to zero. ```python Z = trans.inverse(sphere_t) print(np.abs(Z - sphere).max()) # Should be < 1e-12 ``` -------------------------------- ### Build dtcwt Sphinx documentation Source: https://github.com/rjw57/dtcwt/blob/master/README.rst Instructions to build a local copy of the dtcwt library's documentation using the Sphinx documentation system, with output found in the build/docs/html/ directory. ```bash python setup.py build_sphinx ``` -------------------------------- ### Temporarily Switch dtcwt Backend with Push/Pop Source: https://github.com/rjw57/dtcwt/blob/master/docs/backends.rst This snippet illustrates how to temporarily change the `dtcwt` backend using a push/pop mechanism. It first runs a function with the default (NumPy) backend, then switches to OpenCL, runs the function again, and finally reverts to the previous backend using `dtcwt.pop_backend()`. ```python # Run benchmark with NumPy my_benchmarking_function() # Run benchmark with OpenCL dtcwt.push_backend('opencl') my_benchmarking_function() dtcwt.pop_backend() ``` -------------------------------- ### Core DTCWT Library API Modules Source: https://github.com/rjw57/dtcwt/blob/master/docs/reference.rst Documents the main interfaces and core functionalities of the `dtcwt` library, including modules for keypoint analysis, image sampling, registration, plotting, and general utilities, as well as MATLAB compatibility. ```APIDOC Main interface: - dtcwt - dtcwt.coeffs Keypoint analysis: - dtcwt.keypoint Image sampling: - dtcwt.sampling Image registration: - dtcwt.registration Plotting functions: - dtcwt.plotting Miscellaneous and low-level support functions: - dtcwt.utils Compatibility with MATLAB: - dtcwt.compat ``` -------------------------------- ### Switch to OpenCL Backend in dtcwt Source: https://github.com/rjw57/dtcwt/blob/master/docs/backends.rst This snippet demonstrates how to switch the `dtcwt` library's backend to OpenCL using `dtcwt.push_backend()`. Subsequent operations like `Transform2d` will then utilize the OpenCL backend for computations. ```python dtcwt.push_backend('opencl') xfm = Transform2d() # ... Transform2d, etc now use OpenCL ... ``` -------------------------------- ### Perform DTCWT Forward Transform on Images Source: https://github.com/rjw57/dtcwt/blob/master/docs/registration.rst This snippet imports the `dtcwt` library and initializes a 2D Dual-Tree Complex Wavelet Transform. It then applies the forward transform to both the reference and source images, `ref` and `src`, to obtain their wavelet coefficients at 6 levels. ```python import dtcwt transform = dtcwt.Transform2d() ref_t = transform.forward(ref, nlevels=6) src_t = transform.forward(src, nlevels=6) ``` -------------------------------- ### dtcwt Library Backend and Transform API Reference Source: https://github.com/rjw57/dtcwt/blob/master/docs/backends.rst This section documents key classes, modules, and functions within the `dtcwt` library related to its multi-backend support for Discrete Wavelet Transforms. It covers the main transform class, result structures, backend-specific modules, and backend management functions. ```APIDOC dtcwt.Transform2d: Description: Top-level class for performing 2D Discrete Wavelet Transforms. Usage: Automatically uses the NumPy backend by default. Can be instantiated to perform forward transforms. Methods: forward(X, nlevels): Description: Performs a multi-level transform on input X. Parameters: X: Input array (e.g., NumPy array). nlevels: Number of transform levels. Returns: An instance of a class behaving like dtcwt.Pyramid. dtcwt.Pyramid: Description: Base class representing the result structure of a DT-CWT transform. Properties: lowpass: Coarsest scale low-pass image. highpasses: List of high-pass subbands for each level. dtcwt.opencl.Pyramid: Description: Specific Pyramid instance for the OpenCL backend, keeping device-side results available. dtcwt.numpy: Description: Module containing the NumPy-based reference implementation of the wavelet transform. dtcwt.opencl: Description: Module containing the OpenCL-based implementation of the wavelet transform. dtcwt.tf: Description: Module containing the Tensorflow-based implementation of the wavelet transform. dtcwt.push_backend(backend_name): Description: Function to manipulate the default backend used by dtcwt.Transform2d and similar classes. Parameters: backend_name: The name of the backend to push (e.g., 'numpy', 'opencl', 'tensorflow'). Tensorflow Backend Specific Methods: forward_channels: Description: Method to accept batches of images for parallel processing on GPU. inverse_channels: Description: Method to accept batches of images for parallel processing on GPU. ``` -------------------------------- ### DTCWT Backend API Modules (NumPy, OpenCL, Tensorflow) Source: https://github.com/rjw57/dtcwt/blob/master/docs/reference.rst Documents the backend-specific implementations for the `dtcwt` library, including modules for NumPy, OpenCL, and Tensorflow. These modules provide optimized computations, with notes on their specific functionalities and limitations, such as Tensorflow's single precision support and specific `forward_channels`/`inverse_channels` methods. ```APIDOC Backends: NumPy: - dtcwt.numpy (members, inherited-members) - dtcwt.numpy.lowlevel (members) OpenCL: - dtcwt.opencl (members, inherited-members) - dtcwt.opencl.lowlevel (members) Tensorflow: - dtcwt.tf (members, inherited-members) - dtcwt.tf.lowlevel (members) Notes: - Currently only supports single precision operations. - Transform1d() and Transform2d() classes have 'forward_channels' and 'inverse_channels' methods for batch processing. - Transform3d() still uses NumPy backend. ``` -------------------------------- ### Ancillary Functions for DTCWT/DWT Test M-Files Source: https://github.com/rjw57/dtcwt/blob/master/ORIGINAL_README.txt Details supporting functions used by the test M-files for visualization and figure management, including drawing images, handling complex subimages, generating circular discs, and setting up figure windows. ```APIDOC draw: Draw an image in a correctly sized figure window. cimage5: Draw a complex subimage using a colour palette for the complex numbers. drawcirc: Generate a circular disc image. setfig: Set up a predefined figure window. settitle: Set the title of a figure window. ``` -------------------------------- ### Visualize DTCWT Performance on Small Images Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Generates a bar chart comparing the average performance of Numpy and TensorFlow DTCWT implementations for single and batched small images. The results are displayed in milliseconds, providing a visual summary of the speed comparison. ```python objects = ('np single', 'tf single', 'np batch', 'tf batch') y_pos = np.arange(len(objects)) performance = [small_np.average, small_tf.average, small_np_batch.average, small_tf_batch.average] performance = [i*1000 for i in performance] fig, ax = plt.subplots(1, figsize=(8,8)) ax.bar(y_pos, performance, align='center', alpha=0.5, color=['red', 'blue']) plt.xticks(y_pos, objects) plt.ylabel('Time (ms)') plt.title('Times to perform DTCWT on small images') plt.show() ``` -------------------------------- ### DTCWT and DWT Shift Invariance Test M-Files Source: https://github.com/rjw57/dtcwt/blob/master/ORIGINAL_README.txt Lists M-files designed to demonstrate the shift invariance properties of the Dual-Tree Complex Wavelet Transform (DTCWT) compared to the Discrete Wavelet Transform (DWT) in both 1-D and 2-D, including a movie visualization. ```APIDOC shift_test_1D: Demonstrate shift invariance in 1-D shift_test_2D: Demonstrate shift invariance in 2-D shiftmovie: Show 1-D shift invariance as a movie. ``` -------------------------------- ### Python API for DTCWT Image Registration Module Source: https://github.com/rjw57/dtcwt/blob/master/docs/registration.rst Access the image registration algorithm's implementation through the `dtcwt.registration` module. This module provides core functions for estimating affine parameters and applying image warping. ```APIDOC Module: dtcwt.registration Description: Provides functions for implementing the DTCWT-based image registration algorithm. Functions: estimatereg(image_data, ...) Description: Estimates affine parameters (a_chi) for each 8x8 block in the image. Parameters: image_data: The input image for which to estimate parameters. warp(image_data, affine_parameter_vectors, ...) Description: Warps an image using a set of provided affine parameter vectors. Parameters: image_data: The image to be warped. affine_parameter_vectors: The affine parameters (a_chi) obtained from estimatereg or similar. ``` -------------------------------- ### Visualize Warped Source and Reference Images Source: https://github.com/rjw57/dtcwt/blob/master/docs/registration.rst This snippet displays the reference image and the warped source image, combined in red and green channels. This visualization helps confirm the effectiveness of the image registration by showing a marked reduction in color fringes. ```python figure() imshow(np.dstack((ref, warped_src, np.zeros_like(ref)))) title('Source image warped to reference') ``` -------------------------------- ### Load and Visualize Unregistered Input Images Source: https://github.com/rjw57/dtcwt/blob/master/docs/registration.rst This code loads two image frames, a reference (`ref`) and a source (`src`), using `datasets.regframes`. It then displays them combined in red and green channels to visually highlight any initial misregistration. ```python ref, src = datasets.regframes('traffic') figure() imshow(np.dstack((ref, src, np.zeros_like(ref)))) title('Registration input images') ``` -------------------------------- ### Estimate Image Registration and Warp Source Image Source: https://github.com/rjw57/dtcwt/blob/master/docs/registration.rst This code imports the `dtcwt.registration` module. It estimates the registration parameters (`reg`) between the transformed source and reference images. Subsequently, it warps the original source image (`src`) to align with the reference using the estimated registration and a bilinear interpolation method. ```python import dtcwt.registration as registration reg = registration.estimatereg(src_t, ref_t) warped_src = registration.warp(src, reg, method='bilinear') ``` -------------------------------- ### Visualize DTCWT Performance Benchmarks Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Generates a bar chart to visually compare the performance of different DTCWT operations across NumPy, TensorFlow (GPU), and TensorFlow (CPU) for single and batch image processing, including convolution scenarios. ```python objects = ('np one img', 'tf one img\n(gpu)', 'tf one img\n(cpu)', 'np batch', 'tf batch\n(gpu)', 'tf batch\n(cpu)', 'np batch\n+conv', 'tf batch\n+conv\n(gpu)', 'tf batch\n+conv\n(cpu)') y_pos = np.arange(len(objects)) performance = [large_np.average, large_tf.average, large_tf_cpu.average, large_np_batch.average, large_tf_batch.average, large_tf_batch_cpu.average, large_np_conv.average, large_tf_conv.average, large_tf_conv_cpu.average] performance = [i*1000 for i in performance] fig, ax = plt.subplots(1, figsize=(12,12)) ax.bar(y_pos, performance, align='center', alpha=0.5, color=['red', 'blue', 'green']) plt.xticks(y_pos, objects) plt.ylabel('Time (ms)') plt.title('Times to perform different DTCWT operations on large images') plt.show() ``` -------------------------------- ### Discrete Wavelet Transform (DWT) Core Functions for Comparison Source: https://github.com/rjw57/dtcwt/blob/master/ORIGINAL_README.txt Provides equivalent functions for the standard Discrete Wavelet Transform (DWT), primarily included for comparative analysis with the DTCWT. These cover 1D and 2D decomposition and reconstruction. ```APIDOC wavexfm: 1D DWT decomposition waveifm: 1D DWT reconstruction wavexfm2: 2D DWT decomposition waveifm2: 2D DWT reconstruction ``` -------------------------------- ### Test 2D Dual-Tree Complex Wavelet Transform (DTCWT) Package in Matlab Source: https://github.com/rjw57/dtcwt/blob/master/ORIGINAL_README.txt Illustrates how to test the 2-D DTCWT package in Matlab. It loads an image, performs 2D decomposition and reconstruction, and computes the reconstruction error, expecting it to be minimal. ```Matlab load lenna figure; draw(X); drawnow [Yl,Yh] = dtwavexfm2(X,4,'near_sym_b','qshift_b'); Z = dtwaveifm2(Yl,Yh,'near_sym_b','qshift_b'); figure; draw(Z) dtcwt_error = max(abs(Z(:)-X(:))) % Error should be < 1e-12 ``` -------------------------------- ### Measure TensorFlow FFT Performance on Single Small Image Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Measures the execution time of TensorFlow's 2D FFT operation for a single 64x64 image using `%timeit`. This serves as a highly optimized GPU operation benchmark to gauge the overheads of the DTCWT implementation. ```python %timeit sess.run(out_fft, {in_placeholder: in_}) ``` -------------------------------- ### Measure Numpy DTCWT Performance on Single Small Image Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Measures the execution time of the Numpy DTCWT forward transform for a single 64x64 image using the `%timeit` IPython magic command. This provides a baseline for CPU-based DTCWT performance. ```python small_np = %timeit -o for i in in_: fwd.forward(i, nlevels=nlevels) ``` -------------------------------- ### Measure TensorFlow DTCWT Performance on Single Small Image Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Measures the execution time of the TensorFlow DTCWT forward transform's lowpass output for a single 64x64 image using the `%timeit` IPython magic command and a TensorFlow session. This evaluates GPU-accelerated DTCWT performance. ```python small_tf = %timeit -o sess.run(out_tf.lowpass_op, {in_placeholder: in_}) ``` -------------------------------- ### Discrete Wavelet Transform (DWT) Lower-Level Utilities Source: https://github.com/rjw57/dtcwt/blob/master/ORIGINAL_README.txt Describes lower-level functions specific to the Discrete Wavelet Transform (DWT), focusing on column filtering with decimation and interpolation by two. ```APIDOC coldwtfilt: Column filtering with decimation by 2. coliwtfilt: Column filtering with interpolation (upsampling) by 2. ``` -------------------------------- ### Test 1D Dual-Tree Complex Wavelet Transform (DTCWT) Package in Matlab Source: https://github.com/rjw57/dtcwt/blob/master/ORIGINAL_README.txt Demonstrates how to test the 1-D DTCWT package in Matlab. It generates a random 1D signal, performs decomposition and reconstruction, and calculates the reconstruction error, which should be very small. ```Matlab X = rand(512,1); figure; plot(X); drawnow [Yl,Yh] = dtwavexfm(X,5,'near_sym_b','qshift_b'); Z = dtwaveifm(Yl,Yh,'near_sym_b','qshift_b'); figure; plot(Z) dtcwt_error = max(abs(Z-X)) % Error should be < 1e-12 ``` -------------------------------- ### TensorFlow CPU Session Configuration for Benchmarking Source: https://github.com/rjw57/dtcwt/blob/master/tests/Speed Tests.ipynb Defines a TensorFlow session configuration to restrict operations to a single CPU thread, useful for precise performance benchmarking and isolating CPU-specific behavior. ```python session_conf = tf.ConfigProto( intra_op_parallelism_threads=1, inter_op_parallelism_threads=1, device_count={'CPU': 1}) ```