How it works
This page explains the calculations K-Space Paint performs behind the scenes, using the same definitions as the actual program.
K-Space Paint is a teaching tool for learning how the Fourier transform relates images to k-space. It does not reproduce how an MRI scanner acquires signals or reconstructs images, and it must not be used for medical decisions (see “Differences from real MRI” below).
1. Grid and coordinates
Both image space and k-space are grids ().
- Image pixels: . The origin is the top-left corner of the image, and increases downward.
- k-space frequencies: . The center of the k-space on screen is (the DC component), and frequencies get higher toward the edges. Positive is to the right and positive is downward.
2. Fourier transform
Drawing an image (forward transform)
The app computes the 2D discrete Fourier transform of the image you draw, (white = 1, black = 0).
Drawing in k-space (inverse transform)
The app computes the 2D inverse discrete Fourier transform of the k-space you draw, (complex-valued).
The normalization factor is omitted. Because the display divides by the maximum value, the result looks the same on screen with or without it. The calculation uses a fast Fourier transform (radix-2 FFT), but the results match the definitions above (see “6. Verification”).
Because both transforms are discrete, the image wraps around periodically: the top and bottom edges, and the left and right edges, are treated as neighbors.
3. Display processing
Raw results are hard to see, so the following processing is applied before converting them to brightness values from 0 to 1. What you see is a processed version of the result, so brightness is not proportional to the magnitude of the values.
| What is shown | Displayed value |
|---|---|
| Reconstructed image |
The absolute value (magnitude) is divided by its maximum and brightened slightly (gamma 0.85). |
| k-space (result of the Fourier transform, or k-space taken from an image) |
The values are compressed logarithmically so that the center (DC component) does not drown out everything else (the views labeled “log scale”). Phase is not displayed. |
| k-space you drew yourself | is shown as is (0 to 1). |
| Tiny points of 1–4 cells in k-space | A single cell of the 256×256 grid is only a few pixels on screen and easy to miss. So for any group of nonzero cells connected horizontally, vertically or diagonally that has 4 cells or fewer and no other points or lines within 2 cells, each cell is drawn as a 5×5 block in the display only (e.g. the “Center point”, “Symmetric pair” and “Random” presets). The data used in the calculation is unchanged, and the reconstructed image is not affected. |
Example: why the stripes look finer with “Symmetric pair”
The “Symmetric pair” preset puts the value 1 at and . This gives . Since the screen shows the absolute value , the negative parts are also bright, so you see twice as many bright stripes as there are cosine periods.
4. Brush
Each brush dab adds a smooth Gaussian-shaped value centered on your finger (or mouse). The amount added to a cell at offset from the center is:
- : brush radius. From the “Size” slider value (0.6 to 4), in k-space and in image space (in cells)
- : the “Strength” slider value (0.2 to 1) for the pen, and for the eraser
| Where and which tool | How the value changes |
|---|---|
| Image space, pen / eraser | |
| k-space, pen | is added to the real part. If the magnitude exceeds 1, it is clamped to 1 while keeping the phase. |
| k-space, eraser | Only the magnitude is reduced; the phase is kept. |
Symmetry mode (Hermitian symmetry)
When “Symmetry” is on, whatever you draw at is also drawn at the point mirrored through the center, . k-space drawn in symmetry mode (and k-space taken from an image) satisfies
(the complex conjugate), so the inverse transform is real-valued. This gives a natural look, closer to an image of a real object. Note that the column and the row (the left and top edges of the screen) pair with themselves, because their counterparts would lie outside the grid.
5. Loading images
- Insert image: the image is scaled so that its shorter side fits, and the center is cropped to a 256×256 square. It is converted to grayscale with .
- Edit in k-space (the same happens when you insert an image): the image is forward-transformed, everything is divided so that the largest magnitude becomes 1, and both magnitude and phase are brought into k-space. If you change nothing, the inverse transform gives back the original image.
6. Verification
The program's FFT results were checked against values computed by directly summing the definitions above (October 3, 2026).
- For both the forward and inverse transforms, the relative error was about 1×10−7, within the rounding error of single-precision floating-point arithmetic.
- When k-space drawn in symmetry mode was inverse-transformed, the imaginary part was at most 1×10−7 times the real part (i.e. the image is real-valued).
7. Differences from real MRI
The “k-space” in K-Space Paint is simply the 2D discrete Fourier transform of the image. Real MRI differs in the following ways.
- In MRI, k-space is the signal received by the receiver coils while gradient fields are applied. Under ideal conditions it is related to the image by the Fourier transform, but this app does not simulate how the signal is generated or acquired at all (relaxation, pulse sequences, gradients and so on).
- Noise, multiple receiver coils, coil sensitivity, magnetic field inhomogeneity and patient motion are not included.
- k-space is treated as Cartesian data that fills the whole 256×256 grid. There is no non-Cartesian sampling (radial, spiral, etc.), undersampling, parallel imaging, partial Fourier, or filtering (window functions).
- The reconstructed image is a magnitude image, processed for display as described in section 3.