JavaScript Image Frequency-Domain Graffiti and Restoration: A Practical Guide

    This article presents a browser-based JavaScript approach to image graffiti and restoration across five representation domains: inverse matrix mapping, amplitude spectrum, phase spectrum, combined amplitude and phase, and row derivatives. It explains the underlying reversible transform model, implementation details for each mode, and key engineering concerns such as normalization, numerical stability, performance, and reversibility checks. Readers will learn how different domains carry visual information and how to build the full transform–graffiti–inverse transform pipeline using only general math and pixel-level operations.

    1. Background and Overall Approach

    In image processing and computer vision coursework, a classic and intuitive exercise is to apply a reversible transform to an image, move it into another representation domain, add graffiti (perturbation, occlusion, noise, or local edits) in that domain, and then apply the inverse transform to restore the image. Comparing the original image, the transformed-domain graffiti, and the restored image makes it easy to observe how different representation domains carry information.

    The value of this exercise goes beyond novelty. It highlights several core facts:

    1. Image information can be decomposed into different representation domains, and each domain carries different semantics and sensitivity.
    2. Some domains are extremely sensitive to local perturbations (for example, phase), while others are relatively robust (for example, magnitude).
    3. As long as the transform is reversible and numerically well-conditioned, the perturbed information can still be fully or partially restored.
    4. Engineering implementations must handle boundaries, normalization, numerical overflow, and performance.

    This article uses browser-side JavaScript as the runtime and covers five modes: inverse matrix mapping, amplitude spectrum, phase spectrum, simultaneous amplitude and phase, and row derivative. All implementations rely only on general math and pixel-level operations, with no dependency on any specific third-party library.

    2. Mathematical Background: Reversible Transforms and Graffiti

    Let the original image be a grayscale matrix (I(u,v)), with pixel values normalized to ([0,1]). A transform–graffiti–restoration pipeline can be abstracted as:

    1. J = T(I) // forward transform
    2. J' = D(J) // graffiti in the transform domain
    3. I_rec = T^{-1}(J') // inverse transform for restoration

    Here (T) must be reversible, and (D) is the perturbation operator we control. If (D) is the identity mapping, then (I_{rec}=I); if (D) destroys certain information, the restored image will show the corresponding degradation.

    The most common transform in the browser is the two-dimensional discrete Fourier transform (DFT/FFT). For an (N\times M) image:

    [
    F(k,l)=\sum_{u,v} I(u,v)e^{-2\pi i(ku/N+lv/M)}
    ]

    The magnitude spectrum is (A=|F|), and the phase spectrum is (\phi=\arg F). For restoration, reconstruct (F=A e^{i\phi}), apply the inverse transform, and take the real part.

    3. Implementation Details for the Five Modes

    Mode 1: Graffiti and Restoration via Inverse Matrix Mapping

    Here, the “inverse matrix” refers to applying a reversible linear mapping to pixel coordinates or pixel values. A common approach is to flatten the image into a vector, multiply it by a reversible matrix (P), add graffiti in the transform domain, and then multiply by (P^{-1}) to restore.

    Key points:

    • (P) must be numerically invertible; avoid ill-conditioned matrices. In practice, use orthogonal matrices or matrices with diagonal dominance.
    • Graffiti is applied in the transform domain, for example by zeroing certain components or adding a bias.
    • Restoration error mainly comes from floating-point error and the graffiti itself.

    Example code:

    1. function matMul(A, B) { /* naive O(n^3) multiplication, usable when n is small */ }
    2. function invert(M) { /* Gauss-Jordan or LU decomposition for inversion */ }
    3. function inverseMatrixMode(pixels, P, graffiti) {
    4. const Pinv = invert(P);
    5. const y = matMul(P, pixels); // forward transform
    6. graffiti(y); // graffiti in the transform domain
    7. const x = matMul(Pinv, y); // inverse transform
    8. return x;
    9. }

    Mode 2: Graffiti and Restoration on the Amplitude Spectrum

    The amplitude spectrum carries the image’s energy distribution and overall contrast structure. Applying graffiti to the amplitude spectrum (for example, flattening the mid-frequency region or adding noise to high frequencies) changes overall brightness and texture after restoration, while edge positions are largely preserved.

    Implementation flow:

    1. Apply FFT to the grayscale image to obtain complex arrays.
    2. Compute magnitude (A) and phase (\phi).
    3. Perturb only (A) while keeping (\phi) unchanged.
    4. Reconstruct (F’=A’ e^{i\phi}) and apply the inverse FFT, taking the real part.
    1. function fft2(re, im) { /* 2D FFT, in-place or returning new arrays */ }
    2. function ifft2(re, im) { /* inverse transform */ }
    3. function amplitudeGraffiti(re, im, maskFn) {
    4. const { re: Fr, im: Fi } = fft2(re, im);
    5. for (let i = 0; i < Fr.length; i++) {
    6. const a = Math.hypot(Fr[i], Fi[i]);
    7. const ph = Math.atan2(Fi[i], Fr[i]);
    8. const a2 = maskFn(a, i); // graffiti on magnitude
    9. Fr[i] = a2 * Math.cos(ph);
    10. Fi[i] = a2 * Math.sin(ph);
    11. }
    12. return ifft2(Fr, Fi).re; // take the real part
    13. }

    Mode 3: Graffiti and Restoration on the Phase Spectrum

    The phase spectrum determines structure and position. Experimentally, scrambling the phase or zeroing it locally makes the restored image unrecognizable, while the energy distribution (brightness statistics) still matches the magnitude. This shows that phase contributes more to recognizable structure.

    The implementation is symmetric to Mode 2, except that magnitude is kept unchanged and phase is perturbed:

    1. function phaseGraffiti(re, im, phaseFn) {
    2. const { re: Fr, im: Fi } = fft2(re, im);
    3. for (let i = 0; i < Fr.length; i++) {
    4. const a = Math.hypot(Fr[i], Fi[i]);
    5. const ph = phaseFn(Math.atan2(Fi[i], Fr[i]), i);
    6. Fr[i] = a * Math.cos(ph);
    7. Fi[i] = a * Math.sin(ph);
    8. }
    9. return ifft2(Fr, Fi).re;
    10. }

    Mode 4: Simultaneous Graffiti on Amplitude and Phase

    Perturbing both at once causes the restored image to lose both contrast structure and spatial structure, usually making it the hardest to recognize. This mode is suitable for demonstrating “superimposed degradation of information.” In implementation, simply chain the two mask functions from Modes 2 and 3, applying magnitude first and then phase to avoid overwriting one with the other.

    Mode 5: Graffiti and Restoration via Row Derivatives

    The row derivative is the first-order difference along the row direction, (D_x I(u,v)=I(u+1,v)-I(u,v)). After applying graffiti in the derivative domain, restore by integration:

    [
    I(u,v)=I(0,v)+\sum_{k=0}^{u-1} D_x I(k,v)
    ]

    Key points:

    • The first column must be preserved as the integration constant.
    • If graffiti breaks integrability (for example, randomly modifying derivatives), restoration will show accumulated drift.
    • This mode is highly sensitive to abrupt changes in the horizontal direction.
    1. function rowDerivativeMode(img, w, h, graffiti) {
    2. const d = new Float32Array(w * h);
    3. for (let y = 0; y < h; y++) {
    4. for (let x = 0; x < w - 1; x++) {
    5. d[y * w + x] = img[y * w + x + 1] - img[y * w + x];
    6. }
    7. d[y * w + (w - 1)] = 0;
    8. }
    9. graffiti(d);
    10. const out = new Float32Array(w * h);
    11. for (let y = 0; y < h; y++) {
    12. let acc = img[y * w]; // first column as the constant
    13. out[y * w] = acc;
    14. for (let x = 0; x < w - 1; x++) { acc += d[y * w + x]; out[y * w + x + 1] = acc; }
    15. }
    16. return out;
    17. }

    4. Key Engineering Details

    1. Normalization and clamping: after the inverse transform, values may slightly exceed the valid range and must be clamped to ([0,255]).
    2. Numerical stability: use partial pivoting for matrix inversion and double-precision floating point for FFT.
    3. Performance: in the browser, use a Web Worker to offload FFT computation and avoid blocking the UI.
    4. Visualization: write pixels directly back through Canvas ImageData, with a slider to adjust graffiti strength in real time.
    5. Reversibility check: when (D) is the identity, the maximum error between the restored image and the original should be on the order of (10^{-6}).

    5. Observable Conclusions

    • Amplitude spectrum graffiti mainly affects contrast and texture energy, while structural positions are largely preserved.
    • Phase spectrum graffiti destroys structural recognizability and is the main carrier of “semantic information.”
    • Simultaneous amplitude and phase graffiti causes the most severe degradation.
    • Row derivative graffiti produces accumulated error in the horizontal direction, reflecting sensitivity to the integration constant.
    • The inverse matrix mode directly demonstrates the graffiti–restoration loop of a linear reversible mapping.

    6. Extensions

    The framework can be extended to multi-channel processing (handle RGB channels separately or convert to YCbCr and process luminance), multi-scale processing (apply wavelet or pyramid decomposition first, then add graffiti layer by layer), and real-time interaction (draw graffiti directly in the transform domain with the mouse). For cloud batch processing, generic capabilities such as object storage, container platforms, and message queues can be used for task distribution and result archiving, without binding to any specific vendor interface.