PhyCV Documentation

repository·main·Indexed 19 days ago

https://github.com/jalalilabucla/phycv

A physics-inspired computer vision library that uses physical laws, such as light propagation and diffraction, for image processing. It provides CPU and GPU-accelerated implementations of three main algorithm suites: Phase-Stretch Transform (PST) for edge and texture detection, Phase-Stretch Adaptive Gradient-field Extractor (PAGE) for directional edge detection, and Vision Enhancement via Virtual diffraction and coherent Detection (VEViD) for low-light and color enhancement.

Tokens
3.5K
Snippets
9
Records
14
Agent score
68%

What's inside PhyCV

  1. Overview of PhyCV algorithms

    main

    PhyCV is a physics-inspired computer vision library that emulates the propagation of light through physical media followed by coherent detection. It currently provides three main algorithm suites, each available in CPU and GPU versions:

    1. Phase-Stretch Transform (PST): A computationally efficient edge and texture detection algorithm designed for high performance in visually impaired images.
    2. Phase-Stretch Adaptive Gradient-field Extractor (PAGE): An algorithm for detecting edges and their orientations at various scales using diffraction equations. It uses a bank of filters to detect directional edges, allowing it to pick up structural details that PST might miss.
    3. Vision Enhancement via Virtual diffraction and coherent Detection (VEViD): A low-light and color enhancement algorithm that treats a digital image as a spatially varying light field and applies processes akin to diffraction and coherent detection.
  2. Install PhyCV via pip or from source

    main

    You can install PhyCV using pip or by cloning the repository and installing from source.

    Note on GPU support: The GPU versions of the algorithms require PyTorch and torchvision. Ensure you have installed the correct version of CUDA ToolKit and PyTorch for your system following the official PyTorch instructions before installing PhyCV.

    # From pip
    pip install phycv
    
    # From source
    git clone https://github.com/JalaliLabUCLA/phycv.git
    cd phycv
    pip install .
  3. Optimize PAGE for Video Processing

    main

    When processing video sequences with PAGE, you can optimize performance by calling init_kernel only once if the physical parameters (mu_1, mu_2, sigma_1, sigma_2, S1, S2) remain constant across all frames. This avoids redundant kernel computations for every frame.

    Note that processing video can be time-consuming due to the overhead of saving processed results and reassembling them into a video file.

  4. Optimize PST for video processing

    main
    When processing video frames with the PST or PST_GPU classes, you can optimize performance by separating the kernel initialization from the frame processing. If the transformation parameters (phase_strength and warp_strength) are constant across all frames, call init_kernel only once before starting the frame loop. This avoids redundant computations for every frame.
  5. Use the Phase-Stretch Adaptive Gradient-field Extractor (PAGE) class

    main

    The PAGE class is used for edge extraction using physics-inspired gradient fields. You can use the high-level run method to execute the entire pipeline (loading, kernel initialization, application, and edge creation) in one call, or manually control each step.

    CPU Implementation

    To use the CPU version, instantiate PAGE and provide direction_bins. If h (height) and w (width) are not provided during initialization, they will be inferred from the image loaded via load_img.

    GPU Acceleration

    The PAGE_GPU class (available in phycv/page_gpu.py) provides significant acceleration. It follows a similar API to the CPU version but with these key differences:

    • Device Selection: You must specify a device (e.g., 'cuda') during instantiation, similar to PyTorch.
    • Data Types: Image I/O uses torchvision and matrix operations use torch instead of numpy or opencv.
    • Parallelism: init_kernel and apply_kernel use broadcasting to process direction bins in parallel.
    • Output: Results are returned as torch.Tensor objects located on the specified GPU device.
    from phycv.page import PAGE
    
    # Initialize with direction bins
    page = PAGE(direction_bins=8)
    
    # Run the full pipeline on a single image
    # Parameters: img_file, mu_1, mu_2, sigma_1, sigma_2, S1, S2, sigma_LPF, thresh_min, thresh_max, morph_flag
    result = page.run(
        img_file='path/to/image.jpg',
        mu_1=0.1, mu_2=0.2, sigma_1=1.0, sigma_2=1.0, S1=1.0, S2=1.0,
        sigma_LPF=2.0, thresh_min=0.5, thresh_max=0.8, morph_flag=1
    )
  6. Use the VEVID class for vision enhancement

    main

    The VEVID class implements Vision Enhancement via Virtual diffraction and coherent Detection. It provides methods for image loading, kernel initialization, and applying the enhancement kernel to the V (Value) channel of an HSV image.

    Workflow

    1. Instantiate: Create a VEVID instance. You can optionally specify h (height) and w (width) during initialization. If left as None, they are inferred from the loaded image.
    2. Load Image: Use load_img to provide either an img_file path or an img_array. The class automatically converts the image from RGB to HSV.
    3. Initialize Kernel: Call init_kernel(S, T) where S is the phase scale and T is the phase variance.
    4. Apply Enhancement: Use apply_kernel with regularization constant b and phase activation gain G.
      • Set color=True for color enhancement.
      • Set color=False (default) for low-light enhancement.
      • Set lite=True to use an approximated version of the algorithm.

    Alternatively, use the convenience methods run (full algorithm) or run_lite (accelerated version) to wrap these steps.

    # Full algorithm workflow
    vevid = VEVID()
    result = vevid.run(img_file='path/to/image.jpg', S=S_param, T=T_param, b=b_param, G=G_param, color=False)
    
    # Accelerated (lite) workflow
    vevid_lite = VEVID()
    result = vevid_lite.run_lite(img_file='path/to/image.jpg', b=b_param, G=G_param, color=False)
  7. PAGE Class API Reference

    main

    The PAGE class provides the following methods for edge extraction:

    • __init__(self, direction_bins, h=None, w=None): Initializes the extractor. h and w are optional and can be set to None to be determined by the image shape.
    • load_img(self, img_file=None, img_array=None): Loads an image from a file or a numpy array. Converts RGB to grayscale. If h and w were not set in __init__, they are set here.
    • init_kernel(self, mu_1, mu_2, sigma_1, sigma_2, S1, S2): Initializes the PAGE kernels based on physical parameters. In the CPU version, this is done serially per frequency bin.
    • apply_kernel(self, sigma_LPF, thresh_min, thresh_max, morph_flag): Denoises the image with a low-pass filter (sigma_LPF), applies the kernels, and optionally applies morphological operations if morph_flag == 1 using thresh_min and thresh_max.
    • create_page_edge(self): Generates a weighted color image to visualize the directionality of the extracted edges.
    • run(self, img_file, mu_1, mu_2, sigma_1, sigma_2, S1, S2, sigma_LPF, thresh_min, thresh_max, morph_flag): A convenience wrapper that executes load_img, init_kernel, apply_kernel, and create_page_edge in sequence.
  8. Accelerate PST using GPU (PST_GPU)

    main

    For significantly faster processing, use the PST_GPU class (defined in phycv/pst_gpu.py). It follows a similar API to the CPU-based PST class but with the following differences:

    • Device Selection: You must specify a device (following PyTorch convention, e.g., 'cuda') during instantiation.
    • Backend: Uses torch for matrix operations and torchvision for Image IO instead of numpy and opencv.
    • Output: The resulting processed image is returned as a torch.Tensor located on the specified GPU device.
    from phycv import PST_GPU
    
    # Instantiate with a specific device
    pst_gpu = PST_GPU(device='cuda')
    # Methods like .run() work similarly to the CPU version
    result = pst_gpu.run(img_file='image.png', ...)
    # result is a torch.Tensor on GPU
  9. Use the PST class for Phase-Stretch Transform

    main

    The PST class implements the Phase-Stretch Transform. You can use the high-level run method to execute the entire pipeline in one call, or manually control the lifecycle using load_img, init_kernel, and apply_kernel.

    Workflow

    1. Initialization: Instantiate PST(h=None, w=None). If h and w are provided, the input image will be reshaped to these dimensions. If None, dimensions are inferred from the loaded image.
    2. Loading: Use load_img(img_file=..., img_array=...). RGB images are automatically converted to grayscale.
    3. Kernel Setup: Call init_kernel(S, W) where S is phase strength and W is warp strength.
    4. Execution: Call apply_kernel(...) to denoise (via sigma_LPF), apply the kernel, and optionally perform morphological operations if morph_flag == 1 using thresh_min and thresh_max.
    from phycv import PST
    
    # High-level approach
    pst = PST()
    pst.run(
        img_file='path/to/image.png',
        phase_strength=S,
        warp_strength=W,
        sigma_LPF=sigma,
        thresh_min=t_min,
        thresh_max=t_max,
        morph_flag=1
    )
  10. Use VEVID_GPU for accelerated processing

    main

    The VEVID_GPU class (defined in phycv/vevid_gpu.py) provides a GPU-accelerated version of the VEViD algorithm. Its API is architecturally similar to the standard VEVID class, but with the following requirements and behaviors:

    1. Device Specification: You must indicate the device (e.g., a CUDA device) when instantiating the class.
    2. Input Format: Images must be loaded as torch.Tensor objects instead of numpy.ndarray.
    3. Output Format: The returned results are torch.Tensor objects located on the GPU.
  11. PST Parameters for edge and texture detection

    main

    When using the Phase-Stretch Transform (PST), the following parameters are used to control the transformation and detection process:

    • phase_strength: Controls the phase component of the transform.
    • warp_strength: Controls the warping effect.
    • sigma_LPF: Low-pass filter parameter.
    • thresh_min: Minimum threshold for detection.
    • thresh_max: Maximum threshold for detection.
    • morph_flag: Morphological operation flag.
    # Example parameter set from sample results
    phase_strength = 0.4
    warp_strength = 20
    sigma_LPF = 0.1
    thresh_min = 0.0
    thresh_max = 0.8
    morph_flag = 1
  12. VEViD Parameters for vision enhancement

    main

    Vision Enhancement via Virtual diffraction and coherent Detection (VEViD) is used for low-light and color enhancement. It has two modes: standard VEViD and VEViD Lite.

    Standard VEViD parameters:

    • S: Scaling parameter.
    • T: Temporal/threshold parameter.
    • b: Bias parameter.
    • G: Gain parameter.

    VEViD Lite parameters:

    • b: Bias parameter.
    • G: Gain parameter.
    # Example parameter set for VEViD
    S = 0.2
    T = 0.001
    b = 0.16
    G = 1.4