JavaScript Image Transform-Domain Scribbling and Restoration: A Browser Implementation Guide

    This article describes a browser-based experiment for transforming images into alternative mathematical domains, scribbling on selected coefficients, and restoring the image through inverse transforms. It covers five modes—inverse-matrix, amplitude spectrum, phase spectrum, combined amplitude/phase, and row-derivative—and explains the architecture, canvas interaction, mask handling, FFT performance, and code organization. Readers learn how transform-domain modifications affect reconstruction and how to implement the workflow in JavaScript.

    1. Introduction: Why Scribble in a Transform Domain?

    One of the most intuitive experiments in image processing is to transform an image from the spatial domain into another mathematical domain—such as the frequency domain, gradient domain, or matrix decomposition domain—deliberately “damage” part of the data in that domain, and then restore the image through an inverse transform. By observing the differences in the restored results, you can gain a deeper understanding of what different components in each transform domain represent.

    Implementing this kind of experiment in the browser has natural advantages: no environment installation is required, interaction is immediate, and results are easy to share. This article introduces a web-based “image transform—scribble—restore” experiment tool that covers five modes:

    • Scribbling and restoration using the image inverse matrix
    • Scribbling and restoration using the image amplitude spectrum
    • Scribbling and restoration using the image phase spectrum
    • Restoration after scribbling on both amplitude and phase spectra
    • Scribbling and restoration using image row derivatives

    2. Overall Architecture Design

    The logic of the web tool can be abstracted into a one-way pipeline:

    1. Load image → Forward transform → Transform-domain visualization + scribble interaction → Inverse transform → Restored result display

    From an engineering perspective, the implementation can be split into modules:

    1. Image loading module: Reads a local image through a file selection control, draws it to an offscreen Canvas, and scales it to a fixed size (for example, 256×256). A fixed size serves two purposes: it makes the 2D circular implementation of the Fourier transform controllable, and it ensures a one-to-one correspondence between scribble coordinates and pixel coordinates.
    2. Transform kernel module: Encapsulates forward and inverse transforms. Inputs and outputs are Float32Array or two-dimensional arrays, decoupled from the rendering layer.
    3. Scribble interaction module: Listens for mouse or touch events, overlays brush strokes on the transform-domain canvas, and synchronously writes to a mask matrix that marks modified transform-domain coefficients.
    4. Restoration module: Sends coefficients—with scribbled regions set to zero or replaced by brush values—into the inverse transform and outputs the restored image.

    This “mask-driven destruction” design is more robust than directly modifying pixels: during scribbling, only the mask is updated; during restoration, the mask is applied once, avoiding multiple half-finished states.

    3. Technical Principles and Implementation Notes for the Five Modes

    3.1 Image Inverse-Matrix Scribbling and Restoration

    Treat the image as a large matrix A, then perform LU decomposition or direct inversion to obtain A⁻¹. A scribble operation is equivalent to overlaying a perturbation matrix Δ on the original image, so the corrupted image is A + Δ. If A⁻¹ has been saved, the propagation of the perturbation can theoretically be analyzed by solving (A + Δ)·x = I.

    In a web implementation, directly inverting a large image is too expensive. In practice, local inversion is usually performed on block matrices (for example, 16×16 tiles), with scribbling and restoration applied block by block. This controls computation and visually demonstrates the relationship between matrix invertibility and restoration fidelity. When a scribble happens to fall on an ill-conditioned region, obvious artifacts appear in the restoration—which is exactly where the teaching value lies.

    3.2 Amplitude-Spectrum Scribbling and Restoration

    Apply a two-dimensional discrete Fourier transform (DFT) to the image:

    1. function dft2d(img, N) {
    2. // 1D DFT row by row, then 1D DFT column by column (separability)
    3. const rows = dftRows(img, N);
    4. return dftCols(rows, N);
    5. }

    After obtaining the complex coefficients, decompose them into the amplitude spectrum |F(u,v)| and the phase spectrum φ(u,v). In amplitude-spectrum scribble mode, the user paints on the visualized amplitude spectrum—typically log-scaled and fftshift-centered so low frequencies are in the middle. Amplitude values in painted regions are set to zero, then recombined with the original phase spectrum and passed through an inverse DFT.

    Typical observations: painting high-frequency regions of the amplitude spectrum blurs edges and loses detail; painting the low-frequency center causes overall contrast and brightness to collapse. This directly confirms the classic conclusion that low frequencies carry the main structure while high frequencies carry details.

    3.3 Phase-Spectrum Scribbling and Restoration

    Destroying only the phase spectrum while keeping the amplitude spectrum is the best demonstration of phase importance. In the experiment, even a slight perturbation to phase can make the restored image unrecognizable—because phase encodes the spatial alignment relationships among frequency components, that is, “structural information,” while amplitude mainly determines the relative strength of each frequency component. This experimental conclusion directly helps in understanding modern image compression and reconstruction algorithms.

    3.4 Scribbling on Both Amplitude and Phase

    This mode lets the user paint on both spectra at the same time. The system maintains two separate masks and combines them when applying destruction during restoration. This mode is suitable for controlled experiments: with the same painted area, compare restoration differences among “amplitude only,” “phase only,” and “both,” and quantify the destructive power of phase distortion.

    3.5 Row-Derivative Scribbling and Restoration

    The first-order difference along the row direction, d(i,j) = f(i,j) − f(i,j−1), is an invertible transform: as long as the first pixel value of each row is retained as the integration constant, the image can be perfectly restored by cumulative summation. A scribble in the derivative domain is equivalent to injecting a step-like offset into the corresponding row of the original image—after painting a segment of the derivative, the restored image shifts in brightness across the entire row from that position onward. This mode elegantly demonstrates the idea that “a local modification in the transform domain corresponds to a global effect in the spatial domain,” complementing the frequency-domain experiments.

    4. Engineering Details of Scribble Interaction

    • Dual-canvas layering: The bottom Canvas renders the transform-domain grayscale image by writing pixels directly with putImageData. The top Canvas responds to the mouse with a semi-transparent red brush, ensuring scribbles can be undone and cleared.
    • Coordinate mapping: Scribble coordinates must go through the inverse mapping of fftshift to correctly correspond to the actual storage locations of spectrum coefficients.
    • Performance optimization: One-dimensional DFT can be replaced by a fast algorithm with a butterfly structure (FFT) instead of a naive O(N²) implementation. A 256×256 image can complete in tens of milliseconds in the browser, keeping interaction smooth.
    • Asynchronous debouncing: During scribbling, restoration is scheduled with requestAnimationFrame; the full inverse transform runs after the brush is lifted.

    5. Code Organization Sketch

    1. // Core data flow sketch
    2. const image = loadImageToGray(canvas); // Convert to grayscale
    3. const spectrum = fft2d(image); // Forward transform
    4. renderLogSpectrum(spectrum, viewCanvas); // Log visualization
    5. mask.bindBrush(overlayCanvas); // Scribble mask
    6. spectrum.applyMask(mask); // Apply destruction
    7. const restored = ifft2d(spectrum); // Inverse transform
    8. renderGray(restored, resultCanvas); // Display result

    6. Conclusion

    This web-based experiment tool turns abstract transform-domain concepts into a tangible interactive experience: the inverse-matrix mode demonstrates matrix invertibility, the amplitude/phase spectrum modes reveal the division of responsibilities among frequency-domain components, and the row-derivative mode shows the spatial effect of an invertible difference transform. For developers, it is also a complete browser-side numerical computing practice—covering floating-point array management, FFT engineering, dual-layer Canvas interaction, and performance scheduling. The same approach can be extended to more complex image-processing topics such as image denoising, frequency-domain filtering, and data hiding.